Bug 7386 – Can't use a 'version' that was set inside 'static if'

Status
NEW
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-28T03:56:26Z
Last change time
2024-12-13T17:58:08Z
Keywords
spec
Assigned to
No Owner
Creator
Nick Sabalausky
Moved to GitHub: dmd#18407 →

Comments

Comment #0 by bus_dbugzilla — 2012-01-28T03:56:26Z
static if(true) version = Foo; version(Foo) {} test.d(2): Error: version Foo defined after use The compiler's evaluation seems to occur in the wrong order ("versions before static ifs" instead of "top to bottom"), because this works: static if(true) version = Foo; static if(true) version(Foo) {}
Comment #1 by ibuclaw — 2020-09-09T10:25:42Z
Bumping priority of this one.
Comment #2 by dkorpel — 2021-07-09T22:56:49Z
> The compiler's evaluation seems to occur in the wrong order ("versions before static ifs" instead of "top to bottom") This 'evaluation order' is totally underspecified and super tricky. Simply going top to bottom doesn't work since a static if can have forward references: ``` static if (x == 0) { version = X1; } version(X1) { } enum x = 0; ``` Even trickier: ``` static if (x == 0) { version = X1; } version(X1) { version = X2; } version(X2) { enum x = 0; } ```
Comment #3 by dfj1esp02 — 2021-07-12T14:22:10Z
It feels like version is a lightweight feature and is processed early as it doesn't depend much on semantics of the surrounding code, while static if tends to be processed on later stages as it needs module that passed most semantic checks. As a workaround the original example can simply use static if everywhere; static if can inspect versions too: see issue 7417
Comment #4 by kytodragon — 2022-04-11T07:41:28Z
(In reply to anonymous4 from comment #3) > It feels like version is a lightweight feature and is processed early as it > doesn't depend much on semantics of the surrounding code, while static if > tends to be processed on later stages as it needs module that passed most > semantic checks. > As a workaround the original example can simply use static if everywhere; > static if can inspect versions too: see issue 7417 There are few bugs related to static if (see https://issues.dlang.org/show_bug.cgi?id=20905), so replacing all versions with static if is not currently possible.
Comment #5 by ibuclaw — 2022-04-11T11:44:22Z
Potentially very breaking, but declaring versions could be even more restrictive than it currently is. i.e: When setting a version in a function or user defined type, you expect a compile-time error. Error: version `Foo` declaration must be at module level This could be extended to `static if` as well so that we avoid all these nasty cases of will it, won't it work. Also noting that it's currently an error to declare a version inside a `static foreach`. --- static foreach (_; 0 .. 5) { version = Foo; // Error: version `Foo` declaration must be at module level } --- So why is `static if` different here?
Comment #6 by robert.schadek — 2024-12-13T17:58:08Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18407 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB