Project

General

Profile

Actions

Bug #21138

closed

The modifier expression with "it" is parsed differently in parse.y and Prism, which is unexpected in both.

Added by kinoppyd (Yasuhiro Kinoshita) 9 days ago. Updated 8 days ago.

Status:
Closed
Assignee:
Target version:
-
ruby -v:
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) [arm64-darwin21]
[ruby-dev:<unknown>]

Description

Actual

parse.y

In parse.y, the block parameter "it" behaves as if it is not assigned.

# parse.y
loop.rb:1:in 'block in <main>': undefined method '<' for nil (NoMethodError)

1.then { it *= 10 while it < 1000 }
                           ^
	from <internal:kernel>:126:in 'Kernel#then'
	from loop.rb:1:in '<main>'

prism

In Prism, I may not understand what's happening due to my limited knowledge, but it results in a syntax error."

unexpected integer; expected an expression after the operator
unexpected integer, expecting end-of-input

> 1  1.then { it *= 10 while it < 1000 }

loop.rb:1: syntax errors found (SyntaxError)
> 1 | 1.then { it *= 10 while it < 1000 }
    |             ^~ unexpected integer; expected an expression after the operator
    |                ^~ unexpected integer, expecting end-of-input

Expected

'It' should be reassigned until it is greater than 1000.


Related issues 2 (1 open1 closed)

Related to Ruby master - Bug #21139: Prism and parse.y parses `it = it` differentlyAssignedprismActions
Is duplicate of Ruby master - Bug #21137: Compound assignment operator with "it" parsed differently between parse.y and prismClosedprismActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) 8 days ago

  • Assignee set to prism

it is interpreted as an ordinary local variable, if there is the assignment, including operator-assign.

The shortened example is:

1.then { it = 1 }

The block should be called once and assign 1 to a local variable it.
This works as expected with both of parse.y and prism.

However, with the op-assign:

1.then { it *= 1 }

parse.y parses the example as well as the previous example
(but cannot run it is not assigned and is nil, and raises NoMethodError on NilClass#*).

Prism seems to parse the simple assignment to it properly, but not op-assign.

Actions #2

Updated by nobu (Nobuyoshi Nakada) 8 days ago

  • Is duplicate of Bug #21137: Compound assignment operator with "it" parsed differently between parse.y and prism added
Actions #3

Updated by nobu (Nobuyoshi Nakada) 8 days ago ยท Edited

nobu (Nobuyoshi Nakada) wrote in #note-1:

parse.y parses the example as well as the previous example
(but cannot run it is not assigned and is nil, and raises NoMethodError on NilClass#*).

This error is what occurs from it < 1000 in your example.

Actions #4

Updated by kddnewton (Kevin Newton) 8 days ago

  • Status changed from Open to Closed

Applied in changeset git|ee181d1bb74ef82d6507c411a6aff10d1bf37aa3.


[ruby/prism] Fix up it indirect writes

Fixes [Bug #21137]

https://github.com/ruby/prism/commit/ca493e6797

Actions #5

Updated by nobu (Nobuyoshi Nakada) 8 days ago

  • Related to Bug #21139: Prism and parse.y parses `it = it` differently added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0