Skip to content

Dealias an applied match alias #19854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,12 @@ object Types extends TypeUtils {
private def dealias1(keep: AnnotatedType => Context ?=> Boolean, keepOpaques: Boolean)(using Context): Type = this match {
case tp: TypeRef =>
if (tp.symbol.isClass) tp
else if keepOpaques && tp.symbol.is(Opaque) then tp
else tp.info match {
case TypeAlias(alias) if !(keepOpaques && tp.symbol.is(Opaque)) =>
case TypeAlias(alias) =>
alias.dealias1(keep, keepOpaques)
case MatchAlias(alias: AppliedType) =>
(alias: Type).dealias1(keep, keepOpaques)
case _ => tp
}
case app @ AppliedType(tycon, _) =>
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/inlines/Inlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -446,16 +446,13 @@ object Inlines:
evidence
}

def unrollTupleTypes(tpe: Type): Option[List[Type]] = tpe.dealias match
def unrollTupleTypes(tpe: Type): Option[List[Type]] = tpe.dealias.normalized match
case AppliedType(tycon, args) if defn.isTupleClass(tycon.typeSymbol) =>
Some(args)
case AppliedType(tycon, head :: tail :: Nil) if tycon.isRef(defn.PairClass) =>
unrollTupleTypes(tail).map(head :: _)
case tpe: TermRef if tpe.symbol == defn.EmptyTupleModule =>
Some(Nil)
case tpRef: TypeRef => tpRef.info match
case MatchAlias(alias) => unrollTupleTypes(alias.tryNormalize)
case _ => None
case _ =>
None

Expand Down
8 changes: 1 addition & 7 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,9 @@ trait ImplicitRunInfo:
case t: TypeLambda =>
for p <- t.paramRefs do partSeen += p
traverseChildren(t)
case t: MatchType =>
traverseChildren(t)
traverse(t.normalized)
case MatchType.InDisguise(mt)
if !t.isInstanceOf[LazyRef] // skip recursive applications (eg. Tuple.Map)
=>
traverse(mt)
case t =>
traverseChildren(t)
traverse(t.normalized)
catch case ex: Throwable => handleRecursive("collectParts of", t.show, ex)

def apply(tp: Type): collection.Set[Type] =
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i17395-spec.ordered.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trait ThingWithPart { type Part }
type PartField[A] = ThingWithPart { type Part = A }
type ExtractPart[B] = B match { case PartField[a] => a }

trait TC[C]
object TC:
def tcForOptionPart[D](implicit tc: TC[ExtractPart[D]]): TC[Option[ExtractPart[D]]] = new {}

class Value
object Value:
implicit val tcValue: TC[Value] = new {}

class ValuePartHolder extends ThingWithPart { type Part = Value }

class Test:
def t1: Unit =
val tc = TC.tcForOptionPart[ValuePartHolder]
23 changes: 23 additions & 0 deletions tests/pos/i19821.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type F[X] = X match { case String => Option[Int] }
type G[X] = X match { case Option[x] => Int }

trait T:
type S
val opt1: F[S]
val opt2: O1
type O1 = F[S]

class Test:
def test: Unit =
val t: T { type S = String } = ???

val i1: G[t.opt1.type] = ???
val j1: Int = i1

val i2: G[t.opt2.type] = ???
val j2: Int = i2 // was:
//[E007] Type Mismatch Error: tests/pos/i19821.scala:17:18 --------------------
// val j2: Int = i2
// ^^
// Found: (i2 : G[(t.bar : t.O)])
// Required: Int
23 changes: 23 additions & 0 deletions tests/pos/i19857.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
sealed trait DFTypeAny

sealed trait DFTuple[T <: NonEmptyTuple] extends DFTypeAny

sealed trait DFBit extends DFTypeAny

sealed trait DFValOf[T]

type Of[T] <: DFTypeAny = T match
case DFTypeAny => T & DFTypeAny
case Product => FromProduct[T]

type JUSTVAL[T] = DFValOf[Of[T]]

type FromProduct[T <: Product] <: DFTypeAny = T match
case NonEmptyTuple => DFTuple[Tuple.Map[T, JUSTVAL]]

trait Width2[T]

object Width2:
inline given [T]: Width2[T] = new Width2[T] {}

val x = summon[Width2[Of[(DFBit, DFBit)]]]