Skip to content

Commit 53f7603

Browse files
committed
deconstWiden
1 parent d143271 commit 53f7603

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/compiler/scala/tools/nsc/typechecker/Namers.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,10 @@ trait Namers extends MethodSynthesis {
879879
// If the definition can keep its inferred singleton type,
880880
// or widening would mean no longer conforming to the expected type,
881881
// we must still deconst unless it's a final val. Otherwise, widen.
882-
if (mayKeepSingletonType || cannotWiden) { if (sym.isFinal) tpe else tpe.deconst }
882+
if (mayKeepSingletonType || cannotWiden) {
883+
if (sym.isFinal || sym.isLocalToBlock || sym.isPrivate) tpe
884+
else tpe.deconstWiden
885+
}
883886
else tpe.widen
884887
} else {
885888
val shouldWiden = (

src/reflect/scala/reflect/internal/Types.scala

+7
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ trait Types
187187
override def widen = maybeRewrap(underlying.widen)
188188
override def narrow = underlying.narrow
189189
override def deconst = maybeRewrap(underlying.deconst)
190+
override def deconstWiden = maybeRewrap(underlying.deconstWiden)
190191
override def resultType = maybeRewrap(underlying.resultType)
191192
override def resultType(actuals: List[Type]) = maybeRewrap(underlying.resultType(actuals))
192193
override def paramSectionCount = 0
@@ -353,6 +354,11 @@ trait Types
353354
*/
354355
def deconst: Type = this
355356

357+
/** Map a constant type or not-null-type to its underlying base type,
358+
* identity for all other types.
359+
*/
360+
def deconstWiden: Type = this
361+
356362
/** The type of `this` of a class type or reference type. */
357363
def typeOfThis: Type = typeSymbol.typeOfThis
358364

@@ -1844,6 +1850,7 @@ trait Types
18441850
override def underlying: Type = if (sip23 && value.isSuitableLiteralType) LiteralType(value) else value.tpe // SIP-23
18451851
override def isTrivial: Boolean = true
18461852
override def deconst: Type = underlying.deconst
1853+
override def deconstWiden: Type = if (value.isSuitableLiteralType) underlying.widen else deconst
18471854
override def safeToString: String =
18481855
underlying.widen.toString + "(" + value.escapedStringValue + ")"
18491856
override def kind = "ConstantType"

0 commit comments

Comments
 (0)