Commit b13d617
authored
Check for mismatched argument type length in PatternMatcher due to Named Tuple :* syntax (#23602)
Fixes #23155
After discussing at the compiler meeting, it was decided that the Named
Tuple behavior should be the same as the Tuple behavior with respect to
pattern matching with the `:*` syntax. To error in the exact same way,
it should be caught here:
https://github.com/scala/scala3/blob/00d19dfd0906d2b4a8c076ded8af15cc3a6f2dc9/compiler/src/dotty/tools/dotc/typer/Applications.scala#L291
For regular tuples, `argTypes` is an `AppliedType` of `:*` and is not
yet reduced, so has length 1, so the arity check will fail. However, at
this point Named Tuple types are fully reduced by `tupleElementTypes`
here:
https://github.com/scala/scala3/blob/00d19dfd0906d2b4a8c076ded8af15cc3a6f2dc9/compiler/src/dotty/tools/dotc/typer/Applications.scala#L249
So for Named Tuples, `argTypes` at the time of the check will be `(Int,
Int)` so the arity check will pass, causing a later crash in
PatternMatcher here:
https://github.com/scala/scala3/blob/6146b90b8e32ac81d2703fccdebbc85e72cc5c5b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala#L270
We cannot un-reduce or fail to reduce for Named Tuple types because it
will cause a host of other tests to fail that rely on the fully reduced
Named Tuple-value types. For now I have added a check in PatternMatcher
because it’s better to fix the compiler crash ASAP, but I think we could
revisit if we want something like `Int :* Int :* EmptyTuple` to really
behave differently from `(Int, Int)` here, since pattern matching
currently works for both Tuples and Named Tuples using `(Int, Int)` or
`Tuple2[Int, Int]` , just not `:*`.File tree
3 files changed
+21
-2
lines changed- compiler/src/dotty/tools/dotc/transform
- tests/neg
3 files changed
+21
-2
lines changedLines changed: 8 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
270 | | - | |
271 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
272 | 278 | | |
273 | 279 | | |
274 | 280 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
0 commit comments