Skip to content

Commit abb6909

Browse files
Normalize when verifying if TypeTestCasts are unchecked
1 parent 05114dd commit abb6909

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ object TypeTestsCasts {
135135
def recur(X: Type, P: Type): String = trace(s"recur(${X.show}, ${P.show})") {
136136
(X <:< P) ||| P.dealias.match
137137
case _: SingletonType => ""
138+
case tp if tp.isMatchAlias => recur(X, tp.tryNormalize)
138139
case _: TypeProxy
139140
if isAbstract(P) => i"it refers to an abstract type member or type parameter"
140141
case defn.ArrayOf(tpT) =>

tests/pos/i13433c/A_1.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.reflect.TypeTest
2+
3+
type Matcher[A] = A match { case String => A }
4+
5+
def patternMatch[A](a: Any)(using tt: TypeTest[Any, Matcher[A]]): Option[Matcher[A]] = {
6+
// type T = RDF.Triple[Rdf]
7+
a match {
8+
case res: Matcher[A] => Some(res)
9+
case _ => None
10+
}
11+
}
12+
13+
def patternMatchWithAlias[A](a: Any)(using tt: TypeTest[Any, Matcher[A]]): Option[Matcher[A]] = {
14+
type T = Matcher[A]
15+
a match {
16+
case res: T => Some(res)
17+
case _ => None
18+
}
19+
}
20+
21+
type S = String
22+
type MS = Matcher[S]
23+
24+
type S2 = MS
25+
type MS2 = Matcher[S2]

tests/pos/i13433c/B_2.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//> using options -Xfatal-warnings -deprecation -feature
2+
3+
@main def main = {
4+
println(patternMatch[String]("abc"))
5+
println(patternMatchWithAlias[String]("abc"))
6+
println(patternMatch[String]("abc")(using (s: Any) => {
7+
if s.isInstanceOf[Matcher[String]] then Some[s.type & Matcher[String]](s.asInstanceOf[s.type & Matcher[String]]) else None }))
8+
println(patternMatchWithAlias[String]("abc")(using (s: Any) => {
9+
if s.isInstanceOf[Matcher[String]] then Some[s.type & Matcher[String]](s.asInstanceOf[s.type & Matcher[String]]) else None }))
10+
11+
println(patternMatch[String](1))
12+
println(patternMatchWithAlias[String](1))
13+
14+
println(patternMatch[String]("abc")(using (s: Any) => {
15+
if s.isInstanceOf[S] then Some[s.type & Matcher[String]](s.asInstanceOf[s.type & Matcher[String]]) else None}))
16+
println(patternMatch[String]("abc")(using (s: Any) => {
17+
if s.isInstanceOf[MS] then Some[s.type & Matcher[String]](s.asInstanceOf[s.type & Matcher[String]]) else None}))
18+
println(patternMatch[String]("abc")(using (s: Any) => {
19+
if s.isInstanceOf[S2] then Some[s.type & Matcher[String]](s.asInstanceOf[s.type & Matcher[String]]) else None}))
20+
println(patternMatch[String]("abc")(using (s: Any) => {
21+
if s.isInstanceOf[MS2] then Some[s.type & Matcher[String]](s.asInstanceOf[s.type & Matcher[String]]) else None}))
22+
}

0 commit comments

Comments
 (0)