Show the source line at an invalid class/instance variable

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67264 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2019-03-15 00:44:51 +00:00
Родитель bf4bcaf061
Коммит 3134b20a01
3 изменённых файлов: 14 добавлений и 5 удалений

Просмотреть файл

@ -7586,9 +7586,12 @@ parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
static enum yytokentype
parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
{
const char *ptr = p->lex.pcur;
enum yytokentype result = tIVAR;
register int c = nextc(p);
YYLTYPE loc;
p->lex.ptok = ptr - 1; /* from '@' */
newtok(p);
tokadd(p, '@');
if (c == '@') {
@ -7598,15 +7601,18 @@ parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
}
if (c == -1 || !parser_is_identchar(p)) {
pushback(p, c);
RUBY_SET_YYLLOC(loc);
if (result == tIVAR) {
compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
}
else {
compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
}
parser_show_error_line(p, &loc);
return 0;
}
else if (ISDIGIT(c)) {
RUBY_SET_YYLLOC(loc);
pushback(p, c);
if (result == tIVAR) {
compile_error(p, "`@%c' is not allowed as an instance variable name", c);
@ -7614,6 +7620,7 @@ parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
else {
compile_error(p, "`@@%c' is not allowed as a class variable name", c);
}
parser_show_error_line(p, &loc);
return 0;
}

Просмотреть файл

@ -246,7 +246,7 @@ class TestISeq < Test::Unit::TestCase
end
end
assert_equal([m1, e1.message], [m2, e2.message], feature11951)
e1, e2 = e1.message.lines
e1, *, e2 = e1.message.lines
assert_send([e1, :start_with?, __FILE__])
assert_send([e2, :start_with?, __FILE__])
end

Просмотреть файл

@ -720,13 +720,15 @@ x = __ENCODING__
end
def test_invalid_instance_variable
assert_raise(SyntaxError) { eval('@#') }
assert_raise(SyntaxError) { eval('@') }
pattern = /without identifiers is not allowed as an instance variable name/
assert_raise_with_message(SyntaxError, pattern) { eval('@%') }
assert_raise_with_message(SyntaxError, pattern) { eval('@') }
end
def test_invalid_class_variable
assert_raise(SyntaxError) { eval('@@1') }
assert_raise(SyntaxError) { eval('@@') }
pattern = /without identifiers is not allowed as a class variable name/
assert_raise_with_message(SyntaxError, pattern) { eval('@@%') }
assert_raise_with_message(SyntaxError, pattern) { eval('@@') }
end
def test_invalid_char