diff options
author | Jeremy Evans <code@jeremyevans.net> | 2020-06-10 18:53:25 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2020-06-18 08:21:29 -0700 |
commit | 95dc9c07f3a895f45cfb5dab235cd78f157a9e51 (patch) | |
tree | c5ed4ca00c1700b1c4aa1cea958bb4767acd1ecf | |
parent | aae8223c7076483f1f1641181088790b2f3a66dd (diff) |
Raise RuntimeError for class variable overtaken in nonverbose mode
900e83b50115afda3f79712310e4cb95e4508972 changed from a warning
to an error in this case, but the warning was only issued in
verbose mode, and therefore the error was only raised in verbose
mode. That was not intentional, verbose mode should only change
whether warnings are emitted, not other behavior. This issues
the RuntimeError in all cases.
This change broke a couple tests, as the tests actually issued
the warning and therefore now raise an error. This wasn't caught
earlier as test_variable suppressed the warning in this case,
effectively setting $VERBOSE = false around the code that warned.
basictest isn't run in verbose mode and therefore didn't expose
the issue previously. Fix these tests.
Fixes [Bug #14541]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3210
-rwxr-xr-x | basictest/test.rb | 13 | ||||
-rw-r--r-- | test/ruby/test_variable.rb | 6 | ||||
-rw-r--r-- | variable.c | 2 |
3 files changed, 13 insertions, 8 deletions
diff --git a/basictest/test.rb b/basictest/test.rb index a2eb107bce..52008b78db 100755 --- a/basictest/test.rb +++ b/basictest/test.rb @@ -2137,7 +2137,7 @@ $_ = foobar test_ok($_ == foobar) class Gods - @@rule = "Uranus" # private to Gods + @@rule = "Uranus" def ruler0 @@rule end @@ -2160,7 +2160,7 @@ module Olympians end class Titans < Gods - @@rule = "Cronus" # do not affect @@rule in Gods + @@rule = "Cronus" # modifies @@rule in Gods include Olympians def ruler4 @@rule @@ -2175,7 +2175,14 @@ test_ok(Titans.ruler2 == "Cronus") atlas = Titans.new test_ok(atlas.ruler0 == "Cronus") test_ok(atlas.ruler3 == "Zeus") -test_ok(atlas.ruler4 == "Cronus") +begin + atlas.ruler4 +rescue RuntimeError => e + test_ok(e.message.include?("class variable @@rule of Olympians is overtaken by Gods")) +else + test_ok(false) +end +test_ok(atlas.ruler3 == "Zeus") test_check "trace" $x = 1234 diff --git a/test/ruby/test_variable.rb b/test/ruby/test_variable.rb index 685a06226f..9795223fda 100644 --- a/test/ruby/test_variable.rb +++ b/test/ruby/test_variable.rb @@ -29,9 +29,7 @@ class TestVariable < Test::Unit::TestCase @@rule = "Cronus" # modifies @@rule in Gods include Olympians def ruler4 - EnvUtil.suppress_warning { - @@rule - } + @@rule end end @@ -117,7 +115,7 @@ class TestVariable < Test::Unit::TestCase atlas = Titans.new assert_equal("Cronus", atlas.ruler0) assert_equal("Zeus", atlas.ruler3) - assert_equal("Cronus", atlas.ruler4) + assert_raise(RuntimeError) { atlas.ruler4 } assert_nothing_raised do class << Gods defined?(@@rule) && @@rule diff --git a/variable.c b/variable.c index 0d8a424438..15c4288993 100644 --- a/variable.c +++ b/variable.c @@ -3105,7 +3105,7 @@ cvar_overtaken(VALUE front, VALUE target, ID id) if (front && target != front) { st_data_t did = (st_data_t)id; - if (RTEST(ruby_verbose) && original_module(front) != original_module(target)) { + if (original_module(front) != original_module(target)) { rb_raise(rb_eRuntimeError, "class variable % "PRIsVALUE" of %"PRIsVALUE" is overtaken by %"PRIsVALUE"", ID2SYM(id), rb_class_name(original_module(front)), |