@@ -337,6 +337,21 @@ object Inliner {
337337 def codeOf (arg : Tree , pos : SrcPos )(using Context ): Tree =
338338 Literal (Constant (arg.show)).withSpan(pos.span)
339339 }
340+
341+ extension (tp : Type ) {
342+
343+ /** same as widenTermRefExpr, but preserves modules and singleton enum values */
344+ private final def widenInlineScrutinee (using Context ): Type = tp.stripTypeVar match {
345+ case tp : TermRef =>
346+ val sym = tp.termSymbol
347+ if sym.isAllOf(EnumCase , butNot= JavaDefined ) || sym.is(Module ) then tp
348+ else if ! tp.isOverloaded then tp.underlying.widenExpr.widenInlineScrutinee
349+ else tp
350+ case _ => tp
351+ }
352+
353+ }
354+
340355}
341356
342357/** Produces an inlined version of `call` via its `inlined` method.
@@ -1003,7 +1018,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
10031018 * scrutinee as RHS and type that corresponds to RHS.
10041019 */
10051020 def newTermBinding (sym : TermSymbol , rhs : Tree ): Unit = {
1006- val copied = sym.copy(info = rhs.tpe.widenTermRefExpr , coord = sym.coord, flags = sym.flags &~ Case ).asTerm
1021+ val copied = sym.copy(info = rhs.tpe.widenInlineScrutinee , coord = sym.coord, flags = sym.flags &~ Case ).asTerm
10071022 caseBindingMap += ((sym, ValDef (copied, constToLiteral(rhs)).withSpan(sym.span)))
10081023 }
10091024
@@ -1121,7 +1136,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
11211136 def reduceSubPatterns (pats : List [Tree ], selectors : List [Tree ]): Boolean = (pats, selectors) match {
11221137 case (Nil , Nil ) => true
11231138 case (pat :: pats1, selector :: selectors1) =>
1124- val elem = newSym(InlineBinderName .fresh(), Synthetic , selector.tpe.widenTermRefExpr ).asTerm
1139+ val elem = newSym(InlineBinderName .fresh(), Synthetic , selector.tpe.widenInlineScrutinee ).asTerm
11251140 val rhs = constToLiteral(selector)
11261141 elem.defTree = rhs
11271142 caseBindingMap += ((NoSymbol , ValDef (elem, rhs).withSpan(elem.span)))
0 commit comments