[ruby-core:120895] [Ruby master Bug#21117] Inconsistent behaviour between "_1" and "it" variables

Issue #21117 has been reported by radarek (Radosław Bułat). ---------------------------------------- Bug #21117: Inconsistent behaviour between "_1" and "it" variables https://bugs.ruby-lang.org/issues/21117 * Author: radarek (Radosław Bułat) * Status: Open * ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I believe variables `_1` and `it` should have consistent behaviour and the same as normal local variables. Here are inconsistencies: ```ruby # 1. Assigning new value [1, 2, 3].each { |v| v = v + 1; p v } # works as expected [1, 2, 3].each { it = it + 1; p it } # works as expected [1, 2, 3].each { _1 = _1 + 1; p _1 } # SyntaxError # 2. Using operators like += on them [1, 2, 3].each { |v| v += 1; p v } # works as expected [1, 2, 3].each { it += 1; p it } # SyntaxError but I expected it to work correctly after 1st point [1, 2, 3].each { _1 += 1; p _1 } # works, which is inconsistent with 1 point # however, this one does not work [1, 2, 3].map { _1 += 1 } # runtime error is raised: undefined method '+' for nil (NoMethodError) ``` If both `_1` and `it` are advertised as block local variables then I would expect that overwriting works correctly, both using expressions like `_1 = _1 + 1` and `_1 *= 2`. -- https://bugs.ruby-lang.org/

Issue #21117 has been updated by mame (Yusuke Endoh).
I believe variables `_1` and `it` should have consistent behaviour
Assignments to `it` are currently allowed for compatibility considerations, but I believe they may be prohibited in the future. BTW, `_1` and `it` are different. `_1` cannot be written both inside and outside the block, but `it` is intentionally allowed.
and the same as normal local variables.
No, in my opinion. They are special parameters and should not be considered as just normal local variables. #20965 #21049 ---------------------------------------- Bug #21117: Inconsistent behaviour between "_1" and "it" variables https://bugs.ruby-lang.org/issues/21117#change-111788 * Author: radarek (Radosław Bułat) * Status: Open * ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I believe variables `_1` and `it` should have consistent behaviour and the same as normal local variables. Here are inconsistencies: ```ruby # 1. Assigning new value [1, 2, 3].each { |v| v = v + 1; p v } # works as expected [1, 2, 3].each { it = it + 1; p it } # works as expected [1, 2, 3].each { _1 = _1 + 1; p _1 } # SyntaxError # 2. Using operators like += on them [1, 2, 3].each { |v| v += 1; p v } # works as expected [1, 2, 3].each { it += 1; p it } # SyntaxError but I expected it to work correctly after 1st point [1, 2, 3].each { _1 += 1; p _1 } # works, which is inconsistent with 1 point # however, this one does not work [1, 2, 3].map { _1 += 1 } # runtime error is raised: undefined method '+' for nil (NoMethodError) ``` If both `_1` and `it` are advertised as block local variables then I would expect that overwriting works correctly, both using expressions like `_1 = _1 + 1` and `_1 *= 2`. -- https://bugs.ruby-lang.org/

Issue #21117 has been updated by tompng (tomoya ishida). Prism and parse.y parses these two code in the example differently. ~~~ruby [1, 2, 3].each { it = it + 1; p it } [1, 2, 3].each { it += 1; p it } ~~~ I think `it` was designed not to break old code that uses local variable `it`, so maybe it should be parsed as same as the code below, just like parse.y does. ~~~ruby [1, 2, 3].each { it2 = it2 + 1; p it2 } [1, 2, 3].each { it2 += 1; p it2 } ~~~ ---------------------------------------- Bug #21117: Inconsistent behaviour between "_1" and "it" variables https://bugs.ruby-lang.org/issues/21117#change-111795 * Author: radarek (Radosław Bułat) * Status: Open * ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I believe variables `_1` and `it` should have consistent behaviour and the same as normal local variables. Here are inconsistencies: ```ruby # 1. Assigning new value [1, 2, 3].each { |v| v = v + 1; p v } # works as expected [1, 2, 3].each { it = it + 1; p it } # works as expected [1, 2, 3].each { _1 = _1 + 1; p _1 } # SyntaxError # 2. Using operators like += on them [1, 2, 3].each { |v| v += 1; p v } # works as expected [1, 2, 3].each { it += 1; p it } # SyntaxError but I expected it to work correctly after 1st point [1, 2, 3].each { _1 += 1; p _1 } # works, which is inconsistent with 1 point # however, this one does not work [1, 2, 3].map { _1 += 1 } # runtime error is raised: undefined method '+' for nil (NoMethodError) ``` If both `_1` and `it` are advertised as block local variables then I would expect that overwriting works correctly, both using expressions like `_1 = _1 + 1` and `_1 *= 2`. -- https://bugs.ruby-lang.org/

Issue #21117 has been updated by tompng (tomoya ishida). Status changed from Open to Assigned Assignee set to prism These two should be SyntaxError, and is actually SyntaxError with --parser=parse.y ~~~ruby [1, 2, 3].each { _1 += 1; p _1 } [1, 2, 3].map { _1 += 1 } ~~~ Some difference between `it` and `_1` is by design, but the inconsistencies raised in this issue are caused by difference between parse.y and Prism and should be fixed. ---------------------------------------- Bug #21117: Inconsistent behaviour between "_1" and "it" variables https://bugs.ruby-lang.org/issues/21117#change-111890 * Author: radarek (Radosław Bułat) * Status: Assigned * Assignee: prism * ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I believe variables `_1` and `it` should have consistent behaviour and the same as normal local variables. Here are inconsistencies: ```ruby # 1. Assigning new value [1, 2, 3].each { |v| v = v + 1; p v } # works as expected [1, 2, 3].each { it = it + 1; p it } # works as expected [1, 2, 3].each { _1 = _1 + 1; p _1 } # SyntaxError # 2. Using operators like += on them [1, 2, 3].each { |v| v += 1; p v } # works as expected [1, 2, 3].each { it += 1; p it } # SyntaxError but I expected it to work correctly after 1st point [1, 2, 3].each { _1 += 1; p _1 } # works, which is inconsistent with 1 point # however, this one does not work [1, 2, 3].map { _1 += 1 } # runtime error is raised: undefined method '+' for nil (NoMethodError) ``` If both `_1` and `it` are advertised as block local variables then I would expect that overwriting works correctly, both using expressions like `_1 = _1 + 1` and `_1 *= 2`. -- https://bugs.ruby-lang.org/

Issue #21117 has been updated by k0kubun (Takashi Kokubun). Backport changed from 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: REQUIRED to 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: DONE ruby_3_4 commit:d3fc56dcfa7b408cc3b6788efad36fd8df3e55da merged revision(s) commit:127325a4bad409ee5da91084fac768934a8fd9e3. ---------------------------------------- Bug #21117: Inconsistent behaviour between "_1" and "it" variables https://bugs.ruby-lang.org/issues/21117#change-111941 * Author: radarek (Radosław Bułat) * Status: Closed * Assignee: prism * ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: DONE ---------------------------------------- I believe variables `_1` and `it` should have consistent behaviour and the same as normal local variables. Here are inconsistencies: ```ruby # 1. Assigning new value [1, 2, 3].each { |v| v = v + 1; p v } # works as expected [1, 2, 3].each { it = it + 1; p it } # works as expected [1, 2, 3].each { _1 = _1 + 1; p _1 } # SyntaxError # 2. Using operators like += on them [1, 2, 3].each { |v| v += 1; p v } # works as expected [1, 2, 3].each { it += 1; p it } # SyntaxError but I expected it to work correctly after 1st point [1, 2, 3].each { _1 += 1; p _1 } # works, which is inconsistent with 1 point # however, this one does not work [1, 2, 3].map { _1 += 1 } # runtime error is raised: undefined method '+' for nil (NoMethodError) ``` If both `_1` and `it` are advertised as block local variables then I would expect that overwriting works correctly, both using expressions like `_1 = _1 + 1` and `_1 *= 2`. -- https://bugs.ruby-lang.org/

Issue #21117 has been updated by nagachika (Tomoyuki Chikanaga). Backport changed from 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: DONE to 3.1: DONTNEED, 3.2: DONTNEED, 3.3: WONTFIX, 3.4: DONE I decided to mark "WONTFIX" for ruby_3_3. Introducing syntax error might cause unavoidable problem in real world applications. ---------------------------------------- Bug #21117: Inconsistent behaviour between "_1" and "it" variables https://bugs.ruby-lang.org/issues/21117#change-112489 * Author: radarek (Radosław Bułat) * Status: Closed * Assignee: prism * ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24] * Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: WONTFIX, 3.4: DONE ---------------------------------------- I believe variables `_1` and `it` should have consistent behaviour and the same as normal local variables. Here are inconsistencies: ```ruby # 1. Assigning new value [1, 2, 3].each { |v| v = v + 1; p v } # works as expected [1, 2, 3].each { it = it + 1; p it } # works as expected [1, 2, 3].each { _1 = _1 + 1; p _1 } # SyntaxError # 2. Using operators like += on them [1, 2, 3].each { |v| v += 1; p v } # works as expected [1, 2, 3].each { it += 1; p it } # SyntaxError but I expected it to work correctly after 1st point [1, 2, 3].each { _1 += 1; p _1 } # works, which is inconsistent with 1 point # however, this one does not work [1, 2, 3].map { _1 += 1 } # runtime error is raised: undefined method '+' for nil (NoMethodError) ``` If both `_1` and `it` are advertised as block local variables then I would expect that overwriting works correctly, both using expressions like `_1 = _1 + 1` and `_1 *= 2`. -- https://bugs.ruby-lang.org/
participants (5)
-
k0kubun (Takashi Kokubun)
-
mame (Yusuke Endoh)
-
nagachika (Tomoyuki Chikanaga)
-
radarek
-
tompng (tomoya ishida)