From: "alanwu (Alan Wu) via ruby-core" Date: 2024-12-20T18:16:54+00:00 Subject: [ruby-core:120348] [Ruby master Bug#20970] `it /1/i` raises undefined method 'it' for main (NoMethodError) even if binding.local_variables includes `it` Issue #20970 has been updated by alanwu (Alan Wu). Assignee deleted (prism) Sorry, I was too quick to blame Prism. I agree that having `it` as a local brings hidden semantic complexity that warrent discussion. Whether an identifier is a local influences parsing, so it's important to know exactly when `it` becomes a local to the parser. Usually, assignments make locals, but the way parse.y has it currently, `it` becomes a local on read, so in the OP example the first `it` essentially does `it => it` (not `it = it` because that always sets nil) implicitly to influence lines below it. It's a sort of promotion-on-first-read, if you will, a brand new way to add a local in the grammar. It can be hard to parse code mentally and know that whether an `it` is reading a variable and promoting. To illustrate: ```ruby i=2 42.tap do p it /1/i rescue 0 # `it` treated as method call p it /1/i # NoMethodError under parse.y(e23a60b92) surprising if you expect first `it` to refer to a variable end ``` Essentially, with the promote-to-local-on-first-use behavior, the ambiguity inherent to the design of `it` infects subsequent lines in the same scope. (Maybe this is the same point raised in [[ruby-core:120326]](https://bugs.ruby-lang.org/issues/20965#note-6)) @k0kubun @nobu I feel commit:46fec0f62a1803d44edb8b06e39ac0f358e56670 makes a semantic change maybe too close to the release date. Do we really want this right now? How about a narrower fix for #20955? It's fair enough to say "just don't write code that trigger parse warnings", but it's worth thinking through the details of the design, I think. ---------------------------------------- Bug #20970: `it /1/i` raises undefined method 'it' for main (NoMethodError) even if binding.local_variables includes `it` https://bugs.ruby-lang.org/issues/20970#change-111125 * Author: tompng (tomoya ishida) * Status: Open * ruby -v: ruby 3.4.0dev (2024-12-19T07:16:12Z master 335bba0fde) +PRISM [x86_64-linux] * Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: DONTNEED ---------------------------------------- `it` parameter became a local variable with #20965, but it does not behave like local variable with `--parser=prism` ~~~ruby i=2 42.tap do p it # 42 p local_variables # [:it, :i] p it /1/i # should be 21, got NoMethodError end ~~~ It prints `42`, `[:it, :i], `21` with `--parser=parse.y` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/