Skip to content

Commit

Permalink
Warn it (#9152)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Dec 7, 2023
1 parent 41c00bc commit ae76c8a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions parse.y
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions test/ruby/test_syntax.rb
Expand Up @@ -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)
Expand Down

0 comments on commit ae76c8a

Please sign in to comment.