Bug 23983 – Better error message when brace missing after `else`
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-06-09T23:51:20Z
Last change time
2023-06-19T07:46:44Z
Keywords
diagnostic
Assigned to
No Owner
Creator
ryuukk_
Comments
Comment #0 by ryuukk.dev — 2023-06-09T23:51:20Z
The following code:
```
void main()
{
int stuff = 0;
version (Poxis)
{
stuff++;
}
else
stuff++;
}
}
```
gives:
```
onlineapp.d(14): Error: unmatched closing brace
```
The problem with that error message is the scope is not respected, so if you have bunch of code aafter that missing `{`, then it'll be VERY difficult to guess where the problem exactly is
Solution:
DMD should tell me where the problem is right away, at the right scope
Comment #1 by ryuukk.dev — 2023-06-10T00:09:39Z
Example 2:
```
void main()
{
int stuff = 0;
version (Poxis)
{
stuff++;
}
else
stuff++;
}
version (Poxis)
{
stuff++;
}
else
{
stuff++;
}
}
```
```
onlineapp.d(15): Error: no identifier for declarator `stuff`
onlineapp.d(15): Error: declaration expected, not `++`
onlineapp.d(19): Error: no identifier for declarator `stuff`
onlineapp.d(19): Error: declaration expected, not `++`
onlineapp.d(21): Error: unmatched closing brace
```
Error message is misleading now
Comment #2 by dkorpel — 2023-06-11T22:06:15Z
> The problem with that error message is the scope is not respected
What does 'respecting the scope' mean? Are you talking about indentation?
Comment #3 by ryuukk.dev — 2023-06-12T01:58:16Z
I don't know what the right wording is called
In example 2, it complains at bunch of things, then at the end it says closing braces at line 21; but the problem is actually at line 8
Now imagine if you have lot more code in between, have fun guessing where the problem really is, reason why i opened this issue is because it just happened to me, it's not fun
Comment #4 by dkorpel — 2023-06-12T15:15:44Z
(In reply to ryuukk_ from comment #3)
> In example 2, it complains at bunch of things, then at the end it says
> closing braces at line 21; but the problem is actually at line 8
A human could guess the problem is at line 8, but formally, the else branch is not required to have braces, and the } after the else branch could close the main function.
> Now imagine if you have lot more code in between, have fun guessing where
> the problem really is, reason why i opened this issue is because it just
> happened to me, it's not fun
I understand and agree, I'm just wondering how the compiler could algorithmically find out the problem is likely at line 8. Perhaps it could take note of the mismatched indentation of the closing brace after else, and report that once it encounters a missing brace error.
Comment #5 by ryuukk.dev — 2023-06-12T21:13:22Z
> A human could guess the problem is at line 8, but formally, the else branch is not required to have braces, and the } after the else branch could close the main function.
You don't understand, let me bring you my project and the problem that led me to create this issue
https://gist.github.com/ryuukk/a4580ef154bf7801670e02a13f760858
```
projects/game/context.d(566,1): Error: unmatched closing brace
```
Try to find where the problem is
> I understand and agree, I'm just wondering how the compiler could algorithmically find out the problem is likely at line 8. Perhaps it could take note of the mismatched indentation of the closing brace after else, and report that once it encounters a missing brace error.
I don't know, i am not a compiler developer, my role is to report issues i encounter
Comment #6 by razvan.nitu1305 — 2023-06-13T07:33:00Z
(In reply to ryuukk_ from comment #5)
> > A human could guess the problem is at line 8, but formally, the else branch is not required to have braces, and the } after the else branch could close the main function.
>
> You don't understand, let me bring you my project and the problem that led
> me to create this issue
>
> https://gist.github.com/ryuukk/a4580ef154bf7801670e02a13f760858
>
>
> ```
> projects/game/context.d(566,1): Error: unmatched closing brace
> ```
>
>
> Try to find where the problem is
>
>
>
> > I understand and agree, I'm just wondering how the compiler could algorithmically find out the problem is likely at line 8. Perhaps it could take note of the mismatched indentation of the closing brace after else, and report that once it encounters a missing brace error.
>
> I don't know, i am not a compiler developer, my role is to report issues i
> encounter
You are right for complaining about this. However, what Dennis is pointing out is that this code is perfectly fine:
void main()
{
int stuff = 0;
version (Posix)
{
stuff++;
}
else
stuff++;
}
So, the compiler parses this and has no complaints. Next, you add another brace:
void main()
{
int stuff = 0;
version (Posix)
{
stuff++;
}
else
stuff++;
}
}
The compiler sees this code as being valid up until the last brace. Then it tries to consume the next token which is another curly brace so it just errors and says that you have an unmatched closing brace. In this scenario, it is impossible for the compiler to go back and rescan previous tokens to try and better understand how the braces were meant to be used. Even if it did, your code up to the problematic last brace is perfectly valid. Moreover, look at C:
void main()
{
int a;
if (1)
{}
else
a = 2;
}
} // line 9
This code yields:
gcc: test.c:9:1: error: expected identifier or ‘(’ before ‘}’ token
clang: test.c:9:1: error: extraneous closing brace ('}')
Which is the same thing the D compiler is complaining about.
So, although I understand your point, as Dennis pointed out there is no algorithmic way in which what you are asking for can be done.
Comment #7 by default_357-line — 2023-06-19T07:46:44Z
I recommend using a tool like dfmt that has opinions about correct indentation. Then you'll easily notice on commit that it's indented everything from line 11 on leftwards, which makes it clear where the problem is.