summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS.md5
-rw-r--r--parse.y5
-rw-r--r--test/ruby/test_syntax.rb11
3 files changed, 21 insertions, 0 deletions
diff --git a/NEWS.md b/NEWS.md
index bcd8ccad16..07fff9ff63 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -257,6 +257,10 @@ changelog for details of the default gems or bundled gems.
removed. Environment variables `RUBY_GC_HEAP_%d_INIT_SLOTS` should be
used instead. [[Feature #19785]]
+* `it` calls without arguments in a block with no ordinary parameters are
+ deprecated. `it` will be a reference to the first block parameter in Ruby 3.4.
+ [[Feature #18980]]
+
## Stdlib compatibility issues
* `racc` is promoted to bundled gems.
@@ -374,6 +378,7 @@ changelog for details of the default gems or bundled gems.
[Feature #18515]: https://bugs.ruby-lang.org/issues/18515
[Feature #18551]: https://bugs.ruby-lang.org/issues/18551
[Feature #18885]: https://bugs.ruby-lang.org/issues/18885
+[Feature #18980]: https://bugs.ruby-lang.org/issues/18980
[Feature #18949]: https://bugs.ruby-lang.org/issues/18949
[Bug #19012]: https://bugs.ruby-lang.org/issues/19012
[Bug #19150]: https://bugs.ruby-lang.org/issues/19150
diff --git a/parse.y b/parse.y
index 34c9152bea..b35547ad74 100644
--- a/parse.y
+++ b/parse.y
@@ -12785,6 +12785,11 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
}
# endif
/* method call without arguments */
+ if (dyna_in_block(p) && id == rb_intern("it")
+ && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))
+ && p->max_numparam != ORDINAL_PARAM) {
+ rb_warn0("`it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it");
+ }
return NEW_VCALL(id, loc);
case ID_GLOBAL:
return NEW_GVAR(id, loc);
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index e78d30148c..fe0785a754 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1719,6 +1719,17 @@ eom
assert_valid_syntax("p { [_1 **2] }")
end
+ def test_it
+ assert_no_warning(/`it`/) {eval('if false; it; end')}
+ assert_no_warning(/`it`/) {eval('def foo; it; end')}
+ assert_warn(/`it`/) {eval('0.times { it }')}
+ assert_no_warning(/`it`/) {eval('0.times { || it }')}
+ assert_no_warning(/`it`/) {eval('0.times { |_n| it }')}
+ assert_warn(/`it`/) {eval('0.times { it; it = 1; it }')}
+ assert_no_warning(/`it`/) {eval('0.times { it = 1; it }')}
+ assert_no_warning(/`it`/) {eval('it = 1; 0.times { it }')}
+ end
+
def test_value_expr_in_condition
mesg = /void value expression/
assert_syntax_error("tap {a = (true ? next : break)}", mesg)