From 72a613bc6a7ac389e359fa9f0926ebfe395a9390 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 19 Mar 2024 01:56:30 +0900 Subject: [PATCH] [ruby/prism] Fix a diagnostic incompatibility for `Prism::Translation::Parser` This PR fixes a diagnostic incompatibility for `Prism::Translation::Parser` when using constant argument: ```ruby def foo(A) end ``` ## Parser gem (Expected) Displays `formal argument cannot be a constant (Parser::SyntaxError)`: ```console $ bundle exec ruby -Ilib -rparser/ruby33 -ve 'p Parser::Ruby33.parse("def foo(A) end")' ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] (string):1:9: error: formal argument cannot be a constant (string):1: def foo(A) end (string):1: ^ /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/diagnostic/engine.rb:72: in `process': formal argument cannot be a constant (Parser::SyntaxError) from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:274:in `diagnostic' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/ruby33.rb:12177:in `_reduce_663' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/racc-1.7.3/lib/racc/parser.rb:267:in `_racc_do_parse_c' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/racc-1.7.3/lib/racc/parser.rb:267:in `do_parse' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:190:in `parse' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:33:in `parse' from -e:1:in `
' ``` ## `Prism::Translation::Parser` (Actual) Previously, the error messages displayed by the Parser gem were different. Before: ```console $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'Prism::Translation::Parser33.parse("def foo(A) end")' ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] (string):1:9: error: (string):1: def foo(A) end (string):1: ^ /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/diagnostic/engine.rb:72: in `process': Parser::SyntaxError (Parser::SyntaxError) from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:218:in `block in unwrap' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:216:in `each' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:216:in `unwrap' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:49:in `parse' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:33:in `parse' from -e:1:in `
' ``` After: ```console $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'Prism::Translation::Parser33.parse("def foo(A) end")' ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] (string):1:9: error: formal argument cannot be a constant (string):1: def foo(A) end (string):1: ^ /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/diagnostic/engine.rb:72: in `process': formal argument cannot be a constant (Parser::SyntaxError) from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:218:in `block in unwrap' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:216:in `each' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:216:in `unwrap' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:49:in `parse' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:33:in `parse' from -e:1:in `
' ``` https://github.com/ruby/prism/commit/4f2af88520 --- lib/prism/translation/parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/prism/translation/parser.rb b/lib/prism/translation/parser.rb index 6a2a9bbf24..48a198504b 100644 --- a/lib/prism/translation/parser.rb +++ b/lib/prism/translation/parser.rb @@ -124,7 +124,7 @@ module Prism when :argument_block_multi Diagnostic.new(:error, :block_and_blockarg, {}, diagnostic_location, []) when :argument_formal_constant - Diagnostic.new(:error, :formal_argument, {}, diagnostic_location, []) + Diagnostic.new(:error, :argument_const, {}, diagnostic_location, []) when :argument_formal_class Diagnostic.new(:error, :argument_cvar, {}, diagnostic_location, []) when :argument_formal_global