https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112020-07-03T12:33:04ZRuby Issue Tracking SystemRuby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=864202020-07-03T12:33:04ZEregon (Benoit Daloze)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/16977">Bug #16977</a>: Ambiguous lookup super for refinements</i> added</li></ul> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=864942020-07-10T22:20:26Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>After more analysis, I've determined that this issue is due to CREF handling in method lookup in <code>search_refined_method</code>. It fails in the lexical case because the module is affected by the refinement (refinement blocks have an implicit <code>using</code> of the refinement themselves, I guess).</p>
<p>You can reproduce the SystemStackError outside of the lexical case by having the refinement activated at the point of method definition:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">C</span>
<span class="k">def</span> <span class="nf">foo</span>
<span class="p">[</span><span class="s2">"C"</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">M</span><span class="p">;</span> <span class="k">end</span>
<span class="n">refinement</span> <span class="o">=</span> <span class="no">Module</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span>
<span class="no">R</span> <span class="o">=</span> <span class="n">refine</span> <span class="no">C</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">foo</span>
<span class="p">[</span><span class="s2">"R"</span><span class="p">]</span> <span class="o">+</span> <span class="k">super</span>
<span class="k">end</span>
<span class="kp">include</span> <span class="no">M</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">using</span> <span class="n">refinement</span>
<span class="no">M</span><span class="p">.</span><span class="nf">define_method</span><span class="p">(</span><span class="ss">:foo</span><span class="p">){[</span><span class="s2">"M"</span><span class="p">]</span> <span class="o">+</span> <span class="k">super</span><span class="p">()}</span>
<span class="nb">p</span> <span class="no">C</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">foo</span>
</code></pre>
<p>You can fix the issue by skipping any refined method during super lookup if the current method also uses the same refinement. I implemented this in a pull request: <a href="https://github.com/ruby/ruby/pull/3309" class="external">https://github.com/ruby/ruby/pull/3309</a>.</p> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=867502020-07-27T15:18:41Zjeremyevans (Jeremy Evans)code@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Prevent SystemStackError when calling super in module with activated refinement Without this, if..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/eeef16e190cdabc2ba474622720f8e3df7bac43b">git|eeef16e190cdabc2ba474622720f8e3df7bac43b</a>.</p>
<hr>
<p>Prevent SystemStackError when calling super in module with activated refinement</p>
<p>Without this, if a refinement defines a method that calls super and<br>
includes a module with a module that calls super and has a activated<br>
refinement at the point super is called, the module method super call<br>
will end up calling back into the refinement method, creating a loop.</p>
<p>Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: SystemStackError when using super inside Module included and lexically inside refinement (Closed)" href="https://redmine.ruby-lang.org/issues/17007">#17007</a>]</p> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=867592020-07-27T19:30:59ZEregon (Benoit Daloze)
<ul></ul><p>Thanks for the fix!<br>
BTW this changes the behavior on a new spec, is that intended? (result is [:A, :C] instead of [:A, :LOCAL, :C] on < 2.8)<br>
<a href="https://github.com/ruby/spec/commit/b0da11b52560860e844470d145acee0ff4d4acea?w=1" class="external">https://github.com/ruby/spec/commit/b0da11b52560860e844470d145acee0ff4d4acea?w=1</a></p> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=867622020-07-27T21:02:52Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>Eregon (Benoit Daloze) wrote in <a href="#note-4">#note-4</a>:</p>
<blockquote>
<p>Thanks for the fix!<br>
BTW this changes the behavior on a new spec, is that intended? (result is [:A, :C] instead of [:A, :LOCAL, :C] on < 2.8)<br>
<a href="https://github.com/ruby/spec/commit/b0da11b52560860e844470d145acee0ff4d4acea?w=1" class="external">https://github.com/ruby/spec/commit/b0da11b52560860e844470d145acee0ff4d4acea?w=1</a></p>
</blockquote>
<p>It's a consequence of the fix (skips the currently activated refinement during super), but I wouldn't say it is intended. Ideally, the fix would only affect looping cases. Unfortunately, I'm not sure if there is a better way to detect whether looping during super would occur. I'm also not sure whether the approach I committed can detect all cases of looping (there may be other ways to introduce looping during super).</p>
<p>Maybe the refinements spec needs to be changed if we want to forbid looping, spelling out how to handle super in modules included in refinements where the refinements are activated at the point of super call.</p> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=876282020-09-22T19:07:31Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Assigned</i></li></ul><p>To restore compatibility with Ruby 2.7 and fix <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Refinements behavior is broken (Closed)" href="https://redmine.ruby-lang.org/issues/17182">#17182</a>, I've reverted <a class="changeset" title="Prevent SystemStackError when calling super in module with activated refinement Without this, if..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/eeef16e190cdabc2ba474622720f8e3df7bac43b">eeef16e190cdabc2ba474622720f8e3df7bac43b</a> at <a class="changeset" title="Revert "Prevent SystemStackError when calling super in module with activated refinement" This re..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/179384a66862d5ef7413b6f4850b97d0becf4ec9">179384a66862d5ef7413b6f4850b97d0becf4ec9</a>.</p>
<p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/12">@shugo (Shugo Maeda)</a>, I would appreciate your input here on how refinements should be handled in this case.</p> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=894282020-12-23T01:08:21Zshugo (Shugo Maeda)
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>To restore compatibility with Ruby 2.7 and fix <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Refinements behavior is broken (Closed)" href="https://redmine.ruby-lang.org/issues/17182">#17182</a>, I've reverted <a class="changeset" title="Prevent SystemStackError when calling super in module with activated refinement Without this, if..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/eeef16e190cdabc2ba474622720f8e3df7bac43b">eeef16e190cdabc2ba474622720f8e3df7bac43b</a> at <a class="changeset" title="Revert "Prevent SystemStackError when calling super in module with activated refinement" This re..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/179384a66862d5ef7413b6f4850b97d0becf4ec9">179384a66862d5ef7413b6f4850b97d0becf4ec9</a>.</p>
<p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/12">@shugo (Shugo Maeda)</a>, I would appreciate your input here on how refinements should be handled in this case.</p>
</blockquote>
<p>I think the change is acceptable, but it may be better to prohibit include/prepend in refinement modules in future versions to avoid such implementation difficulties.</p> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=897922021-01-06T02:00:58Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/17429">Bug #17429</a>: Prohibit include/prepend in refinement modules</i> added</li></ul> Ruby master - Bug #17007: SystemStackError when using super inside Module included and lexically inside refinementhttps://redmine.ruby-lang.org/issues/17007?journal_id=942392021-10-21T16:14:12Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Refinement#include is now deprecated and will be removed in Ruby 3.2.</p>