Skip to content

Commit 0e3e0f9

Browse files
authored
fix Issue 22913 - importC: array index expression parsed as cast (#13880)
1 parent ef5331e commit 0e3e0f9

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/dmd/cparse.d

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,10 +1021,16 @@ final class CParser(AST) : Parser!AST
10211021
{
10221022
if (token.value == TOK.leftParenthesis)
10231023
{
1024+
//printf("cparseCastExp()\n");
10241025
auto tk = peek(&token);
1025-
if (tk.value == TOK.identifier &&
1026-
!isTypedef(tk.ident) &&
1027-
peek(tk).value == TOK.rightParenthesis)
1026+
bool iscast;
1027+
bool isexp;
1028+
if (tk.value == TOK.identifier)
1029+
{
1030+
iscast = isTypedef(tk.ident);
1031+
isexp = !iscast;
1032+
}
1033+
if (isexp)
10281034
{
10291035
// ( identifier ) is an expression
10301036
return cparseUnaryExp();
@@ -1050,10 +1056,18 @@ final class CParser(AST) : Parser!AST
10501056
auto ce = new AST.CompoundLiteralExp(loc, t, ci);
10511057
return cparsePostfixOperators(ce);
10521058
}
1053-
else if (t.isTypeIdentifier() &&
1054-
!isTypedef(t.isTypeIdentifier().ident) &&
1055-
token.value == TOK.leftParenthesis &&
1056-
!isCastExpression(pt))
1059+
1060+
if (iscast)
1061+
{
1062+
// ( type-name ) cast-expression
1063+
auto ce = cparseCastExp();
1064+
return new AST.CastExp(loc, ce, t);
1065+
}
1066+
1067+
if (t.isTypeIdentifier() &&
1068+
isexp &&
1069+
token.value == TOK.leftParenthesis &&
1070+
!isCastExpression(pt))
10571071
{
10581072
/* (t)(...)... might be a cast expression or a function call,
10591073
* with different grammars: a cast would be cparseCastExp(),
@@ -1067,12 +1081,10 @@ final class CParser(AST) : Parser!AST
10671081
AST.Expression e = new AST.CallExp(loc, ie, cparseArguments());
10681082
return cparsePostfixOperators(e);
10691083
}
1070-
else
1071-
{
1072-
// ( type-name ) cast-expression
1073-
auto ce = cparseCastExp();
1074-
return new AST.CastExp(loc, ce, t);
1075-
}
1084+
1085+
// ( type-name ) cast-expression
1086+
auto ce = cparseCastExp();
1087+
return new AST.CastExp(loc, ce, t);
10761088
}
10771089
}
10781090
return cparseUnaryExp();

test/compilable/test22904.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ void fn2() { long x = (long) (fn1) (); }
77

88
typedef long my_long;
99
void fn3() { long x = (my_long) (fn1) (); }
10+
11+
// https://issues.dlang.org/show_bug.cgi?id=22913
12+
13+
void fn4()
14+
{
15+
int a[1];
16+
int b = (a[0]);
17+
}

0 commit comments

Comments
 (0)