summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2024-09-13 12:47:33 -0400
committerKevin Newton <[email protected]>2024-09-13 14:09:02 -0400
commit77521afac1687602fde4046ab20f016c61089003 (patch)
treeab446cef387648ab6742eb89a1288187f338bfd9
parent9afc6a981deae6e23d938cf5c2c4baadfeaafdb1 (diff)
[PRISM] Do not warn ambiguous ampersand when symbol literal
Fixes [Bug #20735]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11622
-rw-r--r--prism/prism.c11
-rw-r--r--test/prism/result/warnings_test.rb9
2 files changed, 19 insertions, 1 deletions
diff --git a/prism/prism.c b/prism/prism.c
index c1ef8ed3d4..6d4d2f8199 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -11277,7 +11277,16 @@ parser_lex(pm_parser_t *parser) {
pm_token_type_t type = PM_TOKEN_AMPERSAND;
if (lex_state_spcarg_p(parser, space_seen)) {
- pm_parser_warn_token(parser, &parser->current, PM_WARN_AMBIGUOUS_PREFIX_AMPERSAND);
+ if ((peek(parser) != ':') || (peek_offset(parser, 1) == '\0')) {
+ pm_parser_warn_token(parser, &parser->current, PM_WARN_AMBIGUOUS_PREFIX_AMPERSAND);
+ } else {
+ const uint8_t delim = peek_offset(parser, 1);
+
+ if ((delim != '\'') && (delim != '"') && !char_is_identifier(parser, parser->current.end + 1)) {
+ 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/test/prism/result/warnings_test.rb b/test/prism/result/warnings_test.rb
index fa87295898..5cff2d2d2b 100644
--- a/test/prism/result/warnings_test.rb
+++ b/test/prism/result/warnings_test.rb
@@ -22,6 +22,15 @@ module Prism
assert_warning("a /b/", "wrap regexp in parentheses")
end
+ def test_ambiguous_ampersand
+ assert_warning("a &b", "argument prefix")
+ assert_warning("a &:+", "argument prefix")
+
+ refute_warning("a &:b")
+ refute_warning("a &:'b'")
+ refute_warning("a &:\"b\"")
+ end
+
def test_binary_operator
[
[:**, "argument prefix"],