Bug 23744 – Bug/Edge case - Shorthand static if/foreach + case labels causes logical but unexpected behaviour

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Mac OS X
Creation time
2023-02-26T16:47:23Z
Last change time
2024-12-13T19:27:26Z
Assigned to
No Owner
Creator
Bradley Chatha
Moved to GitHub: dmd#20237 →

Comments

Comment #0 by bradley — 2023-02-26T16:47:23Z
I'm uncertain whether this issue is mainly the "Attribute inference on recursion" bug or instead another bug that is only manifesting itself alongside the other bug; but essentially: ``` module test; bool nextVaryingLengthToken() @safe { bool tryLexLongerOperators(alias TokenType)() { switch(' ') { /* Comment out the first two blocks and you will see that the error dissapears. The only difference between the two sets of blocks is that one has braces and the other doesn't. Additionally, commenting out the line `int i` will cause the error to not appear. It appears having _any_ sort of statement there causes the error to show up. */ static if(false) case "=": int i; if(tryLexLongerOperators!"=") return true; return true; static if(false) case "*": int i3; if(tryLexLongerOperators!"*") return true; return true; static if(false) { case "=": int i2; if(tryLexLongerOperators!"=") return true; return true; case "*": int i4; if(tryLexLongerOperators!"*") return true; return true; } default: return false; } } return tryLexLongerOperators!noreturn; } ``` It seems that `static if(false) <body>` will still cause the compiler to try and evaluate the <body>, however `static if(false) { <body> }` causes it to not bother. And then since it evaluates the body it triggers the inferrence bug, which is what makes me think this is a separate bug on its own. However that's more just an observation rather than a truthful assertion; I'm struggling to reproduce this outside of this particular setup.
Comment #1 by bradley — 2023-03-04T20:18:29Z
Ok, it definitely seems to be its own seperate bug: https://godbolt.org/z/xKWz1h8dT
Comment #2 by bradley — 2023-04-03T07:17:23Z
After some thought and tinkering I don't think this is actually a bug but more an edgecase. ``` static if(false); ```
Comment #3 by bradley — 2023-04-03T07:20:15Z
Apologies, I forget using tab in a browser text editor is generally unwise :D Anyway: ``` static if(false) case 0: yada... break; ``` The static if (also works for static foreach) only attached itself to the resulting `case 0:` AST Node; leaving the 'body' of the case in some strange limbo state, causing semantic errors. Which fully explains some of the weirdness displayed, especially why brackets removes the issue.
Comment #4 by bradley — 2023-04-03T07:28:41Z
Minimal reproduction: ``` void main() { switch(0) { static if (false) case 0: int a; a = 0; break; default: break; } } ``` I guess case labels also directly attach onto the next statement; which explains some other behaviour I've seen (namely the attached statement being omitted).
Comment #5 by robert.schadek — 2024-12-13T19:27:26Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20237 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB