Skip to content

Commit

Permalink
Remove the uninitialized instance variable verbose mode warning
Browse files Browse the repository at this point in the history
This speeds up all instance variable access, even when not in
verbose mode.  Uninitialized instance variable warnings were
rarely helpful, and resulted in slower code if you wanted to
avoid warnings when run in verbose mode.

Implements [Feature #17055]
  • Loading branch information
jeremyevans committed Dec 10, 2020
1 parent 4a559aa commit 01b7d5a
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 24 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ end
message and backtrace are printed in order from the innermost.
[[Feature #8661]]
* Accessing an uninitialized instance variable no longer emits a
warning in verbose mode. [[Feature #17055]]
[Bug #4352]: https://bugs.ruby-lang.org/issues/4352
[Bug #6087]: https://bugs.ruby-lang.org/issues/6087
Expand Down Expand Up @@ -629,6 +631,7 @@ end
[Feature #16815]: https://bugs.ruby-lang.org/issues/16815
[Feature #16828]: https://bugs.ruby-lang.org/issues/16828
[Misc #16961]: https://bugs.ruby-lang.org/issues/16961
[Feature #17055]: https://bugs.ruby-lang.org/issues/17055
[Feature #17104]: https://bugs.ruby-lang.org/issues/17104
[Feature #17122]: https://bugs.ruby-lang.org/issues/17122
[Feature #17134]: https://bugs.ruby-lang.org/issues/17134
Expand Down
6 changes: 2 additions & 4 deletions test/-ext-/symbol/test_inadvertent_creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,8 @@ def test_kwarg_symbol_leak_just_rest

def test_iv_get
obj = Object.new
assert_warning(/not initialized/) do
assert_no_immortal_symbol_created("rb_iv_get") do |name|
Bug::Symbol.iv_get(obj, name)
end
assert_no_immortal_symbol_created("rb_iv_get") do |name|
Bug::Symbol.iv_get(obj, name)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/ruby/test_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,8 @@ def capture_warning_warn(category: false)
end

def test_warning_warn
warning = capture_warning_warn {@a}
assert_match(/instance variable @a not initialized/, warning[0])
warning = capture_warning_warn {$asdfasdsda_test_warning_warn}
assert_match(/global variable `\$asdfasdsda_test_warning_warn' not initialized/, warning[0])

assert_equal(["a\nz\n"], capture_warning_warn {warn "a\n", "z"})
assert_equal([], capture_warning_warn {warn})
Expand Down Expand Up @@ -1040,7 +1040,7 @@ def test_warning_warn_circular_require_backtrace
end

def test_warning_warn_super
assert_in_out_err(%[-W0], "#{<<~"{#"}\n#{<<~'};'}", [], /instance variable @a not initialized/)
assert_in_out_err(%[-W0], "#{<<~"{#"}\n#{<<~'};'}", [], /global variable `\$asdfiasdofa_test_warning_warn_super' not initialized/)
{#
module Warning
def warn(message)
Expand All @@ -1049,7 +1049,7 @@ def warn(message)
end
$VERBOSE = true
@a
$asdfiasdofa_test_warning_warn_super
};
end

Expand Down
2 changes: 0 additions & 2 deletions test/ruby/test_jit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,9 @@ def bar
end
end
verbose, $VERBOSE = $VERBOSE, false # suppress "instance variable @b not initialized"
print(Foo.new.bar)
print(Foo.new.bar)
print(Foo.new.bar)
$VERBOSE = verbose
end;
end

Expand Down
4 changes: 2 additions & 2 deletions test/ruby/test_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2401,7 +2401,7 @@ def ivar

def test_uninitialized_instance_variable
a = AttrTest.new
assert_warning(/instance variable @ivar not initialized/) do
assert_warning('') do
assert_nil(a.ivar)
end
a.instance_variable_set(:@ivar, 42)
Expand All @@ -2410,7 +2410,7 @@ def test_uninitialized_instance_variable
end

name = "@\u{5909 6570}"
assert_warning(/instance variable #{name} not initialized/) do
assert_warning('') do
assert_nil(a.instance_eval(name))
end
end
Expand Down
10 changes: 1 addition & 9 deletions variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1217,14 +1217,8 @@ rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
VALUE
rb_ivar_get(VALUE obj, ID id)
{
VALUE iv = rb_ivar_lookup(obj, id, Qundef);
VALUE iv = rb_ivar_lookup(obj, id, Qnil);
RB_DEBUG_COUNTER_INC(ivar_get_base);

if (iv == Qundef) {
if (RTEST(ruby_verbose))
rb_warning("instance variable %"PRIsVALUE" not initialized", QUOTE_ID(id));
iv = Qnil;
}
return iv;
}

Expand Down Expand Up @@ -3526,8 +3520,6 @@ rb_iv_get(VALUE obj, const char *name)
ID id = rb_check_id_cstr(name, strlen(name), rb_usascii_encoding());

if (!id) {
if (RTEST(ruby_verbose))
rb_warning("instance variable %s not initialized", name);
return Qnil;
}
return rb_ivar_get(obj, id);
Expand Down
3 changes: 0 additions & 3 deletions vm_insnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1179,9 +1179,6 @@ vm_getivar(VALUE obj, ID id, const rb_iseq_t *iseq, IVC ic, const struct rb_call
return val;
}
else {
if (!is_attr && RTEST(ruby_verbose)) {
rb_warning("instance variable %"PRIsVALUE" not initialized", QUOTE_ID(id));
}
return Qnil;
}
}
Expand Down

0 comments on commit 01b7d5a

Please sign in to comment.