summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--bootstraptest/test_eval.rb2
-rw-r--r--test/ruby/test_module.rb50
-rw-r--r--vm_insnhelper.c14
4 files changed, 52 insertions, 19 deletions
diff --git a/ChangeLog b/ChangeLog
index 08dfdb052b..e38f4f98c6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Mar 31 16:49:56 2011 Shugo Maeda <shugo@ruby-lang.org>
+
+ * vm_insnhelper.c (vm_get_ev_const): search root cref properly.
+ [ruby-dev:43365]
+
Thu Mar 31 14:50:25 2011 Shugo Maeda <shugo@ruby-lang.org>
* eval.c (rb_mod_s_constants): should ignore crefs with
diff --git a/bootstraptest/test_eval.rb b/bootstraptest/test_eval.rb
index 9ae50a6d30..c347d50ac9 100644
--- a/bootstraptest/test_eval.rb
+++ b/bootstraptest/test_eval.rb
@@ -137,7 +137,7 @@ assert_equal %q{C}, %q{
}
C.new.m
}
-assert_equal %q{C}, %q{
+assert_equal %q{top}, %q{
Const = :top
class C
Const = :C
diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index 6a7f566ac7..6d28616d86 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -1006,19 +1006,47 @@ class TestModule < Test::Unit::TestCase
assert_in_out_err([], src, %w(Object :ok), [])
end
- module A
- B = 42
- end
-
def test_constant_lookup_in_method_defined_by_class_eval
- A.class_eval do
- def self.f
- B
+ src = <<-INPUT
+ class A
+ B = 42
end
- end
- assert_raise(NameError) do
- A.f
- end
+ A.class_eval do
+ def self.f
+ B
+ end
+
+ def f
+ B
+ end
+ end
+
+ begin
+ A.f
+ rescue NameError
+ puts "A.f"
+ end
+ begin
+ A.new.f
+ rescue NameError
+ puts "A.new.f"
+ end
+ INPUT
+ assert_in_out_err([], src, %w(A.f A.new.f), [])
+ end
+
+ def test_constant_lookup_in_toplevel_class_eval
+ src = <<-INPUT
+ module X
+ A = 123
+ end
+ begin
+ X.class_eval { A }
+ rescue NameError => e
+ puts e
+ end
+ INPUT
+ assert_in_out_err([], src, ["uninitialized constant A"], [])
end
end
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 00a21e3669..508ad8584e 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -1155,16 +1155,16 @@ vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,
if (orig_klass == Qnil) {
/* in current lexical scope */
- const NODE *cref = vm_get_cref(iseq, th->cfp->lfp, th->cfp->dfp);
- const NODE *root_cref = NULL;
+ const NODE *root_cref = vm_get_cref(iseq, th->cfp->lfp, th->cfp->dfp);
+ const NODE *cref;
VALUE klass = orig_klass;
+ while (root_cref && root_cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) {
+ root_cref = root_cref->nd_next;
+ }
+ cref = root_cref;
while (cref && cref->nd_next) {
- if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL)) {
- klass = cref->nd_clss;
- if (root_cref == NULL)
- root_cref = cref;
- }
+ klass = cref->nd_clss;
cref = cref->nd_next;
if (!NIL_P(klass)) {