From ae76c8a11e707176a53c84016256646308f36207 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 6 Dec 2023 23:14:59 -0800 Subject: [PATCH] Warn `it` (#9152) https://bugs.ruby-lang.org/issues/18980 --- NEWS.md | 5 +++++ parse.y | 5 +++++ test/ruby/test_syntax.rb | 11 +++++++++++ 3 files changed, 21 insertions(+) diff --git a/NEWS.md b/NEWS.md index bcd8ccad1650f5..07fff9ff63e053 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 34c9152beadbb7..b35547ad744936 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 e78d30148c4801..fe0785a7548b79 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1719,6 +1719,17 @@ def test_numbered_parameter 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)