https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-07-03T08:28:49ZRuby Issue Tracking SystemRuby master - Bug #11326: Defining a writer as a Struct member allowed?https://bugs.ruby-lang.org/issues/11326?journal_id=532502015-07-03T08:28:49Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:peter-rl@suschlik.de" class="email">peter-rl@suschlik.de</a> wrote:</p>
<blockquote>
<p>I don't have a strong opinion whether defining a writer member should<br>
be possible or not.</p>
</blockquote>
<p>I suggest banning it (barely-tested patch):</p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gd">--- a/struct.c
</span><span class="gi">+++ b/struct.c
</span><span class="p">@@ -12,6 +12,7 @@</span>
#include "internal.h"
#include "vm_core.h"
#include "id.h"
<span class="gi">+#include "symbol.h"
</span>
/* only for struct[:field] access */
#define AREF_HASH_THRESHOLD (10)
<span class="p">@@ -510,6 +511,10 @@</span> rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
rest = rb_ary_tmp_new(argc);
for (i=0; i<argc; i++) {
id = rb_to_id(argv[i]);
<span class="gi">+ if (is_attrset_id(id)) {
+ rb_raise(rb_eArgError, "invalid struct member: %+"PRIsVALUE,
+ argv[i]);
+ }
</span> RARRAY_ASET(rest, i, ID2SYM(id));
rb_ary_set_len(rest, i+1);
}
</code></pre>
<pre><code>$ ./ruby -e 'Struct.new(:foo=)'
-e:1:in `new': invalid struct member: :foo= (ArgumentError)
from -e:1:in `<main>'
</code></pre> Ruby master - Bug #11326: Defining a writer as a Struct member allowed?https://bugs.ruby-lang.org/issues/11326?journal_id=532562015-07-03T09:25:42Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/53256/diff?detail_id=38348">diff</a>)</li></ul><p>Eric Wong wrote:</p>
<blockquote>
<p>I suggest banning it (barely-tested patch):</p>
</blockquote>
<p>+1</p> Ruby master - Bug #11326: Defining a writer as a Struct member allowed?https://bugs.ruby-lang.org/issues/11326?journal_id=532742015-07-04T04:38:45Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:nobu@ruby-lang.org" class="email">nobu@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>Eric Wong wrote:</p>
<blockquote>
<p>I suggest banning it (barely-tested patch):</p>
</blockquote>
<p>+1</p>
</blockquote>
<p>OK with matz to commit my patch? (+ tests)?</p>
<p>Also, what about '?' and '!' in field names?<br>
Can we continue to allow those?<br>
I guess the following work in existing code:</p>
<pre><code>s[:foo?] = :bar
s[:foo!] = :bar
</code></pre> Ruby master - Bug #11326: Defining a writer as a Struct member allowed?https://bugs.ruby-lang.org/issues/11326?journal_id=791332019-07-05T17:10:17Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>File</strong> <a href="/attachments/7880">struct-ban-attrset-id.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/7880/struct-ban-attrset-id.patch">struct-ban-attrset-id.patch</a> added</li></ul><p>This issue still exists in the master branch. Eric's patch no longer applies, but attached is a similar approach that works and includes a test. However, maybe there was a reason this wasn't committed originally?</p> Ruby master - Bug #11326: Defining a writer as a Struct member allowed?https://bugs.ruby-lang.org/issues/11326?journal_id=812382019-08-29T04:32:26Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Accepted.</p>
<p>Matz.</p> Ruby master - Bug #11326: Defining a writer as a Struct member allowed?https://bugs.ruby-lang.org/issues/11326?journal_id=812732019-08-29T15:23:40Zjeremyevans (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="Disallow use of attrset symbols as Struct members Fixes [Bug #11326]" href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/e51dca2596db9567bd4d698b18b4d300575d3881">git|e51dca2596db9567bd4d698b18b4d300575d3881</a>.</p>
<hr>
<p>Disallow use of attrset symbols as Struct members</p>
<p>Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Defining a writer as a Struct member allowed? (Closed)" href="https://bugs.ruby-lang.org/issues/11326">#11326</a>]</p>