https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112012-10-06T05:47:51ZRuby Issue Tracking SystemRuby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=301132012-10-06T05:47:51Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>=begin<br>
Additionally, when the module is given a name it still can't find the constant:</p>
<p>module M<br>
C = 1</p>
<pre><code>def self.m
C
end
</code></pre>
<p>end</p>
<p>puts 'named module'<br>
M.m</p>
<p>puts 'anonymous module'<br>
m = M.dup<br>
begin<br>
m.m<br>
rescue NameError<br>
p $!<br>
end</p>
<p>puts 're-named module'<br>
N = m<br>
begin<br>
N.m<br>
rescue NameError<br>
p $!<br>
end</p>
<p>Ruby trunk:</p>
<p>$ ruby20 -v t.rb<br>
ruby 2.0.0dev (2012-09-06 trunk 36915) [x86_64-darwin12.1.0]<br>
named module<br>
anonymous module<br>
#<NameError: uninitialized constant Module::C><br>
re-named module<br>
#<NameError: uninitialized constant Module::C></p>
<p>=end</p> Ruby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=317322012-10-27T09:31:59Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>mame (Yusuke Endoh)</i></li></ul> Ruby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=317882012-10-27T19:09:45Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> changed from <i>mame (Yusuke Endoh)</i> to <i>nobu (Nobuyoshi Nakada)</i></li></ul><p>Indeed, it looks a bug. Nobu, could you investigate?</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=346752012-12-13T05:54:39Ztarui (Masaya Tarui)tarui@prx.jp
<ul></ul><p>hi,</p>
<p>I found strange behavior.</p>
<p>$ ruby -e "module M;C=1;def self.f;C end end;d=M.dup;p M.f;p d.f;class A;end;p d.f"<br>
1<br>
1<br>
-e:1:in <code>f': uninitialized constant Module::C (NameError) from -e:1:in </code>'</p>
<p>It seems to hold the problem in InlineCache too.</p> Ruby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=346762012-12-13T06:29:08Ztarui (Masaya Tarui)tarui@prx.jp
<ul></ul><p>additional sample.<br>
d.f referring to M::C is correct? or d::C?</p>
<p>$ ruby -e "module M;C=1;def self.f;C end end;d=M.dup;p M.f;p d.f;d::C=2;p M.f;p d::C;p d.f"<br>
1<br>
1<br>
-e:1: warning: already initialized constant #<a href="Module:0x007ffb39f4c860" class="external">Module:0x007ffb39f4c860</a>::C<br>
-e:1: warning: previous definition of C was here<br>
1<br>
2<br>
1</p> Ruby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=347582012-12-15T12:25:44ZAnonymous
<ul></ul><p>=begin</p>
<p>Nobu, I have found the cause of the bug - the cref_stack of methods are not fixed up to point to the new class/module when the class/module is duped.</p>
<p>Do you want me to commit it or attach it here for your review?</p>
<p>=end</p> Ruby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=347862012-12-17T11:38:39ZAnonymous
<ul><li><strong>Assignee</strong> changed from <i>nobu (Nobuyoshi Nakada)</i> to <i>Anonymous</i></li></ul> Ruby master - Bug #7107: Ruby can no longer find constants in methods in anonymous moduleshttps://bugs.ruby-lang.org/issues/7107?journal_id=347962012-12-17T18:49:04ZAnonymous
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li><li><strong>% Done</strong> changed from <i>0</i> to <i>100</i></li></ul><p>This issue was solved with changeset r38423.<br>
Eric, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<hr>
<ul>
<li>class.c (rewrite_cref_stack, clone_method): rewrite a method's cref<br>
stack when cloning into a new class to allow lexical const lookup to<br>
work as expected <a href="/issues/7107">[ruby-core:47834]</a> [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Ruby can no longer find constants in methods in anonymous modules (Closed)" href="https://bugs.ruby-lang.org/issues/7107">#7107</a>]</li>
<li>test/ruby/test_class.rb (class TestClass): related test</li>
</ul>