Bug 20653 – Short-circuiting boolean logic not working
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-03-09T17:46:28Z
Last change time
2020-03-19T04:16:06Z
Keywords
pull
Assigned to
No Owner
Creator
Manu
Comments
Comment #0 by turkeyman — 2020-03-09T17:46:28Z
I feel like this should work:
enum Thing { A, B }
enum IsThing(T) = is(T == enum) || (__traits(getLinkage, T) == "C++");
enum Is = IsThing!Thing;
> Error: argument to `__traits(getLinkage, Thing)` is not a declaration
> Error: template instance `IsThing!(Thing)` error instantiating
`is(T == enum)` is true, so it shouldn't attempt to evaluate `getLinkage`.
Comment #1 by ketmar — 2020-03-10T11:09:14Z
it never worked with static ifs. i was pretty sure that it was By Design, but i cannot remember if i really read that somewhere in the specs, or simply made it out.
Comment #2 by turkeyman — 2020-03-10T17:13:12Z
My work-around is to use a `static if()` to split the expression across multiple lines.
It's not a good workaround; it's lame and unnecessary.
Comment #3 by dkorpel — 2020-03-10T20:31:15Z
(In reply to Ketmar Dark from comment #1)
> it never worked with static ifs. i was pretty sure that it was By Design,
> but i cannot remember if i really read that somewhere in the specs, or
> simply made it out.
Short circuiting actually works in certain static conditions:
```
static if (false && bogusName) {}
static assert(true || bogusName);
void foo(T)() if (true ? true : bogusName) {};
alias x = foo!int;
```
This is implemented in staticcond.d, function `evalStaticCondition`:
> Semantically analyze and then evaluate a static condition at compile time.
> This is special because short circuit operators &&, || and ?: at the top
> level are not semantically analyzed if the result of the expression is not
> necessary.
That function is not used to evaluate enums however.
Comment #4 by turkeyman — 2020-03-10T20:36:21Z
Right. That's why there is a bug report :)
Comment #5 by dlang-bot — 2020-03-18T03:21:02Z
@benjones updated dlang/dmd pull request #10939 "fix issue 20653 . Short circuit manifest constant evaluation" fixing this issue:
- fix issue 20653 . Short circuit manifest constant evaluation
https://github.com/dlang/dmd/pull/10939
Comment #6 by dlang-bot — 2020-03-19T04:16:06Z
dlang/dmd pull request #10939 "fix issue 20653 . Short circuit manifest constant evaluation" was merged into master:
- 73fab586f68d28b448f3712a28edc024862015b2 by Ben Jones:
fix issue 20653 -- apply short circuit evaluation for manifest constants
https://github.com/dlang/dmd/pull/10939