Skip to content

Commit

Permalink
Extract shared prepareRhsCtx
Browse files Browse the repository at this point in the history
(cherry picked from commit 2c81588)
  • Loading branch information
dwijnand committed Feb 23, 2024
1 parent 017c38d commit e6359f5
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1734,12 +1734,7 @@ class Namer { typer: Typer =>
val tpe = (paramss: @unchecked) match
case TypeSymbols(tparams) :: TermSymbols(vparams) :: Nil => tpFun(tparams, vparams)
case TermSymbols(vparams) :: Nil => tpFun(Nil, vparams)
val rhsCtx = (paramss: @unchecked) match
case TypeSymbols(tparams) :: TermSymbols(_) :: Nil =>
val rhsCtx = ctx.fresh.setFreshGADTBounds
rhsCtx.gadtState.addToConstraint(tparams)
rhsCtx
case TermSymbols(_) :: Nil => ctx
val rhsCtx = prepareRhsCtx(ctx.fresh, paramss)
if (isFullyDefined(tpe, ForceDegree.none)) tpe
else typedAheadExpr(mdef.rhs, tpe)(using rhsCtx).tpe

Expand Down Expand Up @@ -1939,14 +1934,7 @@ class Namer { typer: Typer =>
var rhsCtx = ctx.fresh.addMode(Mode.InferringReturnType)
if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode.InlineableBody)
if sym.is(ExtensionMethod) then rhsCtx = rhsCtx.addMode(Mode.InExtensionMethod)
val typeParams = paramss.collect { case TypeSymbols(tparams) => tparams }.flatten
if (typeParams.nonEmpty) {
// we'll be typing an expression from a polymorphic definition's body,
// so we must allow constraining its type parameters
// compare with typedDefDef, see tests/pos/gadt-inference.scala
rhsCtx.setFreshGADTBounds
rhsCtx.gadtState.addToConstraint(typeParams)
}
rhsCtx = prepareRhsCtx(rhsCtx, paramss)

def typedAheadRhs(pt: Type) =
PrepareInlineable.dropInlineIfError(sym,
Expand Down Expand Up @@ -1991,4 +1979,15 @@ class Namer { typer: Typer =>
lhsType orElse WildcardType
}
end inferredResultType

/** Prepare a GADT-aware context used to type the RHS of a ValOrDefDef. */
def prepareRhsCtx(rhsCtx: FreshContext, paramss: List[List[Symbol]])(using Context): FreshContext =
val typeParams = paramss.collect { case TypeSymbols(tparams) => tparams }.flatten
if typeParams.nonEmpty then
// we'll be typing an expression from a polymorphic definition's body,
// so we must allow constraining its type parameters
// compare with typedDefDef, see tests/pos/gadt-inference.scala
rhsCtx.setFreshGADTBounds
rhsCtx.gadtState.addToConstraint(typeParams)
rhsCtx
}

0 comments on commit e6359f5

Please sign in to comment.