Skip to content

Commit 628e432

Browse files
committed
fix NameError message
The following code produces two NameErrors respectively and they are independent, but the second one can show `private constant` message because of first NameError. ```ruby class C class PrivateClass; end private_constant :PrivateClass end begin eval('class C::PrivateClass; end') rescue => e p e end begin Object.const_get 'Foo' rescue => e p e end #<NameError: private constant C::PrivateClass referenced> #<NameError: private constant C::Foo referenced> #=> should be #<NameError: uninitialized constant Foo> ``` It fails the test-all tests with `make test-all TESTS='ruby/class ruby/parse --seed=58891 -v`. The reason is clear miss from 7387c08373a
1 parent 62781d4 commit 628e432

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

test/ruby/test_class.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,17 @@ def foo; 42; end
355355
assert_equal(42, PrivateClass.new.foo)
356356
end
357357

358+
def test_private_const_access
359+
assert_raise_with_message NameError, /uninitialized/ do
360+
begin
361+
eval('class ::TestClass::PrivateClass; end')
362+
rescue NameError => e
363+
end
364+
365+
Object.const_get "NOT_AVAILABLE_CONST_NAME_#{__LINE__}"
366+
end
367+
end
368+
358369
StrClone = String.clone
359370
Class.new(StrClone)
360371

variable.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,11 +2073,12 @@ rb_const_missing(VALUE klass, VALUE name)
20732073
VALUE
20742074
rb_mod_const_missing(VALUE klass, VALUE name)
20752075
{
2076-
VALUE ref = GET_EC()->private_const_reference;
2076+
rb_execution_context_t *ec = GET_EC();
2077+
VALUE ref = ec->private_const_reference;
20772078
rb_vm_pop_cfunc_frame();
20782079
if (ref) {
2079-
rb_name_err_raise("private constant %2$s::%1$s referenced",
2080-
ref, name);
2080+
ec->private_const_reference = 0;
2081+
rb_name_err_raise("private constant %2$s::%1$s referenced", ref, name);
20812082
}
20822083
uninitialized_constant(klass, name);
20832084

0 commit comments

Comments
 (0)