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