Skip to content

Commit d0af55c

Browse files
committed
SI-7895 Avoid cascade of "symbol not found" in pattern matches
If we can't type check the `Foo` in `case Foo(a, b) => (a, b)`, we should enter error symbols for `a` and `b` to avoid further errors being reported in the case body.
1 parent acd7780 commit d0af55c

File tree

11 files changed

+28
-35
lines changed

11 files changed

+28
-35
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,9 +2392,16 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
23922392
if (pat1.tpe.paramSectionCount > 0)
23932393
pat1 modifyType (_.finalResultType)
23942394

2395-
for (bind @ Bind(name, _) <- cdef.pat)
2396-
if (name.toTermName != nme.WILDCARD && bind.symbol != null && bind.symbol != NoSymbol)
2397-
namer.enterIfNotThere(bind.symbol)
2395+
for (bind @ Bind(name, _) <- cdef.pat) {
2396+
val sym = bind.symbol
2397+
if (name.toTermName != nme.WILDCARD && sym != null) {
2398+
if (sym == NoSymbol) {
2399+
if (context.scope.lookup(name) == NoSymbol)
2400+
namer.enterInScope(context.owner.newErrorSymbol(name))
2401+
} else
2402+
namer.enterIfNotThere(sym)
2403+
}
2404+
}
23982405

23992406
val guard1: Tree = if (cdef.guard == EmptyTree) EmptyTree
24002407
else typed(cdef.guard, BooleanTpe)

test/files/neg/t0418.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
t0418.scala:2: error: not found: value Foo12340771
22
null match { case Foo12340771.Bar(x) => x }
33
^
4-
t0418.scala:2: error: not found: value x
5-
null match { case Foo12340771.Bar(x) => x }
6-
^
7-
two errors found
4+
one error found

test/files/neg/t418.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
t418.scala:2: error: not found: value Foo12340771
22
null match { case Foo12340771.Bar(x) => x }
33
^
4-
t418.scala:2: error: not found: value x
5-
null match { case Foo12340771.Bar(x) => x }
6-
^
7-
two errors found
4+
one error found

test/files/neg/t545.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
t545.scala:4: error: value blah is not a member of Test.Foo
22
val x = foo.blah match {
33
^
4-
t545.scala:5: error: recursive value x needs type
5-
case List(x) => x
6-
^
7-
two errors found
4+
one error found

test/files/neg/t5903a.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
Test_2.scala:4: error: wrong number of patterns for <$anon: AnyRef> offering (SomeTree.type, SomeTree.type): expected 2, found 3
22
case nq"$x + $y + $z" => println((x, y))
33
^
4-
Test_2.scala:4: error: not found: value x
5-
case nq"$x + $y + $z" => println((x, y))
6-
^
7-
two errors found
4+
one error found

test/files/neg/t5903b.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@ Test_2.scala:4: error: type mismatch;
33
required: String
44
case t"$x" => println(x)
55
^
6-
Test_2.scala:4: error: not found: value x
7-
case t"$x" => println(x)
8-
^
9-
two errors found
6+
one error found

test/files/neg/t5903c.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
Test_2.scala:4: error: String is not supported
22
case t"$x" => println(x)
33
^
4-
Test_2.scala:4: error: not found: value x
5-
case t"$x" => println(x)
6-
^
7-
two errors found
4+
one error found

test/files/neg/t5903d.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
Test_2.scala:4: error: extractor macros can only expand into extractor calls
22
case t"$x" => println(x)
33
^
4-
Test_2.scala:4: error: not found: value x
5-
case t"$x" => println(x)
6-
^
7-
two errors found
4+
one error found

test/files/neg/t7895.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
t7895.scala:4: error: not found: value Goop
2+
case Goop(a, b, c) => Tuple2(a, b)
3+
^
4+
one error found

test/files/neg/t7895.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class A {
2+
(null: Any) match {
3+
// We don't want "symbol not found errors" for `a` and `b` in the case body.
4+
case Goop(a, b, c) => Tuple2(a, b)
5+
}
6+
}

0 commit comments

Comments
 (0)