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