@@ -1738,8 +1738,9 @@ class Namer { typer: Typer =>
17381738 val tpe = (paramss : @ unchecked) match
17391739 case TypeSymbols (tparams) :: TermSymbols (vparams) :: Nil => tpFun(tparams, vparams)
17401740 case TermSymbols (vparams) :: Nil => tpFun(Nil , vparams)
1741+ val rhsCtx = prepareRhsCtx(ctx.fresh, paramss)
17411742 if (isFullyDefined(tpe, ForceDegree .none)) tpe
1742- else typedAheadExpr(mdef.rhs, tpe).tpe
1743+ else typedAheadExpr(mdef.rhs, tpe)( using rhsCtx) .tpe
17431744
17441745 case TypedSplice (tpt : TypeTree ) if ! isFullyDefined(tpt.tpe, ForceDegree .none) =>
17451746 mdef match {
@@ -1937,14 +1938,7 @@ class Namer { typer: Typer =>
19371938 var rhsCtx = ctx.fresh.addMode(Mode .InferringReturnType )
19381939 if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode .InlineableBody )
19391940 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)
19481942
19491943 def typedAheadRhs (pt : Type ) =
19501944 PrepareInlineable .dropInlineIfError(sym,
@@ -1989,4 +1983,15 @@ class Namer { typer: Typer =>
19891983 lhsType orElse WildcardType
19901984 }
19911985 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
19921997}
0 commit comments