Permalink
Browse files

Add more precise void expression checks; ref #3686

  • Loading branch information...
matz committed Jun 2, 2017
1 parent e0fbf25 commit b59d244c0db89ca87ddba1c94eb59e491d65a058
Showing with 40 additions and 2 deletions.
  1. +40 −2 mrbgems/mruby-compiler/core/parse.y
@@ -356,34 +356,39 @@ new_alias(parser_state *p, mrb_sym a, mrb_sym b)
static node*
new_if(parser_state *p, node *a, node *b, node *c)
{
void_expr_error(p, a);
return list4((node*)NODE_IF, a, b, c);
}
/* (:unless cond then else) */
static node*
new_unless(parser_state *p, node *a, node *b, node *c)
{
void_expr_error(p, a);
return list4((node*)NODE_IF, a, c, b);
}
/* (:while cond body) */
static node*
new_while(parser_state *p, node *a, node *b)
{
void_expr_error(p, a);
return cons((node*)NODE_WHILE, cons(a, b));
}
/* (:until cond body) */
static node*
new_until(parser_state *p, node *a, node *b)
{
void_expr_error(p, a);
return cons((node*)NODE_UNTIL, cons(a, b));
}
/* (:for var obj body) */
static node*
new_for(parser_state *p, node *v, node *o, node *b)
{
void_expr_error(p, o);
return list4((node*)NODE_FOR, v, o, b);
}
@@ -394,6 +399,7 @@ new_case(parser_state *p, node *a, node *b)
node *n = list2((node*)NODE_CASE, a);
node *n2 = n;
void_expr_error(p, a);
while (n2->cdr) {
n2 = n2->cdr;
}
@@ -420,6 +426,7 @@ static node*
new_call(parser_state *p, node *a, mrb_sym b, node *c, int pass)
{
node *n = list4(nint(pass?NODE_CALL:NODE_SCALL), a, nsym(b), c);
void_expr_error(p, a);
NODE_LINENO(n, a);
return n;
}
@@ -515,6 +522,7 @@ new_dot3(parser_state *p, node *a, node *b)
static node*
new_colon2(parser_state *p, node *b, mrb_sym c)
{
void_expr_error(p, b);
return cons((node*)NODE_COLON2, cons(b, nsym(c)));
}
@@ -622,13 +630,15 @@ new_undef(parser_state *p, mrb_sym sym)
static node*
new_class(parser_state *p, node *c, node *s, node *b)
{
void_expr_error(p, s);
return list4((node*)NODE_CLASS, c, s, cons(locals_node(p), b));
}
/* (:sclass obj body) */
static node*
new_sclass(parser_state *p, node *o, node *b)
{
void_expr_error(p, o);
return list3((node*)NODE_SCLASS, o, cons(locals_node(p), b));
}
@@ -650,6 +660,7 @@ new_def(parser_state *p, mrb_sym m, node *a, node *b)
static node*
new_sdef(parser_state *p, node *o, mrb_sym m, node *a, node *b)
{
void_expr_error(p, o);
return list6((node*)NODE_SDEF, o, nsym(m), locals_node(p), a, b);
}
@@ -702,20 +713,23 @@ new_lambda(parser_state *p, node *a, node *b)
static node*
new_asgn(parser_state *p, node *a, node *b)
{
void_expr_error(p, b);
return cons((node*)NODE_ASGN, cons(a, b));
}
/* (:masgn mlhs=(pre rest post) mrhs) */
static node*
new_masgn(parser_state *p, node *a, node *b)
{
void_expr_error(p, b);
return cons((node*)NODE_MASGN, cons(a, b));
}
/* (:asgn lhs rhs) */
static node*
new_op_asgn(parser_state *p, node *a, mrb_sym op, node *b)
{
void_expr_error(p, b);
return list4((node*)NODE_OP_ASGN, a, nsym(op), b);
}
@@ -835,6 +849,7 @@ new_symbols(parser_state *p, node *a)
static node*
call_uni_op(parser_state *p, node *recv, const char *m)
{
void_expr_error(p, recv);
return new_call(p, recv, intern_cstr(m), 0, 1);
}
@@ -1404,7 +1419,6 @@ expr_value : expr
{
if (!$1) $$ = new_nil(p);
else {
void_expr_error(p, $1);
$$ = $1;
}
}
@@ -1416,6 +1430,9 @@ command_call : command
block_command : block_call
| block_call call_op2 operation2 command_args
{
$$ = new_call(p, $1, $3, $4, $2);
}
;
cmd_brace_block : tLBRACE_ARG
@@ -1661,6 +1678,7 @@ cpath : tCOLON3 cname
}
| primary_value tCOLON2 cname
{
void_expr_error(p, $1);
$$ = cons($1, nsym($3));
}
;
@@ -1931,12 +1949,12 @@ aref_args : none
arg_rhs : arg %prec tOP_ASGN
{
void_expr_error(p, $1);
$$ = $1;
}
| arg modifier_rescue arg
{
void_expr_error(p, $1);
void_expr_error(p, $3);
$$ = new_mod_rescue(p, $1, $3);
}
;
@@ -1972,6 +1990,7 @@ opt_call_args : none
call_args : command
{
void_expr_error(p, $1);
$$ = cons(list1($1), 0);
NODE_LINENO($$, $1);
}
@@ -3112,12 +3131,14 @@ f_opt_asgn : tIDENTIFIER '='
f_opt : f_opt_asgn arg
{
void_expr_error(p, $2);
$$ = cons(nsym($1), $2);
}
;
f_block_opt : f_opt_asgn primary_value
{
void_expr_error(p, $2);
$$ = cons(nsym($1), $2);
}
;
@@ -3230,22 +3251,28 @@ assocs : assoc
assoc : arg tASSOC arg
{
void_expr_error(p, $1);
void_expr_error(p, $3);
$$ = cons($1, $3);
}
| tLABEL arg
{
void_expr_error(p, $2);
$$ = cons(new_sym(p, $1), $2);
}
| tLABEL_END arg
{
void_expr_error(p, $2);
$$ = cons(new_sym(p, new_strsym(p, $1)), $2);
}
| tSTRING_BEG tLABEL_END arg
{
void_expr_error(p, $3);
$$ = cons(new_sym(p, new_strsym(p, $2)), $3);
}
| tSTRING_BEG string_rep tLABEL_END arg
{
void_expr_error(p, $4);
$$ = cons(new_dsym(p, push($2, $3)), $4);
}
;
@@ -3441,6 +3468,17 @@ void_expr_error(parser_state *p, node *n)
case NODE_RETRY:
yyerror(p, "void value expression");
break;
case NODE_AND:
case NODE_OR:
void_expr_error(p, n->cdr->car);
void_expr_error(p, n->cdr->cdr);
break;
case NODE_BEGIN:
while (n->cdr) {
n = n->cdr;
}
void_expr_error(p, n->car);
break;
default:
break;
}

0 comments on commit b59d244

Please sign in to comment.