https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112020-08-05T20:25:12ZRuby Issue Tracking SystemRuby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869432020-08-05T20:25:12Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Other people have felt the same way, including me (see <a href="https://bugs.ruby-lang.org/issues/11473#note-7" class="external">https://bugs.ruby-lang.org/issues/11473#note-7</a> and <a href="https://bugs.ruby-lang.org/issues/8976#note-67" class="external">https://bugs.ruby-lang.org/issues/8976#note-67</a>). However, <a class="user active user-mention" href="https://redmine.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> decided during the November 2015 developer meeting that they should be frozen for simplicity, see <a href="https://docs.google.com/document/d/1D0Eo5N7NE_unIySOKG9lVj_eyXf66BQPM4PKp7NvMyQ/pub" class="external">https://docs.google.com/document/d/1D0Eo5N7NE_unIySOKG9lVj_eyXf66BQPM4PKp7NvMyQ/pub</a></p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869442020-08-05T20:58:44Zbughit (bug hit)
<ul></ul><p>If you want to get a mutable string from an interpolated literal <code>+"_#{method1}_"</code>, 2 allocation will be done instead of 1, if it weren't pointlessly frozen.</p>
<p>In this case a feature designed to reduce allocations is producing more allocations. Behavior that's counter-intuitive and illogical and acting counter to its intent, is not simple.</p>
<p>This happens to be something that can be changed without breaking anything. Can it get a second look?</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869452020-08-05T21:05:31Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>If you would like the behavior changed, you should file a feature request for it, and add it to the agenda of a future developer meeting.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869462020-08-05T21:10:45ZEregon (Benoit Daloze)
<ul></ul><p>FWIW I'd be +1 to make interpolated Strings non-frozen.<br>
It's BTW still the behavior in TruffleRuby to this day, since it seems to cause no incompatibility and is convenient for writing the core library with Ruby code.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869472020-08-05T21:18:00Zbughit (bug hit)
<ul></ul><p>Can't we just treat this as I feature request? The reasons are, it will reduce allocations, be more logical, less surprising and produce simpler code (when a mutable string is needed and you don't want extra allocations)</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869482020-08-05T21:26:54Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Tracker</strong> changed from <i>Misc</i> to <i>Feature</i></li><li><strong>Subject</strong> changed from <i>Why are interpolated string literals frozen?</i> to <i>Do not freeze interpolated strings when using frozen-string-literal</i></li><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li></ul><p>We can treat this as a feature request. I changed it from Misc to Feature and modified the Subject to be the feature you are requesting as opposed to a question. The issue for the next developer meeting is at <a href="https://bugs.ruby-lang.org/issues/17041" class="external">https://bugs.ruby-lang.org/issues/17041</a> if you want to add it to the agenda.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869502020-08-06T01:37:04ZDan0042 (Daniel DeLorme)
<ul></ul><p>+1<br>
A while ago I opened ticket <a class="issue tracker-5 status-5 priority-4 priority-default closed" title="Misc: Reconsider impact of frozen_string_literal on dynamic strings (Closed)" href="https://redmine.ruby-lang.org/issues/16047">#16047</a> about the same thing.<br>
Seems a lot of people don't like the current behavior so much.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869542020-08-06T09:23:16Zbyroot (Jean Boussier)byroot@ruby-lang.org
<ul></ul><blockquote>
<p>If you want to get a mutable string from an interpolated literal +"<em>#{method1}</em>", 2 allocation will be done instead of 1</p>
</blockquote>
<p>Maybe the parser could understand <code>+"#{}"</code> and avoid that second allocation? The same way it understand <code>"".freeze</code>.</p>
<p>Because I understand the consistency argument. "All string literals are frozen" is much easier to wrap your head around than "All string literals are frozen except the ones that are interpolated".</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869552020-08-06T10:12:19ZEregon (Benoit Daloze)
<ul></ul><p>byroot (Jean Boussier) wrote in <a href="#note-8">#note-8</a>:</p>
<blockquote>
<p>Because I understand the consistency argument. "All string literals are frozen" is much easier to wrap your head around than "All string literals are frozen except the ones that are interpolated".</p>
</blockquote>
<p>I'd argue <code>"a#{2}c"</code> is not a string literal (<code>"a2c"</code> is a string literal).<br>
It's actually syntactic sugar for <code>mutableEmptyStringOfSourceEncoding << "a" << 2.to_s << "c"</code>.<br>
(+ <code>.freeze</code> the result in current semantics which feels unneeded)<br>
Same as <code>42</code> is an integer literal, but <code>41 + 1</code> is not an integer literal.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=869582020-08-06T11:10:15Zbyroot (Jean Boussier)byroot@ruby-lang.org
<ul></ul><blockquote>
<p>I'd argue "a#{2}c" is not a string literal ("a2c" is a string literal).</p>
</blockquote>
<p>I understand your point of view. However in my view what defines a literal, is the use of a specific syntax, so <code>""</code> in this case.</p>
<p>Same for hashes or arrays, <code>[1 + 2]</code> is a literal (to me), it might not be a "static" literal, but it is a literal nonetheless.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=870052020-08-10T18:51:40Zbughit (bug hit)
<ul></ul><blockquote>
<p>However in my view what defines a literal, is the use of a specific syntax, so ""</p>
</blockquote>
<p>There is only one reason for freezing literals, to be able to intern them and reduce allocation. In fact the feature is poorly named, after the consequence(freezing), not the cause (interning).</p>
<p>Freezing strings that are not interned is pointless and counterproductive (it leads to more allocation in the name of less).</p>
<p>A user who does not understand why literals are frozen is unlikely to even notice that interpolated ones are not. And if he does and wonders why, he will, if persistent, arrive at the reason (interning) and be better off for it. The foolish, in this case, consistency in the name of "simplicity" helps no one.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=870132020-08-11T06:22:27Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>bughit (bug hit) wrote in <a href="#note-11">#note-11</a>:</p>
<blockquote>
<blockquote>
<p>However in my view what defines a literal, is the use of a specific syntax, so ""</p>
</blockquote>
<p>There is only one reason for freezing literals, to be able to intern them and reduce allocation. In fact the feature is poorly named, after the consequence(freezing), not the cause (interning).</p>
</blockquote>
<p>My understanding is that another reason is avoidance of alias effects. It's easy to write code that when cut down to the essential, does this:</p>
<pre><code>a = b = "My string"
a.gsub!(/My/, 'Your')
</code></pre>
<p>and expects <code>b</code> to still be "My string". Freezing makes sure this throws an error.</p>
<p>(This is not an argument for or againts freezing interpolated strings.)</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=870202020-08-11T15:57:20Zbughit (bug hit)
<ul></ul><blockquote>
<p>another reason is avoidance of alias effects</p>
</blockquote>
<p>What you've shown is not another reason for freezing.</p>
<p><code>a = b = "My string"</code></p>
<p>both a and b refer to the same string object regardless of interning/freezing</p>
<p>there's no expectation that mutating it via <code>a</code> will not affect <code>b</code></p>
<p>the interning scenario is:</p>
<pre><code class="rb syntaxhl" data-language="rb"><span class="n">a</span> <span class="o">=</span> <span class="s2">"My string"</span>
<span class="n">b</span> <span class="o">=</span> <span class="s2">"My string"</span>
<span class="n">a</span><span class="p">.</span><span class="nf">gsub!</span><span class="p">(</span><span class="sr">/My/</span><span class="p">,</span> <span class="s1">'Your'</span><span class="p">)</span>
</code></pre>
<p>here there's an appearance of 2 string objects but when they are interned, there's only one, so mutation can not be allowed. As I said, interning is the feature, and it requires freezing.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=870722020-08-15T10:47:32Zsawa (Tsuyoshi Sawada)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/87072/diff?detail_id=57748">diff</a>)</li></ul> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=871902020-08-26T07:43:38Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>Non-freezing interpolated strings is good thing to reduce allocations.<br>
I think it is possible if most developers understands difference of interpolated and non-interpolated strings.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=873102020-08-31T08:50:38Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>OK. Persuaded. Make them unfrozen.</p>
<p>Matz.</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=873222020-08-31T19:28:04ZEregon (Benoit Daloze)
<ul><li><strong>Assignee</strong> set to <i>Eregon (Benoit Daloze)</i></li></ul><p>I'll try to make a PR for this change: <a href="https://github.com/ruby/ruby/pull/3488" class="external">https://github.com/ruby/ruby/pull/3488</a></p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=873412020-09-01T15:39:53Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Eregon (Benoit Daloze) wrote in <a href="#note-17">#note-17</a>:</p>
<blockquote>
<p>I'll try to make a PR for this change: <a href="https://github.com/ruby/ruby/pull/3488" class="external">https://github.com/ruby/ruby/pull/3488</a></p>
</blockquote>
<p>Thanks, I've given it a try.</p>
<p>I found <code>"foo#{ "foo" }"</code> frozen because it is optimized to <code>"foofoo"</code> at the parser. What do you think?</p>
<pre><code>$ ./miniruby --enable-frozen-string-literal -e 'p "foo#{ "foo" }".frozen?'
true
</code></pre> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=873472020-09-01T18:18:21ZEregon (Benoit Daloze)
<ul></ul><p>mame (Yusuke Endoh) wrote in <a href="#note-18">#note-18</a>:</p>
<blockquote>
<p>I found <code>"foo#{ "foo" }"</code> frozen because it is optimized to <code>"foofoo"</code> at the parser. What do you think?</p>
</blockquote>
<p>I guess that's semantically correct (besides the frozen status), since interpolation does not need to call <code>#to_s</code> for a String.<br>
Since such code is very unlikely to appear in real code, I think it ultimately does not matter too much.</p>
<p>But if we can easily remove that optimization in the parser, I think it would be better, because this is an inconsistency (it makes it harder to reason about Ruby semantics & it breaks referential transparency) and optimizing <code>"foo#{ "foo" }"</code> seems to have no use in practice.<br>
Could you point me to where the optimization is done in the parser if you found it? :)</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=873642020-09-02T14:15:21Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p><a href="https://github.com/nobu/ruby/tree/unfrozen-literal" class="external">https://github.com/nobu/ruby/tree/unfrozen-literal</a></p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=873852020-09-03T00:29:04Zko1 (Koichi Sasada)
<ul></ul><p>I found that freezing interpolated strings are help for Ractor programming with constants.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">C</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">10</span>
<span class="no">STR</span> <span class="o">=</span> <span class="s2">"foo</span><span class="si">#{</span><span class="n">i</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=874042020-09-03T14:26:46ZEregon (Benoit Daloze)
<ul></ul><p>ko1 (Koichi Sasada) wrote in <a href="#note-21">#note-21</a>:</p>
<blockquote>
<p>I found that freezing interpolated strings are help for Ractor programming with constants.</p>
</blockquote>
<p>Right, anything deeply frozen is helpful for Ractor.<br>
But interpolated Strings are probably not that common.<br>
I see an interpolated String much like an Array literals, and those are not frozen without an explicit <code>.freeze</code>.<br>
Related comment: <a href="https://bugs.ruby-lang.org/issues/17100#note-23" class="external">https://bugs.ruby-lang.org/issues/17100#note-23</a></p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=875652020-09-15T19:32:53ZEregon (Benoit Daloze)
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Interpolated strings are no longer frozen with frozen-string-literal: true * Remove freezestring..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/9b535f3ff7c2f48e34dd44564df7adc723b81276">git|9b535f3ff7c2f48e34dd44564df7adc723b81276</a>.</p>
<hr>
<p>Interpolated strings are no longer frozen with frozen-string-literal: true</p>
<ul>
<li>Remove freezestring instruction since this was the only usage for it.</li>
<li>[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Do not freeze interpolated strings when using frozen-string-literal (Closed)" href="https://redmine.ruby-lang.org/issues/17104">#17104</a>]</li>
</ul> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=875662020-09-15T19:35:52ZEregon (Benoit Daloze)
<ul></ul><p>I merged <a href="https://github.com/ruby/ruby/pull/3488" class="external">https://github.com/ruby/ruby/pull/3488</a> so it's in time for preview1.<br>
<a class="user active user-mention" href="https://redmine.ruby-lang.org/users/4">@nobu (Nobuyoshi Nakada)</a> Could you decide if you think <a href="https://github.com/nobu/ruby/tree/unfrozen-literal" class="external">https://github.com/nobu/ruby/tree/unfrozen-literal</a> is worth it?</p> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=875672020-09-15T19:36:00ZEregon (Benoit Daloze)
<ul><li><strong>Target version</strong> set to <i>3.0</i></li></ul> Ruby master - Feature #17104: Do not freeze interpolated strings when using frozen-string-literalhttps://redmine.ruby-lang.org/issues/17104?journal_id=892462020-12-16T17:34:01Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Is duplicate of</strong> <i><a class="issue tracker-5 status-5 priority-4 priority-default closed" href="/issues/16047">Misc #16047</a>: Reconsider impact of frozen_string_literal on dynamic strings</i> added</li></ul>