Skip to content

Prism accepts double wildcard with no required fields in pattern match #2915

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

Closed
herwinw opened this issue Jun 28, 2024 · 2 comments · Fixed by #2918
Closed

Prism accepts double wildcard with no required fields in pattern match #2915

herwinw opened this issue Jun 28, 2024 · 2 comments · Fixed by #2918

Comments

@herwinw
Copy link
Member

herwinw commented Jun 28, 2024

Slightly related to #2914

Prism parser the following code:

[1,2,3,4] => *a, *b

This results in a FindPatternNode with an empty list requireds.

MRI's parser rejects this syntax (I've tried this with Ruby 3.3.3 and a 3.4.0-dev of today):

ruby -e '[1,2,3,4] => *a, *b; p a; p b'
-e:1: syntax error, unexpected *
[1,2,3,4] => *a, *b; p a; p b
ruby: compile error (SyntaxError)

The behaviour is ambiguous: if we enable the prism parser in MRI, it turns out that the whole array is put into the right splat:

ruby --parser=prism -e '[1,2,3,4] => *a, *b; p a; p b'
ruby: warning: The compiler based on the Prism parser is currently experimental and compatibility with the compiler based on parse.y is not yet complete. Please report any issues you find on the `ruby/prism` issue tracker.
[]
[1, 2, 3, 4]

But the result a = [1, 2, 3, 4]; b = [] would have been just as valid. Other combinations like a = [1]; b = [2, 3, 4] would be valid too, but kind of random.

I would guess a FindPatternNode with no requireds should be a syntax error.

@herwinw
Copy link
Member Author

herwinw commented Jun 28, 2024

The same thing can happen with an ArrayPatternNode:

[1,2,3,4] => *a, *b, 4

This too gets rejected by MRI

ruby -e '[1,2,3,4] => *a, *b, 4'
-e:1: syntax error, unexpected *
[1,2,3,4] => *a, *b, 4
ruby: compile error (SyntaxError)

Switching the parser to Prism (ruby 3.4-dev, the default included prism 0.30) results in a segfault.

In this case, MRI even rejects syntax that I would think is not ambiguous:

ruby -e '[1,2,3,4] => *a, 2, *b, 4'
-e:1: syntax error, unexpected ',', expecting end-of-input
[1,2,3,4] => *a, 2, *b, 4
ruby: compile error (SyntaxError)

This still results in a segfault when using --parser=prism

@kddnewton
Copy link
Collaborator

Thanks @herwinw! I'll get these fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants