@@ -1734,12 +1734,7 @@ 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 = (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)
1743
1738
if (isFullyDefined(tpe, ForceDegree .none)) tpe
1744
1739
else typedAheadExpr(mdef.rhs, tpe)(using rhsCtx).tpe
1745
1740
@@ -1939,14 +1934,7 @@ class Namer { typer: Typer =>
1939
1934
var rhsCtx = ctx.fresh.addMode(Mode .InferringReturnType )
1940
1935
if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode .InlineableBody )
1941
1936
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)
1950
1938
1951
1939
def typedAheadRhs (pt : Type ) =
1952
1940
PrepareInlineable .dropInlineIfError(sym,
@@ -1991,4 +1979,15 @@ class Namer { typer: Typer =>
1991
1979
lhsType orElse WildcardType
1992
1980
}
1993
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
1994
1993
}
0 commit comments