@@ -337,6 +337,21 @@ object Inliner {
337
337
def codeOf (arg : Tree , pos : SrcPos )(using Context ): Tree =
338
338
Literal (Constant (arg.show)).withSpan(pos.span)
339
339
}
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
+
340
355
}
341
356
342
357
/** Produces an inlined version of `call` via its `inlined` method.
@@ -1003,7 +1018,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
1003
1018
* scrutinee as RHS and type that corresponds to RHS.
1004
1019
*/
1005
1020
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
1007
1022
caseBindingMap += ((sym, ValDef (copied, constToLiteral(rhs)).withSpan(sym.span)))
1008
1023
}
1009
1024
@@ -1121,7 +1136,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
1121
1136
def reduceSubPatterns (pats : List [Tree ], selectors : List [Tree ]): Boolean = (pats, selectors) match {
1122
1137
case (Nil , Nil ) => true
1123
1138
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
1125
1140
val rhs = constToLiteral(selector)
1126
1141
elem.defTree = rhs
1127
1142
caseBindingMap += ((NoSymbol , ValDef (elem, rhs).withSpan(elem.span)))
0 commit comments