Skip to content

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

Closed

Description

@herwinw

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.

Activity

  1. herwinw commented on Jun 28, 2024

    @herwinw
    MemberAuthor

    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

  2. kddnewton commented on Jun 28, 2024

    @kddnewton
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions