@@ -1738,8 +1738,9 @@ class Namer { typer: Typer =>
1738
1738
val tpe = (paramss : @ unchecked) match
1739
1739
case TypeSymbols (tparams) :: TermSymbols (vparams) :: Nil => tpFun(tparams, vparams)
1740
1740
case TermSymbols (vparams) :: Nil => tpFun(Nil , vparams)
1741
+ val rhsCtx = prepareRhsCtx(ctx.fresh, paramss)
1741
1742
if (isFullyDefined(tpe, ForceDegree .none)) tpe
1742
- else typedAheadExpr(mdef.rhs, tpe).tpe
1743
+ else typedAheadExpr(mdef.rhs, tpe)( using rhsCtx) .tpe
1743
1744
1744
1745
case TypedSplice (tpt : TypeTree ) if ! isFullyDefined(tpt.tpe, ForceDegree .none) =>
1745
1746
mdef match {
@@ -1937,14 +1938,7 @@ class Namer { typer: Typer =>
1937
1938
var rhsCtx = ctx.fresh.addMode(Mode .InferringReturnType )
1938
1939
if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode .InlineableBody )
1939
1940
if sym.is(ExtensionMethod ) then rhsCtx = rhsCtx.addMode(Mode .InExtensionMethod )
1940
- val typeParams = paramss.collect { case TypeSymbols (tparams) => tparams }.flatten
1941
- if (typeParams.nonEmpty) {
1942
- // we'll be typing an expression from a polymorphic definition's body,
1943
- // so we must allow constraining its type parameters
1944
- // compare with typedDefDef, see tests/pos/gadt-inference.scala
1945
- rhsCtx.setFreshGADTBounds
1946
- rhsCtx.gadtState.addToConstraint(typeParams)
1947
- }
1941
+ rhsCtx = prepareRhsCtx(rhsCtx, paramss)
1948
1942
1949
1943
def typedAheadRhs (pt : Type ) =
1950
1944
PrepareInlineable .dropInlineIfError(sym,
@@ -1989,4 +1983,15 @@ class Namer { typer: Typer =>
1989
1983
lhsType orElse WildcardType
1990
1984
}
1991
1985
end inferredResultType
1986
+
1987
+ /** Prepare a GADT-aware context used to type the RHS of a ValOrDefDef. */
1988
+ def prepareRhsCtx (rhsCtx : FreshContext , paramss : List [List [Symbol ]])(using Context ): FreshContext =
1989
+ val typeParams = paramss.collect { case TypeSymbols (tparams) => tparams }.flatten
1990
+ if typeParams.nonEmpty then
1991
+ // we'll be typing an expression from a polymorphic definition's body,
1992
+ // so we must allow constraining its type parameters
1993
+ // compare with typedDefDef, see tests/pos/gadt-inference.scala
1994
+ rhsCtx.setFreshGADTBounds
1995
+ rhsCtx.gadtState.addToConstraint(typeParams)
1996
+ rhsCtx
1992
1997
}
0 commit comments