Skip to content

Commit 2c81588

Browse files
committed
Extract shared prepareRhsCtx
1 parent cc1e37e commit 2c81588

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

+13-14
Original file line numberDiff line numberDiff line change
@@ -1733,12 +1733,7 @@ class Namer { typer: Typer =>
17331733
val tpe = (paramss: @unchecked) match
17341734
case TypeSymbols(tparams) :: TermSymbols(vparams) :: Nil => tpFun(tparams, vparams)
17351735
case TermSymbols(vparams) :: Nil => tpFun(Nil, vparams)
1736-
val rhsCtx = (paramss: @unchecked) match
1737-
case TypeSymbols(tparams) :: TermSymbols(_) :: Nil =>
1738-
val rhsCtx = ctx.fresh.setFreshGADTBounds
1739-
rhsCtx.gadtState.addToConstraint(tparams)
1740-
rhsCtx
1741-
case TermSymbols(_) :: Nil => ctx
1736+
val rhsCtx = prepareRhsCtx(ctx.fresh, paramss)
17421737
if (isFullyDefined(tpe, ForceDegree.none)) tpe
17431738
else typedAheadExpr(mdef.rhs, tpe)(using rhsCtx).tpe
17441739

@@ -1938,14 +1933,7 @@ class Namer { typer: Typer =>
19381933
var rhsCtx = ctx.fresh.addMode(Mode.InferringReturnType)
19391934
if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode.InlineableBody)
19401935
if sym.is(ExtensionMethod) then rhsCtx = rhsCtx.addMode(Mode.InExtensionMethod)
1941-
val typeParams = paramss.collect { case TypeSymbols(tparams) => tparams }.flatten
1942-
if (typeParams.nonEmpty) {
1943-
// we'll be typing an expression from a polymorphic definition's body,
1944-
// so we must allow constraining its type parameters
1945-
// compare with typedDefDef, see tests/pos/gadt-inference.scala
1946-
rhsCtx.setFreshGADTBounds
1947-
rhsCtx.gadtState.addToConstraint(typeParams)
1948-
}
1936+
rhsCtx = prepareRhsCtx(rhsCtx, paramss)
19491937

19501938
def typedAheadRhs(pt: Type) =
19511939
PrepareInlineable.dropInlineIfError(sym,
@@ -1990,4 +1978,15 @@ class Namer { typer: Typer =>
19901978
lhsType orElse WildcardType
19911979
}
19921980
end inferredResultType
1981+
1982+
/** Prepare a GADT-aware context used to type the RHS of a ValOrDefDef. */
1983+
def prepareRhsCtx(rhsCtx: FreshContext, paramss: List[List[Symbol]])(using Context): FreshContext =
1984+
val typeParams = paramss.collect { case TypeSymbols(tparams) => tparams }.flatten
1985+
if typeParams.nonEmpty then
1986+
// we'll be typing an expression from a polymorphic definition's body,
1987+
// so we must allow constraining its type parameters
1988+
// compare with typedDefDef, see tests/pos/gadt-inference.scala
1989+
rhsCtx.setFreshGADTBounds
1990+
rhsCtx.gadtState.addToConstraint(typeParams)
1991+
rhsCtx
19931992
}

0 commit comments

Comments
 (0)