Skip to content

Commit d539605

Browse files
Do not show deprecation warning for _ in type match case (#18887)
Fixes #18808
2 parents b6b53b5 + 6d7aa99 commit d539605

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

Diff for: compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+10-2
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,14 @@ object Parsers {
413413
finally inEnum = saved
414414
}
415415

416+
private var inTypeMatchPattern = false
417+
private def withinTypeMatchPattern[T](body: => T): T = {
418+
val saved = inTypeMatchPattern
419+
inTypeMatchPattern = true
420+
try body
421+
finally inTypeMatchPattern = saved
422+
}
423+
416424
private var staged = StageKind.None
417425
def withinStaged[T](kind: StageKind)(op: => T): T = {
418426
val saved = staged
@@ -1862,7 +1870,7 @@ object Parsers {
18621870
val start = in.skipToken()
18631871
Ident(tpnme.USCOREkw).withSpan(Span(start, in.lastOffset, start))
18641872
else
1865-
if sourceVersion.isAtLeast(future) then
1873+
if !inTypeMatchPattern && sourceVersion.isAtLeast(future) then
18661874
deprecationWarning(em"`_` is deprecated for wildcard arguments of types: use `?` instead")
18671875
patch(source, Span(in.offset, in.offset + 1), "?")
18681876
val start = in.skipToken()
@@ -2898,7 +2906,7 @@ object Parsers {
28982906
val start = in.skipToken()
28992907
Ident(tpnme.WILDCARD).withSpan(Span(start, in.lastOffset, start))
29002908
case _ =>
2901-
rejectWildcardType(infixType())
2909+
withinTypeMatchPattern(rejectWildcardType(infixType()))
29022910
}
29032911
}
29042912
CaseDef(pat, EmptyTree, atSpan(accept(ARROW)) {

Diff for: compiler/test-resources/repl/i13208.scala

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
//> using options -source:future -deprecation
22
scala> type M[X] = X match { case Int => String case _ => Int }
33
scala> type N[X] = X match { case List[_] => Int }
4-
1 warning found
5-
-- Deprecation Warning: --------------------------------------------------------
6-
1 | type N[X] = X match { case List[_] => Int }
7-
| ^
8-
| `_` is deprecated for wildcard arguments of types: use `?` instead

Diff for: tests/pos/i18808.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using options -Werror
2+
3+
import language.future
4+
5+
type F[X] = X match
6+
case List[_] => Int
7+
8+
type G[X] = X match
9+
case List[?] => Int

0 commit comments

Comments
 (0)