Skip to content

Commit 1cd85bc

Browse files
committed
Address review comments
1 parent 3a352e6 commit 1cd85bc

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ trait ConstraintHandling[AbstractContext] {
300300
* (i.e. `inst.widenSingletons <:< bound` succeeds with satisfiable constraint)
301301
* 2. If `inst` is a union type, approximate the union type from above by an intersection
302302
* of all common base types, provided the result is a subtype of `bound`.
303-
* 3. (currently not enabled) If `inst` is an intersection with some protected base types, drop
304-
* the protected base types from the intersection, provided the result is a subtype of `bound`.
303+
* 3. (currently not enabled) If `inst` is an intersection with some restricted base types, drop
304+
* the restricted base types from the intersection, provided the result is a subtype of `bound`.
305305
*
306306
* Don't do these widenings if `bound` is a subtype of `scala.Singleton`.
307307
* Also, if the result of these widenings is a TypeRef to a module class,
@@ -313,18 +313,20 @@ trait ConstraintHandling[AbstractContext] {
313313
*/
314314
def widenInferred(inst: Type, bound: Type)(implicit actx: AbstractContext): Type =
315315

316-
def isProtected(tp: Type) = tp.typeSymbol == defn.EnumValueClass // for now, to be generalized later
316+
def isRestricted(tp: Type) = tp.typeSymbol == defn.EnumValueClass // for now, to be generalized later
317317

318-
def dropProtected(tp: Type): Type = tp.dealias match
319-
case tp @ AndType(tp1, tp2) =>
320-
if isProtected(tp1) then tp2
321-
else if isProtected(tp2) then tp1
322-
else tp.derivedAndType(dropProtected(tp1), dropProtected(tp2))
318+
def dropRestricted(tp: Type): Type = tp.dealias match
319+
case tpd @ AndType(tp1, tp2) =>
320+
if isRestricted(tp1) then tp2
321+
else if isRestricted(tp2) then tp1
322+
else
323+
val tpw = tpd.derivedAndType(dropRestricted(tp1), dropRestricted(tp2))
324+
if tpw ne tpd then tpw else tp
323325
case _ =>
324326
tp
325327

326-
def widenProtected(tp: Type) =
327-
val tpw = dropProtected(tp)
328+
def widenRestricted(tp: Type) =
329+
val tpw = dropRestricted(tp)
328330
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
329331

330332
def widenOr(tp: Type) =
@@ -341,8 +343,8 @@ trait ConstraintHandling[AbstractContext] {
341343

342344
val wideInst =
343345
if isSingleton(bound) then inst
344-
else /*widenProtected*/(widenOr(widenSingle(inst)))
345-
// widenProtected is currently not called since it's special cased in `dropEnumValue`
346+
else /*widenRestricted*/(widenOr(widenSingle(inst)))
347+
// widenRestricted is currently not called since it's special cased in `dropEnumValue`
346348
// in `Namer`. It's left in here in case we want to generalize the scheme to other
347349
// "protected inheritance" classes.
348350
wideInst match

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,10 +1443,12 @@ class Namer { typer: Typer =>
14431443

14441444
// Drop EnumValue parents from inferred types of enum constants
14451445
def dropEnumValue(tp: Type): Type = tp.dealias match
1446-
case tp @ AndType(tp1, tp2) =>
1446+
case tpd @ AndType(tp1, tp2) =>
14471447
if isEnumValue(tp1) then tp2
14481448
else if isEnumValue(tp2) then tp1
1449-
else tp.derivedAndType(dropEnumValue(tp1), dropEnumValue(tp2))
1449+
else
1450+
val tpw = tpd.derivedAndType(dropEnumValue(tp1), dropEnumValue(tp2))
1451+
if tpw ne tpd then tpw else tp
14501452
case _ =>
14511453
tp
14521454

0 commit comments

Comments
 (0)