Issue 24802 - misleading "cannot `goto` into `try` block" when skipping variable with destructor
Summary: misleading "cannot `goto` into `try` block" when skipping variable with destr...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2024-10-09 13:49 UTC by Manu
Modified: 2024-10-11 08:45 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2024-10-09 13:49:55 UTC
struct S
{
    ~this() nothrow;
}

void test() nothrow
{
    goto skip;
    S r;
skip:
}


error : cannot `goto` into `try` block


test() is nothrow, S.~this() is nothrow. There should be no `try` blocks in sight... the message should be about skipping initialisation.
Comment 1 Dennis 2024-10-09 15:34:27 UTC
The compiler rewrites things that happen on scope exit (such as destructors) into try-finally blocks. The same happens with scope guards:
```
void main()
{
    goto x;    
    scope(exit) {}
    x:
}
```

See also issue 24300. The solution is probably to add a field to the lowered AST node keeping track of where a try-finally block came from.
Comment 2 Manu 2024-10-11 08:45:27 UTC
Is that efficient? Does that affect optimisation opportunities in any way?