1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

parse.y: non-local/const attrset

* parse.y (rb_enc_symname_type): allow ID_ATTRSET for ID_INSTANCE,
  ID_GLOBAL, ID_CLASS, ID_JUNK too.  [Bug #8756]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42509 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-08-11 06:11:17 +00:00
parent dc548ba04e
commit c6282e9bf9
3 changed files with 29 additions and 13 deletions

View file

@ -1,3 +1,8 @@
Sun Aug 11 15:10:40 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (rb_enc_symname_type): allow ID_ATTRSET for ID_INSTANCE,
ID_GLOBAL, ID_CLASS, ID_JUNK too. [Bug #8756]
Sun Aug 11 13:17:00 2013 Charlie Somerville <charliesome@ruby-lang.org>
* include/ruby/encoding.h: Reduce ENCODING_INLINE_MAX to 127 as this

29
parse.y
View file

@ -10182,8 +10182,11 @@ rb_enc_symname_p(const char *name, rb_encoding *enc)
return rb_enc_symname2_p(name, strlen(name), enc);
}
#define IDSET_ATTRSET_FOR_SYNTAX ((1U<<ID_LOCAL)|(1U<<ID_CONST))
#define IDSET_ATTRSET_FOR_INTERN (~(~0U<<ID_SCOPE_MASK) & ~(1U<<ID_ATTRSET))
static int
rb_enc_symname_type(const char *name, long len, rb_encoding *enc)
rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_atttset)
{
const char *m = name;
const char *e = m + len;
@ -10267,7 +10270,7 @@ rb_enc_symname_type(const char *name, long len, rb_encoding *enc)
++m;
break;
case '=':
if (type != ID_CONST && type != ID_LOCAL) return -1;
if (!(allowed_atttset & (1U << type))) return -1;
type = ID_ATTRSET;
++m;
break;
@ -10280,15 +10283,15 @@ rb_enc_symname_type(const char *name, long len, rb_encoding *enc)
int
rb_enc_symname2_p(const char *name, long len, rb_encoding *enc)
{
return rb_enc_symname_type(name, len, enc) != -1;
return rb_enc_symname_type(name, len, enc, IDSET_ATTRSET_FOR_SYNTAX) != -1;
}
static int
rb_str_symname_type(VALUE name)
rb_str_symname_type(VALUE name, unsigned int allowed_atttset)
{
const char *ptr = StringValuePtr(name);
long len = RSTRING_LEN(name);
int type = rb_enc_symname_type(ptr, len, rb_enc_get(name));
int type = rb_enc_symname_type(ptr, len, rb_enc_get(name), allowed_atttset);
RB_GC_GUARD(name);
return type;
}
@ -10719,43 +10722,43 @@ rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
int
rb_is_const_name(VALUE name)
{
return rb_str_symname_type(name) == ID_CONST;
return rb_str_symname_type(name, 0) == ID_CONST;
}
int
rb_is_class_name(VALUE name)
{
return rb_str_symname_type(name) == ID_CLASS;
return rb_str_symname_type(name, 0) == ID_CLASS;
}
int
rb_is_global_name(VALUE name)
{
return rb_str_symname_type(name) == ID_GLOBAL;
return rb_str_symname_type(name, 0) == ID_GLOBAL;
}
int
rb_is_instance_name(VALUE name)
{
return rb_str_symname_type(name) == ID_INSTANCE;
return rb_str_symname_type(name, 0) == ID_INSTANCE;
}
int
rb_is_attrset_name(VALUE name)
{
return rb_str_symname_type(name) == ID_ATTRSET;
return rb_str_symname_type(name, IDSET_ATTRSET_FOR_INTERN) == ID_ATTRSET;
}
int
rb_is_local_name(VALUE name)
{
return rb_str_symname_type(name) == ID_LOCAL;
return rb_str_symname_type(name, 0) == ID_LOCAL;
}
int
rb_is_method_name(VALUE name)
{
switch (rb_str_symname_type(name)) {
switch (rb_str_symname_type(name, 0)) {
case ID_LOCAL: case ID_ATTRSET: case ID_JUNK:
return TRUE;
}
@ -10765,7 +10768,7 @@ rb_is_method_name(VALUE name)
int
rb_is_junk_name(VALUE name)
{
return rb_str_symname_type(name) == -1;
return rb_str_symname_type(name, IDSET_ATTRSET_FOR_SYNTAX) == -1;
}
#endif /* !RIPPER */

View file

@ -131,7 +131,15 @@ class TestStruct < Test::Unit::TestCase
klass = Struct.new(:@a)
o = klass.new(1)
assert_equal(1, o.__send__(:@a))
assert_equal("#<struct :@a=1>", o.inspect)
o.__send__(:"@a=", 2)
assert_equal(2, o.__send__(:@a))
assert_equal("#<struct :@a=2>", o.inspect)
o.__send__("@a=", 3)
assert_equal(3, o.__send__(:@a))
assert_equal("#<struct :@a=3>", o.inspect)
methods = klass.instance_methods(false)
assert_equal([:@a, :"@a="].inspect, methods.inspect, '[Bug #8756]')
assert_include(methods, :@a)