Skip to content

Check for mismatched argument type length in PatternMatcher due to Named Tuple :* syntax #23602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2025

Conversation

aherlihy
Copy link
Contributor

@aherlihy aherlihy commented Jul 25, 2025

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:

report.error(UnapplyInvalidNumberOfArguments(qual, argTypes), pos)

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:

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:

val sym :: syms1 = syms: @unchecked

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 :*.

@aherlihy aherlihy requested a review from EugeneFlesselle July 25, 2025 18:50
@WojciechMazur WojciechMazur added the backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. label Jul 29, 2025
@WojciechMazur WojciechMazur added this to the 3.7.3 milestone Jul 29, 2025
EugeneFlesselle added a commit to dotty-staging/dotty that referenced this pull request Aug 6, 2025
This makes the behaviour of auto-untupling consistent between the usual and
generic tuples.

Related to scala#23602
Copy link
Contributor

@EugeneFlesselle EugeneFlesselle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching the issue in the PatternMatcher is a bit unideal, though much better than crashing of course.

I spent a few hours trying to figure out where things go wrong in Applications, i.e. why the Typer outputs a tree without Tuple2.unapply and without error.
I didn't really reach anything, but commenting the following for reference.

As far as un-named generic tuples are concerned (tests/neg/i23155b.scala), I believe we can fix auto-untupling by modifying Applications.typedPatterns to also allow argType.isGenericTuple at:

&& defn.isTupleNType(argType) =>

However, I'm not sure I really understood the logic for auto-untupling named tuples.
It seems like it would become ambiguous pretty quickly, e.g.:

case class C(a: (a: Int, b: Int))
C(???) match
  case C((a = x, b = y)) => ??? // ok, no auto-untupling
  case C(x, y) => ??? // ok, auto-tupling due to length constraints
  case C(a = x) => ??? // presumably `a` is the outer-one and x has type (a: Int, b: Int)
  case C(b = y) => ??? // inner one?
  case C(a = x, b = y) => ??? // inner ones due to length constraints?

From an implementation perspective, the point from where named and unnamed tuples take a different code path appears to be in productUnapplySelectors as @aherlihy reported.
Specifically, we may be missing some named-tuple equivalent of the if isProductMatch(tp, args1.length, pos) guard from the unnamed case.

case Some(args1) if isProductMatch(tp, args1.length, pos) =>

@aherlihy aherlihy merged commit b13d617 into scala:main Aug 11, 2025
35 checks passed
@aherlihy aherlihy deleted the i23155 branch August 11, 2025 12:55
WojciechMazur added a commit that referenced this pull request Aug 12, 2025
… due to Named Tuple :* syntax" to 3.7.3 (#23717)

Backports #23602 to the 3.7.3-RC2.

PR submitted by the release tooling.
[skip ci]
@WojciechMazur WojciechMazur added backport:done This PR was successfully backported. and removed backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. labels Aug 12, 2025
aherlihy added a commit that referenced this pull request Aug 13, 2025
…#23706)

The crash reported in #23552 was
fixed by #23602, adding the issue as
a test case. Will now fail with "Wrong number of argument patterns".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:done This PR was successfully backported.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler crash using Tuple :* syntax for a NamedTuple in unapply
3 participants