Skip to content

Commit 01eab3b

Browse files
committed
fix: make the pattern matcher understand this types
1 parent ccde55e commit 01eab3b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,13 @@ object PatternMatcher {
482482
onSuccess
483483
)
484484
}
485-
case WildcardPattern() =>
485+
// When match against a `this.type` (say case a: this.type => ???),
486+
// the typer will transform the pattern to a `Bind(..., Typed(Ident(a), ThisType(...)))`,
487+
// then post typer will change all the `Ident` with a `ThisType` to a `This`.
488+
// Therefore, after pattern matching, we will have the following tree `Bind(..., Typed(This(...), ThisType(...)))`.
489+
// We handle now here the case were the pattern was transformed to a `This`, relying on the fact that the logic for
490+
// `Typed` above will create the correct type test.
491+
case WildcardPattern() | This(_) =>
486492
onSuccess
487493
case SeqLiteral(pats, _) =>
488494
matchElemsPlan(scrutinee, pats, exact = true, onSuccess)

tests/run/i23875.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
true
2+
false
3+
false

tests/run/i23875.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Foo {
2+
override def equals(that : Any) = that match {
3+
case _: this.type => true
4+
case _ => false
5+
}
6+
}
7+
8+
@main def Test =
9+
println(Foo.equals(Foo))
10+
println(Foo.equals(new AnyRef {}))
11+
println(Foo.equals(0))

0 commit comments

Comments
 (0)