Skip to content

Commit b1311cb

Browse files
committed
gc: fix parenthesization check
Cannot use paren field in Node because all instances of a given symbol name use the same Node. Fixes #1022. R=ken2 CC=golang-dev https://golang.org/cl/2015043
1 parent 7ddbe79 commit b1311cb

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/cmd/gc/go.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ struct Node
209209
uchar dodata; // compile literal assignment as data statement
210210
uchar used;
211211
uchar isddd;
212-
uchar paren; // was parenthesized
213212
uchar pun; // dont registerize variable ONAME
214213

215214
// most nodes
@@ -411,6 +410,7 @@ enum
411410
OTINTER,
412411
OTFUNC,
413412
OTARRAY,
413+
OTPAREN,
414414

415415
// misc
416416
ODDD,

src/cmd/gc/go.y

+20-15
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
%type <node> name_or_type non_expr_type
5959
%type <node> new_name dcl_name oexpr typedclname
6060
%type <node> onew_name
61-
%type <node> osimple_stmt pexpr
61+
%type <node> osimple_stmt pexpr pexpr_no_paren
6262
%type <node> pseudocall range_stmt select_stmt
6363
%type <node> simple_stmt
6464
%type <node> switch_stmt uexpr
@@ -812,7 +812,7 @@ pseudocall:
812812
$$->list = $3;
813813
}
814814

815-
pexpr:
815+
pexpr_no_paren:
816816
LLITERAL
817817
{
818818
$$ = nodlit($1);
@@ -829,11 +829,6 @@ pexpr:
829829
}
830830
$$ = nod(OXDOT, $1, newname($3));
831831
}
832-
| '(' expr_or_type ')'
833-
{
834-
$$ = $2;
835-
$$->paren++;
836-
}
837832
| pexpr '.' '(' expr_or_type ')'
838833
{
839834
$$ = nod(ODOTTYPE, $1, $4);
@@ -873,16 +868,28 @@ pexpr:
873868
if($2 == LBODY)
874869
loophack = 1;
875870
}
876-
| pexpr '{' braced_keyval_list '}'
871+
| pexpr_no_paren '{' braced_keyval_list '}'
877872
{
878-
if($1->paren)
879-
yyerror("cannot parenthesize type in composite literal");
880873
// composite expression
881874
$$ = nod(OCOMPLIT, N, $1);
882875
$$->list = $3;
883876
}
877+
| '(' expr_or_type ')' '{' braced_keyval_list '}'
878+
{
879+
yyerror("cannot parenthesize type in composite literal");
880+
// composite expression
881+
$$ = nod(OCOMPLIT, N, $2);
882+
$$->list = $5;
883+
}
884884
| fnliteral
885885

886+
pexpr:
887+
pexpr_no_paren
888+
| '(' expr_or_type ')'
889+
{
890+
$$ = $2;
891+
}
892+
886893
expr_or_type:
887894
expr
888895
| non_expr_type %prec PreferToRightParen
@@ -965,8 +972,7 @@ ntype:
965972
| dotname
966973
| '(' ntype ')'
967974
{
968-
$$ = $2;
969-
$$->paren++;
975+
$$ = nod(OTPAREN, $2, N);
970976
}
971977

972978
non_expr_type:
@@ -985,8 +991,7 @@ non_recvchantype:
985991
| dotname
986992
| '(' ntype ')'
987993
{
988-
$$ = $2;
989-
$$->paren++;
994+
$$ = nod(OTPAREN, $2, N);
990995
}
991996

992997
convtype:
@@ -1146,7 +1151,7 @@ fndcl:
11461151
yyerror("bad receiver in method");
11471152
break;
11481153
}
1149-
if(rcvr->right->paren || (rcvr->right->op == OIND && rcvr->right->left->paren))
1154+
if(rcvr->right->op == OTPAREN || (rcvr->right->op == OIND && rcvr->right->left->op == OTPAREN))
11501155
yyerror("cannot parenthesize receiver type");
11511156

11521157
$$ = nod(ODCLFUNC, N, N);

src/cmd/gc/print.c

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ exprfmt(Fmt *f, Node *n, int prec)
6666
case OPLUS:
6767
case ORECV:
6868
case OCONVIFACE:
69+
case OTPAREN:
6970
nprec = 7;
7071
break;
7172

@@ -165,6 +166,12 @@ exprfmt(Fmt *f, Node *n, int prec)
165166
fmtprint(f, "[]");
166167
exprfmt(f, n->left, PFIXME);
167168
break;
169+
170+
case OTPAREN:
171+
fmtprint(f, "(");
172+
exprfmt(f, n->left, 0);
173+
fmtprint(f, ")");
174+
break;
168175

169176
case OTMAP:
170177
fmtprint(f, "map[");

src/cmd/gc/typecheck.c

+10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ typecheck(Node **np, int top)
169169
goto error;
170170
break;
171171

172+
case OTPAREN:
173+
ok |= Etype;
174+
l = typecheck(&n->left, Etype);
175+
if(l->type == T)
176+
goto error;
177+
n->op = OTYPE;
178+
n->type = l->type;
179+
n->left = N;
180+
break;
181+
172182
case OTARRAY:
173183
ok |= Etype;
174184
t = typ(TARRAY);

0 commit comments

Comments
 (0)