Skip to content

Commit 76130f5

Browse files
committed
Fix calculation to drop transparent classes
Two fixes: 1. Don't forget about refinements 2. Don't dealias Fixes #16342 The first fix is essential for $16342. The second fix is just to keep types tidy and not open aliases needlessly. The previous incorrect version hid errors in previous regressions #15365 and #16311 which will need to be re-opened now.
1 parent 970d119 commit 76130f5

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,10 @@ trait ConstraintHandling {
557557
end approximation
558558

559559
private def isTransparent(tp: Type)(using Context): Boolean = tp match
560-
case AndType(tp1, tp2) => isTransparent(tp1) && isTransparent(tp2)
561-
case _ => tp.typeSymbol.isTransparentClass && !tp.isLambdaSub
560+
case AndType(tp1, tp2) =>
561+
isTransparent(tp1) && isTransparent(tp2)
562+
case _ =>
563+
tp.underlyingClassRef(refinementOK = false).typeSymbol.isTransparentClass
562564

563565
/** If `tp` is an intersection such that some operands are transparent trait instances
564566
* and others are not, replace as many transparent trait instances as possible with Any
@@ -572,18 +574,17 @@ trait ConstraintHandling {
572574
var dropped: List[Type] = List() // the types dropped so far, last one on top
573575

574576
def dropOneTransparentClass(tp: Type): Type =
575-
val tpd = tp.dealias
576-
if isTransparent(tpd) && !kept.contains(tpd) then
577-
dropped = tpd :: dropped
577+
if isTransparent(tp) && !kept.contains(tp) then
578+
dropped = tp :: dropped
578579
defn.AnyType
579-
else tpd match
580+
else tp match
580581
case AndType(tp1, tp2) =>
581582
val tp1w = dropOneTransparentClass(tp1)
582583
if tp1w ne tp1 then tp1w & tp2
583584
else
584585
val tp2w = dropOneTransparentClass(tp2)
585586
if tp2w ne tp2 then tp1 & tp2w
586-
else tpd
587+
else tp
587588
case _ =>
588589
tp
589590

File renamed without changes.
File renamed without changes.

tests/pos/i16342.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type Opaque = Base with Tag
2+
3+
type Base = Any {
4+
type Hack
5+
}
6+
7+
trait Tag
8+
9+
object Opaque {
10+
def apply(value: String): Opaque = value.asInstanceOf[Opaque]
11+
12+
def unapply(userId: Opaque): Option[String] = Option(userId).map(_.value)
13+
def unappy2(userId: Base with Tag): Option[String] = Option(userId).map(_.value)
14+
}
15+
16+
final implicit class Ops(private val userId: Opaque) extends AnyVal {
17+
def value: String = userId.asInstanceOf[String]
18+
}

0 commit comments

Comments
 (0)