Project

General

Profile

Actions

Feature #21572

open

Make illegal variable in alternation pattern a syntax error

Feature #21572: Make illegal variable in alternation pattern a syntax error

Added by kddnewton (Kevin Newton) about 1 month ago. Updated 1 day ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:123241]

Description

Currently when you have an illegal variable in an alternation pattern, you get a compile error, but no syntax error.

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

yields

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

but

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

Looking at the compiler, since illegal jumps (like break, next, etc.) are now syntax errors, the only other actual compiler errors are related to builtins. I think this should be a proper syntax error, so that editors and other tools can report on it and users can find it in development sooner.

Updated by Earlopain (Earlopain _) about 1 month ago · Edited Actions #1 [ruby-core:123259]

Thanks for opening this.

It seems just like a normal bug though, no? I understand that this doesn't deviate from what parse.y does but it is a syntax error and right now consumers of prism simply accept such code. I guess there are tests in ruby/ruby that very specifically assert against this somehow? Other than that, is seems like an implementation detail.

Updated by kddnewton (Kevin Newton) about 1 month ago Actions #2 [ruby-core:123283]

I agree, but there's precedence here of some things only being detectable at run-time, like illegal break usage. I just want to make sure this change goes through the proper channels to make sure everyone is onboard.

Updated by ktsj (Kazuki Tsujimoto) 3 days ago Actions #3 [ruby-core:123511]

I agree with you, @kddnewton (Kevin Newton).
Your proposal makes sense.

Updated by kddnewton (Kevin Newton) 2 days ago Actions #4 [ruby-core:123524]

Thank you @ktsj (Kazuki Tsujimoto) — I will start working on a patch for Prism.

Actions

Also available in: PDF Atom