@@ -1734,8 +1734,9 @@ class Namer { typer: Typer =>
17341734 val tpe = (paramss : @ unchecked) match
17351735 case TypeSymbols (tparams) :: TermSymbols (vparams) :: Nil => tpFun(tparams, vparams)
17361736 case TermSymbols (vparams) :: Nil => tpFun(Nil , vparams)
1737+ val rhsCtx = prepareRhsCtx(ctx.fresh, paramss)
17371738 if (isFullyDefined(tpe, ForceDegree .none)) tpe
1738- else typedAheadExpr(mdef.rhs, tpe).tpe
1739+ else typedAheadExpr(mdef.rhs, tpe)( using rhsCtx) .tpe
17391740
17401741 case TypedSplice (tpt : TypeTree ) if ! isFullyDefined(tpt.tpe, ForceDegree .none) =>
17411742 mdef match {
@@ -1933,14 +1934,7 @@ class Namer { typer: Typer =>
19331934 var rhsCtx = ctx.fresh.addMode(Mode .InferringReturnType )
19341935 if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode .InlineableBody )
19351936 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)
19441938
19451939 def typedAheadRhs (pt : Type ) =
19461940 PrepareInlineable .dropInlineIfError(sym,
@@ -1985,4 +1979,15 @@ class Namer { typer: Typer =>
19851979 lhsType orElse WildcardType
19861980 }
19871981 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
19881993}
0 commit comments