Thread overview
[Issue 23983] Better error message when brace missing after `else`
Jun 09, 2023
ryuukk_
Jun 10, 2023
ryuukk_
Jun 11, 2023
Dennis
Jun 12, 2023
ryuukk_
Jun 12, 2023
Dennis
Jun 12, 2023
ryuukk_
Jun 13, 2023
RazvanN
Jun 19, 2023
RazvanN
Jun 19, 2023
FeepingCreature
June 09, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

ryuukk_ <ryuukk.dev@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--
June 10, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

--- Comment #1 from ryuukk_ <ryuukk.dev@gmail.com> ---
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

--
June 11, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl
           Hardware|x86_64                      |All

--- Comment #2 from Dennis <dkorpel@live.nl> ---
> The problem with that error message is the scope is not respected

What does 'respecting the scope' mean? Are you talking about indentation?

--
June 12, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

--- Comment #3 from ryuukk_ <ryuukk.dev@gmail.com> ---
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

--
June 12, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

--- Comment #4 from Dennis <dkorpel@live.nl> ---
(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.

--
June 12, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

--- Comment #5 from ryuukk_ <ryuukk.dev@gmail.com> ---
> 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

--
June 13, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #6 from RazvanN <razvan.nitu1305@gmail.com> ---
(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.

--
June 19, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX

--
June 19, 2023
https://issues.dlang.org/show_bug.cgi?id=23983

FeepingCreature <default_357-line@yahoo.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |default_357-line@yahoo.de

--- Comment #7 from FeepingCreature <default_357-line@yahoo.de> ---
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.

--