Commit df35063c

df35063c58b7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Relocate &:symbol block-pass after a positional argument

m(a, &:sym) and m a, &:sym failed to parse: the textual &:symbol -> { |_spx| _spx.symbol } rewrite in spinel_parse.c only removed the enclosing parens for a sole argument, so after a positional arg the brace landed in argument position and Prism read it as a hash literal ("unexpected '{', expecting '}' or a key in the hash literal"). This was the front-door blocker for bundler (File.open(file, "r:UTF-8", &:read)) and minitest (define_method :mu_pp, &:pretty_inspect).

Prism parses &:sym natively, but Spinel only lowers the native BlockArgumentNode+SymbolNode shape for a few methods (inject / reduce / sort_by); general block-takers depend on this rewrite producing an explicit block. So fix the rewrite's placement instead of removing it: detect the char preceding &: (whitespace/newline aware) and relocate the block to where Ruby reads it as a block --

  • sole parenthesized arg m(&:s) -> m { |_spx| _spx.s }
  • after a positional, paren m(a, &:s) -> m(a) { |_spx| _spx.s }
  • after a positional, cmd m a, &:s -> m a do |_spx| _spx.s end
  • command-position sole arg m &:s -> m { |_spx| _spx.s } Operator symbols (&:+, name_len 0) stay on Prism's native path.

Known limitation (documented in-code): a paren-less command carrying a symbol-proc nested as an argument inside a paren call -- p(foo :a, &:s) -- mis-binds the block to the outer call, since textually its args sit at the same paren depth as the enclosing call. Neither target gem hits this; the robust fix is an AST-level lowering.

Closes #1359

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
df35063c58b7
Parent
2557203aec85

spinel_parse.cmodifiedView file

@@ -2048,32 +2048,95 @@
20482048 REWRITE_SEND_CALL(".public_send(\"", 14, 1);
20492049 continue;
20502050 }
2051- /* (&:symbol) → { |_spx| _spx.symbol } — also remove enclosing parens */
2051+ /* `&:symbol` symbol-to-proc. Prism parses `&:sym` natively, but
2052+ Spinel only lowers the native BlockArgumentNode+SymbolNode shape
2053+ for a few methods (inject / reduce / sort_by); general block
2054+ takers rely on this textual rewrite to an explicit block. The
2055+ block has to land where Ruby reads it as a block, not a hash:
2056+ - sole parenthesized arg `m(&:s)` -> `m { |_spx| _spx.s }`
2057+ - after a positional, paren call
2058+ `m(a, &:s)` -> `m(a) { |_spx| _spx.s }`
2059+ - after a positional, command call
2060+ `m a, &:s` -> `m a do |_spx| _spx.s end`
2061+ - command-position sole arg `m &:s` -> `m { |_spx| _spx.s }`
2062+ The bare `{ |_spx| ... }` was only correct in block position; a
2063+ `,`-prefixed brace parses as a hash literal, which was the bundler
2064+ (`File.open(f, "r:UTF-8", &:read)`) / minitest
2065+ (`define_method :mu_pp, &:pretty_inspect`) parse failure.
2066+
2067+ Operator symbols (`&:+`) have name_len 0 here and are left for
2068+ Prism's native path (the arith reduce/inject lowering handles
2069+ them). Known limitation: a paren-less command carrying a
2070+ symbol-proc that is itself nested as an argument inside a paren
2071+ call -- `p(foo :a, &:s)` -- mis-binds the block to the outer
2072+ call, because textually its args sit at the same paren depth as
2073+ the enclosing call (indistinguishable without re-implementing
2074+ Ruby's command/arg disambiguation). Neither target gem hits
2075+ this; a robust fix is an AST-level lowering, deferred. */
20522076 if (i + 2 < len && source[i] == '&' && source[i + 1] == ':') {
2053- /* Check if preceded by ( and remove it */
2054- int had_paren = 0;
2055- if (oi > 0 && out[oi - 1] == '(') { oi--; had_paren = 1; }
2077+ /* Last emitted non-space char. Skip newlines too so a multi-line
2078+ `m(a,\n &:s)` still sees the comma. */
2079+ size_t back = oi;
2080+ while (back > 0 && (out[back - 1] == ' ' || out[back - 1] == '\t' ||
2081+ out[back - 1] == '\n' || out[back - 1] == '\r')) back--;
2082+ char prev = (back > 0) ? out[back - 1] : '\0';
20562083 i += 2;
20572084 size_t ns = i;
20582085 while (i < len && (source[i] == '_' || (source[i] >= 'a' && source[i] <= 'z') ||
20592086 (source[i] >= 'A' && source[i] <= 'Z') || (source[i] >= '0' && source[i] <= '9') ||
20602087 source[i] == '?' || source[i] == '!')) i++;
20612088 size_t name_len = i - ns;
2062- if (name_len > 0) {
2063- /* Issue #792: skip any whitespace between the symbol name
2064- and the closing paren so `(&:bar )` matches the same way
2065- `(&:bar)` does. */
2066- if (had_paren) {
2067- while (i < len && (source[i] == ' ' || source[i] == '\t')) i++;
2068- if (i < len && source[i] == ')') i++;
2069- }
2089+ if (name_len == 0) {
2090+ /* Operator / empty symbol: leave `&:` for Prism's native path. */
2091+ OUT_STR("&:");
2092+ continue;
2093+ }
2094+ if (prev == '(') {
2095+ /* Sole parenthesized arg: drop the `(` (and any trailing ws)
2096+ and the matching `)` so the block binds paren-less. #792:
2097+ tolerate whitespace before the `)`. Restore `i` if no `)`
2098+ follows so trailing content isn't swallowed. */
2099+ oi = back - 1;
2100+ size_t after_sym = i;
2101+ while (i < len && (source[i] == ' ' || source[i] == '\t' ||
2102+ source[i] == '\n' || source[i] == '\r')) i++;
2103+ if (i < len && source[i] == ')') i++;
2104+ else i = after_sym;
20702105 OUT_STR(" { |_spx| _spx.");
2071- size_t k; for (k = 0; k < name_len; k++) OUT_CHAR(source[ns + k]);
2106+ { size_t k; for (k = 0; k < name_len; k++) OUT_CHAR(source[ns + k]); }
20722107 OUT_STR(" }");
20732108 continue;
20742109 }
2075- if (had_paren) OUT_CHAR('('); /* restore if failed */
2076- OUT_STR("&:");
2110+ if (prev == ',') {
2111+ /* After a positional arg. Drop the separating comma and
2112+ relocate the block: a parenthesized call closes first then
2113+ takes a brace block; a command call takes a trailing do..end
2114+ (a brace would still read as a hash). Only consume up to the
2115+ `)`; otherwise restore `i` so the newline/`end` that follows
2116+ a paren-less command is preserved. */
2117+ oi = back - 1;
2118+ size_t after_sym = i;
2119+ while (i < len && (source[i] == ' ' || source[i] == '\t' ||
2120+ source[i] == '\n' || source[i] == '\r')) i++;
2121+ if (i < len && source[i] == ')') {
2122+ i++;
2123+ OUT_CHAR(')');
2124+ OUT_STR(" { |_spx| _spx.");
2125+ { size_t k; for (k = 0; k < name_len; k++) OUT_CHAR(source[ns + k]); }
2126+ OUT_STR(" }");
2127+ } else {
2128+ i = after_sym;
2129+ OUT_STR(" do |_spx| _spx.");
2130+ { size_t k; for (k = 0; k < name_len; k++) OUT_CHAR(source[ns + k]); }
2131+ OUT_STR(" end");
2132+ }
2133+ continue;
2134+ }
2135+ /* Command-position sole arg (`m &:s`): the brace binds as a
2136+ block (no comma precedes it). */
2137+ OUT_STR(" { |_spx| _spx.");
2138+ { size_t k; for (k = 0; k < name_len; k++) OUT_CHAR(source[ns + k]); }
2139+ OUT_STR(" }");
20772140 continue;
20782141 }
20792142 OUT_CHAR(source[i]);