From 0dd8ec36c0533f1b63f222bcf0ff91e09e5b05dc Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 10 Aug 2013 04:53:31 +0000 Subject: parse.y: non-local/const attrset * parse.y (rb_id_attrset): allow other than ID_ATTRSET. * parse.y (intern_str): ditto. try stem ID for ID_INSTANCE, ID_GLOBAL, ID_CLASS, ID_JUNK too. [Bug #8756] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42490 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ parse.y | 52 +++++++++++++++++++++++++++++------------------- test/ruby/test_struct.rb | 4 ++++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 84fc777771..71c12a4489 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat Aug 10 13:53:22 2013 Nobuyoshi Nakada + + * parse.y (rb_id_attrset): allow other than ID_ATTRSET. + + * parse.y (intern_str): ditto. try stem ID for ID_INSTANCE, + ID_GLOBAL, ID_CLASS, ID_JUNK too. [Bug #8756] + Sat Aug 10 12:49:50 2013 Kouhei Sutou * lib/rexml/parsers/baseparser.rb diff --git a/parse.y b/parse.y index 8a31a74158..f5e79c18e7 100644 --- a/parse.y +++ b/parse.y @@ -8785,7 +8785,11 @@ rb_id_attrset(ID id) } else { int scope = (int)(id & ID_SCOPE_MASK); - if (scope != ID_LOCAL && scope != ID_CONST) { + switch (scope) { + case ID_LOCAL: case ID_INSTANCE: case ID_GLOBAL: + case ID_CONST: case ID_CLASS: case ID_JUNK: + break; + default: rb_bug("rb_id_attrset: %s ID - %"PRIdVALUE, id_type_names[scope], (VALUE)id); @@ -10415,24 +10419,25 @@ intern_str(VALUE str) } } } - - if (m[last] == '=') { - /* attribute assignment */ - id = rb_intern3(name, last, enc); - if (id > tLAST_OP_ID && !is_attrset_id(id)) { - enc = rb_enc_get(rb_id2str(id)); - id = rb_id_attrset(id); - goto id_register; - } - id = ID_ATTRSET; + break; + } + if (name[last] == '=') { + /* attribute assignment */ + id = rb_intern3(name, last, enc); + if (id > tLAST_OP_ID && !is_attrset_id(id)) { + enc = rb_enc_get(rb_id2str(id)); + id = rb_id_attrset(id); + goto id_register; } - else if (rb_enc_isupper(m[0], enc)) { + id = ID_ATTRSET; + } + else if (id == 0) { + if (rb_enc_isupper(m[0], enc)) { id = ID_CONST; - } + } else { id = ID_LOCAL; } - break; } if (!rb_enc_isdigit(*m, enc)) { while (m <= name + last && is_identchar(m, e, enc)) { @@ -10444,7 +10449,7 @@ intern_str(VALUE str) } } } - if (m - name < len) id = ID_JUNK; + if (id != ID_ATTRSET && m - name < len) id = ID_JUNK; if (sym_check_asciionly(str)) symenc = rb_usascii_encoding(); new_id: if (symenc != enc) rb_enc_associate(str, symenc); @@ -10527,16 +10532,21 @@ rb_id2str(ID id) } if (is_attrset_id(id)) { - ID id2 = (id & ~ID_SCOPE_MASK) | ID_LOCAL; + ID id_stem = (id & ~ID_SCOPE_MASK); VALUE str; - while (!(str = rb_id2str(id2))) { - if (!is_local_id(id2)) return 0; - id2 = (id & ~ID_SCOPE_MASK) | ID_CONST; - } + do { + if (!!(str = rb_id2str(id_stem | ID_LOCAL))) break; + if (!!(str = rb_id2str(id_stem | ID_CONST))) break; + if (!!(str = rb_id2str(id_stem | ID_INSTANCE))) break; + if (!!(str = rb_id2str(id_stem | ID_GLOBAL))) break; + if (!!(str = rb_id2str(id_stem | ID_CLASS))) break; + if (!!(str = rb_id2str(id_stem | ID_JUNK))) break; + return 0; + } while (0); str = rb_str_dup(str); rb_str_cat(str, "=", 1); - rb_intern_str(str); + register_symid_str(id, str); if (st_lookup(global_symbols.id_str, id, &data)) { VALUE str = (VALUE)data; if (RBASIC(str)->klass == 0) diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index c5ac2eb330..677452879c 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -132,6 +132,10 @@ class TestStruct < Test::Unit::TestCase klass = Struct.new(:@a) o = klass.new(1) assert_equal("#", o.inspect) + methods = klass.instance_methods(false) + assert_equal([:@a, :"@a="].inspect, methods.inspect, '[Bug #8756]') + assert_include(methods, :@a) + assert_include(methods, :"@a=") end def test_init_copy -- cgit v1.2.3