summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-26 08:25:48 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-26 08:25:48 +0000
commiteab191040e9356a8ed4aaa418a7904d6f94064b9 (patch)
treed3b3b15e3f380c272e855cd320506d51296d7620
parentc7855be25c379206a1bddd2bbd76e11cc642c8f8 (diff)
parse.y: fix invalid keyword argument
* parse.y (f_label, f_kw, formal_argument_gen): ignore invalid formal argument in keyword argument definition. [ruby-dev:48742] [Bug #10545] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--parse.y36
-rw-r--r--test/ruby/test_syntax.rb6
3 files changed, 40 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index e02dadc..bbe64dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Nov 26 17:25:45 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * parse.y (f_label, f_kw, formal_argument_gen): ignore invalid
+ formal argument in keyword argument definition.
+ [ruby-dev:48742] [Bug #10545]
+
Wed Nov 26 15:32:06 2014 Koichi Sasada <ko1@atdot.net>
* compile.c (iseq_set_sequence): use "nop" insn instead of
diff --git a/parse.y b/parse.y
index 00d85e0..0aacaa3 100644
--- a/parse.y
+++ b/parse.y
@@ -411,6 +411,7 @@ static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*);
#define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
static NODE *new_args_tail_gen(struct parser_params*,NODE*,ID,ID);
#define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
+#define new_kw_arg(k) ((k) ? NEW_KW_ARG(0, (k)) : 0)
static VALUE negate_lit(VALUE);
static NODE *ret_args_gen(struct parser_params*,NODE*);
@@ -4715,9 +4716,9 @@ f_arg : f_arg_item
f_label : tLABEL
{
ID id = get_id($1);
- arg_var(formal_argument(id));
+ $$ = formal_argument(id);
+ arg_var($$);
current_arg = id;
- $$ = $1;
}
;
@@ -4726,7 +4727,7 @@ f_kw : f_label arg_value
current_arg = 0;
$$ = assignable($1, $2);
/*%%%*/
- $$ = NEW_KW_ARG(0, $$);
+ $$ = new_kw_arg($$);
/*%
$$ = rb_assoc_new($$, $2);
%*/
@@ -4736,7 +4737,7 @@ f_kw : f_label arg_value
current_arg = 0;
$$ = assignable($1, (NODE *)-1);
/*%%%*/
- $$ = NEW_KW_ARG(0, $$);
+ $$ = new_kw_arg($$);
/*%
$$ = rb_assoc_new($$, 0);
%*/
@@ -4747,7 +4748,7 @@ f_block_kw : f_label primary_value
{
$$ = assignable($1, $2);
/*%%%*/
- $$ = NEW_KW_ARG(0, $$);
+ $$ = new_kw_arg($$);
/*%
$$ = rb_assoc_new($$, $2);
%*/
@@ -4756,7 +4757,7 @@ f_block_kw : f_label primary_value
{
$$ = assignable($1, (NODE *)-1);
/*%%%*/
- $$ = NEW_KW_ARG(0, $$);
+ $$ = new_kw_arg($$);
/*%
$$ = rb_assoc_new($$, 0);
%*/
@@ -6746,10 +6747,29 @@ arg_ambiguous_gen(struct parser_params *parser, char c)
static ID
formal_argument_gen(struct parser_params *parser, ID lhs)
{
+ switch (id_type(lhs)) {
+ case ID_LOCAL:
+ break;
#ifndef RIPPER
- if (!is_local_id(lhs))
- yyerror("formal argument must be local variable");
+ case ID_CONST:
+ yyerror("formal argument cannot be a constant");
+ return 0;
+ case ID_INSTANCE:
+ yyerror("formal argument cannot be an instance variable");
+ return 0;
+ case ID_GLOBAL:
+ yyerror("formal argument cannot be a global variable");
+ return 0;
+ case ID_CLASS:
+ yyerror("formal argument cannot be a class variable");
+ return 0;
+#else
+ default:
+ lhs = dispatch1(param_error, lhs);
+ ripper_error();
+ return 0;
#endif
+ }
shadowing_lvar(lhs);
return lhs;
}
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 00e2160..d36cbb0 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -530,6 +530,12 @@ eom
assert_syntax_error(code, /def n "\u{2208}"; end/, bug10114)
end
+ def test_bad_kwarg
+ bug10545 = '[ruby-dev:48742] [Bug #10545]'
+ src = 'def foo(A: a) end'
+ assert_syntax_error(src, /formal argument/, bug10545)
+ end
+
private
def not_label(x) @result = x; @not_label ||= nil end