Skip to content

Commit 4b3652f

Browse files
committed
widen enum cases if not upper bound
1 parent 6706e27 commit 4b3652f

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,24 @@ trait ConstraintHandling {
350350
val tpw = tp.widenSingletons
351351
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
352352

353+
def widenEnum(tp: Type) =
354+
val tpw = tp.widenEnumCase
355+
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
356+
353357
def isSingleton(tp: Type): Boolean = tp match
354358
case WildcardType(optBounds) => optBounds.exists && isSingleton(optBounds.bounds.hi)
355359
case _ => isSubTypeWhenFrozen(tp, defn.SingletonType)
356360

361+
def isEnumCase(tp: Type): Boolean = tp match
362+
case WildcardType(optBounds) => optBounds.exists && isEnumCase(optBounds.bounds.hi)
363+
case _ => tp.classSymbol.isAllOf(EnumCase, butNot=JavaDefined)
364+
357365
val wideInst =
358366
if isSingleton(bound) then inst
359-
else dropSuperTraits(widenOr(widenSingle(inst)))
367+
else
368+
val lub = widenOr(widenSingle(inst))
369+
val asAdt = if isEnumCase(bound) then lub else widenEnum(lub)
370+
dropSuperTraits(asAdt)
360371
wideInst match
361372
case wideInst: TypeRef if wideInst.symbol.is(Module) =>
362373
TermRef(wideInst.prefix, wideInst.symbol.sourceModule)

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,11 @@ object Types {
11461146
case _ => this
11471147
}
11481148

1149+
/** if this type is a reference to a class case of an enum, replace it by its first parent */
1150+
final def widenEnumCase(using Context): Type = this match
1151+
case tp: (TypeRef | AppliedType) if tp.classSymbol.isAllOf(EnumCase) => tp.parents.head
1152+
case _ => this
1153+
11491154
/** Widen this type and if the result contains embedded union types, replace
11501155
* them by their joins.
11511156
* "Embedded" means: inside type lambdas, intersections or recursive types, or in prefixes of refined types.

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,8 +1399,8 @@ object Scanners {
13991399

14001400
object IndentWidth {
14011401
private inline val MaxCached = 40
1402-
private val spaces = Array.tabulate(MaxCached + 1)(new Run(' ', _))
1403-
private val tabs = Array.tabulate(MaxCached + 1)(new Run('\t', _))
1402+
private val spaces = Array.tabulate[Run](MaxCached + 1)(new Run(' ', _)) // TODO: remove new after bootstrap
1403+
private val tabs = Array.tabulate[Run](MaxCached + 1)(new Run('\t', _)) // TODO: remove new after bootstrap
14041404

14051405
def Run(ch: Char, n: Int): Run =
14061406
if (n <= MaxCached && ch == ' ') spaces(n)

0 commit comments

Comments
 (0)