https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112018-07-05T10:54:59ZRuby Issue Tracking SystemRuby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=728292018-07-05T10:54:59Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>The names of <code>A</code>, <code>B</code>, and <code>Z</code> are temporary names at the moments of assignment.<br>
Once you assign <code>x.singleton_class</code> to a constant, say <code>C</code>, they appear as <code>C::A</code>, <code>C::B</code>, and <code>C::Z</code>, respectively.</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=728332018-07-05T14:29:34Zsilver_phoenix (Pedro Pinto)
<ul></ul><p>Yes, I understand that, but why isn't <code>Z</code> named like <code>B</code> is?</p>
<p><code>Z</code> is <code>#<Module:...></code> (unnamed)<br>
<code>B</code> is <code>#<...>::B</code> (named)</p>
<p>There's something not being initialized on the singleton class, which is only triggered after adding a named module explicitly (like what's being done with the <code>A</code> module example).</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=728342018-07-05T15:38:09Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>silver_phoenix (Pedro Pinto) wrote:</p>
<blockquote>
<p>Yes, I understand that, but why isn't <code>Z</code> named like <code>B</code> is?</p>
<p><code>Z</code> is <code>#<Module:...></code> (unnamed)<br>
<code>B</code> is <code>#<...>::B</code> (named)</p>
</blockquote>
<p>Because they are not "fully-named" yet.</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=728352018-07-05T17:35:04Zsilver_phoenix (Pedro Pinto)
<ul></ul><p>So, what you're saying is that the singleton class is being assigned to a constant when it creates module <code>A</code>, so modules are only named after this step?</p>
<p>Is this expected behavior then?</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=728382018-07-05T21:10:25Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>This looks like a bug to me.</p>
<p>Here's a simplified example:</p>
<pre><code>s = Object.new.singleton_class
a = s.const_set(:A, Module.new) # => #<Module:0x00007fed619915f0>
b = s.class_eval "module B; self end" # => #<Class:0x00007fed6198a020>::B
c = s.const_set(:C, Module.new) # => #<Class:0x00007fed6198a020>::C
</code></pre>
<p><code>a</code> and <code>c</code> should be treated identically. The right behavior would be for their <code>name</code> to be <code>#<Class:0x...>::A/C</code></p>
<p>Note that naming the singleton class afterwards changes all names correctly:</p>
<pre><code>S = s # => #<Class:#<Object:0x00007fed6198a1b0>>
a # => S::A
b # => S::B
c # => S::C
</code></pre> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=873322020-08-31T22:32:07Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>marcandre (Marc-Andre Lafortune) wrote in <a href="#note-5">#note-5</a>:</p>
<blockquote>
<p>This looks like a bug to me.</p>
<p>Here's a simplified example:</p>
<pre><code>s = Object.new.singleton_class
a = s.const_set(:A, Module.new) # => #<Module:0x00007fed619915f0>
b = s.class_eval "module B; self end" # => #<Class:0x00007fed6198a020>::B
c = s.const_set(:C, Module.new) # => #<Class:0x00007fed6198a020>::C
</code></pre>
<p><code>a</code> and <code>c</code> should be treated identically. The right behavior would be for their <code>name</code> to be <code>#<Class:0x...>::A/C</code></p>
</blockquote>
<p>Ruby 2.7 changed the behavior so that <code>a</code> and <code>c</code> are treated identically, but that <code>c</code> is like <code>a</code>:</p>
<pre><code>a # #<Module:0x00000b0bda620368>
b # #<Class:0x00000b0bda6203b8>::B
c # #<Module:0x00000b0bdcdd3c20>
</code></pre>
<p>I think that is sufficient for this bug to be considered closed. However, if you would like the singleton classes named, here is a patch for that:</p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gh">diff --git a/object.c b/object.c
index 08fec850d3..1181b45b3c 100644
</span><span class="gd">--- a/object.c
</span><span class="gi">+++ b/object.c
</span><span class="p">@@ -2536,6 +2536,11 @@</span> rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
{
ID id = id_for_var(mod, name, const);
if (!id) id = rb_intern_str(name);
<span class="gi">+ if (rb_attr_get(mod, rb_intern("__tmp_classpath__")) == Qnil &&
+ rb_attr_get(mod, rb_intern("__classpath__")) == Qnil) {
+ rb_ivar_set(mod, rb_intern("__tmp_classpath__"),
+ rb_funcall(mod, rb_intern("to_s"), 0));
+ }
</span> rb_const_set(mod, id, value);
<span class="err">
</span> return value;
</code></pre>
<p>Output:</p>
<pre><code>a # #<Class:#<Object:0x0000003003436178>>::A
b # #<Class:#<Object:0x0000003003436178>>::B
c # #<Class:#<Object:0x0000003003436178>>::C
</code></pre>
<p>I kind of prefer this as it shows the module is defined under a singleton class. If you like that idea, please submit a feature request for it (or switch this to a feature request and reopen).</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=873342020-09-01T04:50:42Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>Output:</p>
<pre><code>a # #<Class:#<Object:0x0000003003436178>>::A
b # #<Class:#<Object:0x0000003003436178>>::B
c # #<Class:#<Object:0x0000003003436178>>::C
</code></pre>
<p>I kind of prefer this as it shows the module is defined under a singleton class.</p>
</blockquote>
<p>... and it shows the name 'A' and 'C'. That's much better!</p>
<blockquote>
<p>If you like that idea, please submit a feature request for it (or switch this to a feature request and reopen).</p>
</blockquote>
<p>...or consider the current output a bug (even if now it is consistently buggy 😆)</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="o"><<</span> <span class="n">o</span>
<span class="k">module</span> <span class="nn">Bar</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">o</span><span class="p">.</span><span class="nf">singleton_class</span><span class="o">::</span><span class="no">Bar</span> <span class="c1"># => #<Class:0x00007f90f3300008>::Bar</span>
</code></pre>
<p>So I suggest I'll commit your patch, unless there's an objection.</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=873382020-09-01T14:20:32Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote in <a href="#note-7">#note-7</a>:</p>
<blockquote>
<p>So I suggest I'll commit your patch, unless there's an objection.</p>
</blockquote>
<p>I have no objection, so if you consider the current behavior a bug, please commit the patch (assuming it doesn't cause test or spec failures).</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=873402020-09-01T15:01:26Zsawa (Tsuyoshi Sawada)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/87340/diff?detail_id=57843">diff</a>)</li></ul><p>Fixed dangling participle.</p> Ruby master - Bug #14895: Inconsistent constant names when using const_set on a singleton classhttps://redmine.ruby-lang.org/issues/14895?journal_id=873542020-09-02T04:05:33ZAnonymous
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Fix constant names set using const_set on a singleton class Fixes [Bug #14895]" href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/5e16857315bf55307c5fc887ca6f03bfa0630a93">git|5e16857315bf55307c5fc887ca6f03bfa0630a93</a>.</p>
<hr>
<p>Fix constant names set using const_set on a singleton class</p>
<p>Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Inconsistent constant names when using const_set on a singleton class (Closed)" href="https://redmine.ruby-lang.org/issues/14895">#14895</a>]</p>