Skip to content

Binding to variable in alternation pattern is not a proper syntax error #3541

@Earlopain

Description

@Earlopain

Consider this code:

case foo
in {a: } | Array
  "matched: #{a}"
end

Ruby claims no syntax error:

$ ruby -cv test.rb
ruby 3.5.0dev (2025-04-10T10:21:51Z master 756479324f) +PRISM [x86_64-linux]
Syntax OK

But running that code is not possible:

$ ruby test.rb
test.rb:2: illegal variable in alternative pattern (a)
test.rb: compile error (SyntaxError)

That's because this syntax error is not part of prism itself but rather prism_compile in ruby/ruby:
https://github.com/ruby/ruby/blob/e6e698224697061728b309923109b34c8b720b09/prism_compile.c#L2982-L2995

It looks like this is the only syntax error of this kind, the others from COMPILE_ERROR look more like NOT_REACHED to me.

Is there a reason for handling it this way (maybe it's not trivial to check this in the parsing step)? In any case, it would be nice for this to be a "real" syntax error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions