summaryrefslogtreecommitdiff
path: root/prism
diff options
context:
space:
mode:
authorKoichi ITO <koic.ito@gmail.com>2024-03-13 10:46:10 +0900
committergit <svn-admin@ruby-lang.org>2024-03-13 12:38:45 +0000
commit5fa28ce015a595ba8042be58a951b2f2a83eef10 (patch)
tree88e26cfec87a46e599428968278a9737afe2bb2d /prism
parent824e3e6c3d2a172a7dbffbff0b29c0e25e022e13 (diff)
[ruby/prism] Warn `&` interpreted as argument prefix
This PR makes `Prism` warn `&` interpreted as argument prefix. This carries a similar meaning to the following Ruby warning: ```console $ ruby -cwe "foo &bar" -e:1: warning: `&' interpreted as argument prefix Syntax OK ``` Previously, it did not issue a warning: ```console $ bundle exec ruby -rprism -ve "p Prism.parse('foo &bar').warnings" ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] [] ``` From now on, it will issue a warning similar to Ruby's: ```console $ bundle exec ruby -rprism -ve "p Prism.parse('foo &bar').warnings" ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] [#<Prism::ParseWarning @type=:ambiguous_prefix_amp @message="ambiguous `&` has been interpreted as an argument prefix" @location=#<Prism::Location @start_offset=4 @length=1 start_line=1> @level=:verbose>] ``` https://github.com/ruby/prism/commit/312f99cd1e
Diffstat (limited to 'prism')
-rw-r--r--prism/config.yml1
-rw-r--r--prism/prism.c5
-rw-r--r--prism/templates/src/diagnostic.c.erb1
3 files changed, 6 insertions, 1 deletions
diff --git a/prism/config.yml b/prism/config.yml
index e395c5a8c3..f7dcc1f79f 100644
--- a/prism/config.yml
+++ b/prism/config.yml
@@ -233,6 +233,7 @@ errors:
warnings:
- AMBIGUOUS_FIRST_ARGUMENT_MINUS
- AMBIGUOUS_FIRST_ARGUMENT_PLUS
+ - AMBIGUOUS_PREFIX_AMPERSAND
- AMBIGUOUS_PREFIX_STAR
- AMBIGUOUS_PREFIX_STAR_STAR
- AMBIGUOUS_SLASH
diff --git a/prism/prism.c b/prism/prism.c
index 95ac872bb2..947d1a92bb 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -9912,7 +9912,10 @@ parser_lex(pm_parser_t *parser) {
}
pm_token_type_t type = PM_TOKEN_AMPERSAND;
- if (lex_state_spcarg_p(parser, space_seen) || lex_state_beg_p(parser)) {
+ if (lex_state_spcarg_p(parser, space_seen)) {
+ pm_parser_warn_token(parser, &parser->current, PM_WARN_AMBIGUOUS_PREFIX_AMPERSAND);
+ type = PM_TOKEN_UAMPERSAND;
+ } else if (lex_state_beg_p(parser)) {
type = PM_TOKEN_UAMPERSAND;
}
diff --git a/prism/templates/src/diagnostic.c.erb b/prism/templates/src/diagnostic.c.erb
index 8ca5cff3e8..5ba4d62317 100644
--- a/prism/templates/src/diagnostic.c.erb
+++ b/prism/templates/src/diagnostic.c.erb
@@ -313,6 +313,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
// Warnings
[PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_MINUS] = { "ambiguous first argument; put parentheses or a space even after `-` operator", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_PLUS] = { "ambiguous first argument; put parentheses or a space even after `+` operator", PM_WARNING_LEVEL_VERBOSE },
+ [PM_WARN_AMBIGUOUS_PREFIX_AMPERSAND] = { "ambiguous `&` has been interpreted as an argument prefix", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_PREFIX_STAR] = { "ambiguous `*` has been interpreted as an argument prefix", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_PREFIX_STAR_STAR] = { "ambiguous `**` has been interpreted as an argument prefix", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_SLASH] = { "ambiguous `/`; wrap regexp in parentheses or add a space after `/` operator", PM_WARNING_LEVEL_VERBOSE },