Skip to content

Commit bc3c423

Browse files
authored
Fix error location for binary operations to point at operator instead of first operand (dlang#21253)
1 parent 93d2e53 commit bc3c423

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

compiler/src/dmd/parse.d

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9068,11 +9068,11 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
90689068

90699069
private AST.Expression parseMulExp()
90709070
{
9071-
const loc = token.loc;
90729071
auto e = parseUnaryExp();
90739072

90749073
while (1)
90759074
{
9075+
const loc = token.loc;
90769076
switch (token.value)
90779077
{
90789078
case TOK.mul:
@@ -9103,11 +9103,11 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
91039103

91049104
private AST.Expression parseAddExp()
91059105
{
9106-
const loc = token.loc;
91079106
auto e = parseMulExp();
91089107

91099108
while (1)
91109109
{
9110+
const loc = token.loc;
91119111
switch (token.value)
91129112
{
91139113
case TOK.add:
@@ -9138,11 +9138,11 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
91389138

91399139
private AST.Expression parseShiftExp()
91409140
{
9141-
const loc = token.loc;
91429141
auto e = parseAddExp();
91439142

91449143
while (1)
91459144
{
9145+
const loc = token.loc;
91469146
switch (token.value)
91479147
{
91489148
case TOK.leftShift:
@@ -9173,10 +9173,9 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
91739173

91749174
private AST.Expression parseCmpExp()
91759175
{
9176-
const loc = token.loc;
9177-
91789176
auto e = parseShiftExp();
91799177
EXP op = EXP.reserved;
9178+
const loc = token.loc;
91809179

91819180
switch (token.value)
91829181
{
@@ -9238,28 +9237,26 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
92389237

92399238
private AST.Expression parseAndExp()
92409239
{
9241-
Loc loc = token.loc;
92429240
auto e = parseCmpExp();
92439241
while (token.value == TOK.and)
92449242
{
92459243
checkParens(TOK.and, e);
9244+
const loc = token.loc;
92469245
nextToken();
92479246
auto e2 = parseCmpExp();
92489247
checkParens(TOK.and, e2);
92499248
e = new AST.AndExp(loc, e, e2);
9250-
loc = token.loc;
92519249
}
92529250
return e;
92539251
}
92549252

92559253
private AST.Expression parseXorExp()
92569254
{
9257-
const loc = token.loc;
9258-
92599255
auto e = parseAndExp();
92609256
while (token.value == TOK.xor)
92619257
{
92629258
checkParens(TOK.xor, e);
9259+
const loc = token.loc;
92639260
nextToken();
92649261
auto e2 = parseAndExp();
92659262
checkParens(TOK.xor, e2);
@@ -9270,12 +9267,11 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
92709267

92719268
private AST.Expression parseOrExp()
92729269
{
9273-
const loc = token.loc;
9274-
92759270
auto e = parseXorExp();
92769271
while (token.value == TOK.or)
92779272
{
92789273
checkParens(TOK.or, e);
9274+
const loc = token.loc;
92799275
nextToken();
92809276
auto e2 = parseXorExp();
92819277
checkParens(TOK.or, e2);
@@ -9286,11 +9282,10 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
92869282

92879283
private AST.Expression parseAndAndExp()
92889284
{
9289-
const loc = token.loc;
9290-
92919285
auto e = parseOrExp();
92929286
while (token.value == TOK.andAnd)
92939287
{
9288+
const loc = token.loc;
92949289
nextToken();
92959290
auto e2 = parseOrExp();
92969291
e = new AST.LogicalExp(loc, EXP.andAnd, e, e2);
@@ -9300,11 +9295,10 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
93009295

93019296
private AST.Expression parseOrOrExp()
93029297
{
9303-
const loc = token.loc;
9304-
93059298
auto e = parseAndAndExp();
93069299
while (token.value == TOK.orOr)
93079300
{
9301+
const loc = token.loc;
93089302
nextToken();
93099303
auto e2 = parseAndAndExp();
93109304
e = new AST.LogicalExp(loc, EXP.orOr, e, e2);
@@ -9314,11 +9308,10 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
93149308

93159309
private AST.Expression parseCondExp()
93169310
{
9317-
const loc = token.loc;
9318-
93199311
auto e = parseOrOrExp();
93209312
if (token.value == TOK.question)
93219313
{
9314+
const loc = token.loc;
93229315
nextToken();
93239316
auto e1 = parseExpression();
93249317
check(TOK.colon);

compiler/test/fail_compilation/fail196.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fail_compilation/fail196.d(44): expression: `";\n assert(s == "`
2121
fail_compilation/fail196.d(45): Error: found `}` when expecting `;` following expression
2222
fail_compilation/fail196.d(45): expression: `xxx`
2323
fail_compilation/fail196.d(47): Error: found `<` when expecting `;` following expression
24-
fail_compilation/fail196.d(45): expression: `");\n\n s = q" < foo`
24+
fail_compilation/fail196.d(47): expression: `");\n\n s = q" < foo`
2525
fail_compilation/fail196.d(48): Error: found `foo` when expecting `;` following expression
2626
fail_compilation/fail196.d(47): expression: `xxx >> ";\n assert(s == "`
2727
fail_compilation/fail196.d(48): Error: found `<` instead of statement

compiler/test/fail_compilation/fail_pretty_errors.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fail_compilation/fail_pretty_errors.d(44): Error: mixin `fail_pretty_errors.test
1616
^
1717
fail_compilation/fail_pretty_errors.d(50): Error: invalid array operation `"" + ""` (possible missing [])
1818
auto x = ""+"";
19-
^
19+
^
2020
fail_compilation/fail_pretty_errors.d(50): did you mean to concatenate (`"" ~ ""`) instead ?
2121
fail_compilation/fail_pretty_errors.d(53): Error: cannot implicitly convert expression `1111` of type `int` to `byte`
2222
byte ɑ = 1111;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation/fix21166.d(12): Error: invalid array operation `"foo" + "bar"` (possible missing [])
5+
fail_compilation/fix21166.d(12): did you mean to concatenate (`"foo" ~ "bar"`) instead ?
6+
---
7+
*/
8+
9+
// Test case for https://github.com/dlang/dmd/issues/21166
10+
auto r =
11+
"foo"
12+
+
13+
"bar";

0 commit comments

Comments
 (0)