@@ -1734,12 +1734,7 @@ 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 = (paramss : @ unchecked) match
1738- case TypeSymbols (tparams) :: TermSymbols (_) :: Nil =>
1739- val rhsCtx = ctx.fresh.setFreshGADTBounds
1740- rhsCtx.gadtState.addToConstraint(tparams)
1741- rhsCtx
1742- case TermSymbols (_) :: Nil => ctx
1737+ val rhsCtx = prepareRhsCtx(ctx.fresh, paramss)
17431738 if (isFullyDefined(tpe, ForceDegree .none)) tpe
17441739 else typedAheadExpr(mdef.rhs, tpe)(using rhsCtx).tpe
17451740
@@ -1939,14 +1934,7 @@ class Namer { typer: Typer =>
19391934 var rhsCtx = ctx.fresh.addMode(Mode .InferringReturnType )
19401935 if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode .InlineableBody )
19411936 if sym.is(ExtensionMethod ) then rhsCtx = rhsCtx.addMode(Mode .InExtensionMethod )
1942- val typeParams = paramss.collect { case TypeSymbols (tparams) => tparams }.flatten
1943- if (typeParams.nonEmpty) {
1944- // we'll be typing an expression from a polymorphic definition's body,
1945- // so we must allow constraining its type parameters
1946- // compare with typedDefDef, see tests/pos/gadt-inference.scala
1947- rhsCtx.setFreshGADTBounds
1948- rhsCtx.gadtState.addToConstraint(typeParams)
1949- }
1937+ rhsCtx = prepareRhsCtx(rhsCtx, paramss)
19501938
19511939 def typedAheadRhs (pt : Type ) =
19521940 PrepareInlineable .dropInlineIfError(sym,
@@ -1991,4 +1979,15 @@ class Namer { typer: Typer =>
19911979 lhsType orElse WildcardType
19921980 }
19931981 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
19941993}
0 commit comments