Issue 20653 - Short-circuiting boolean logic not working
Summary: Short-circuiting boolean logic not working
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-03-09 17:46 UTC by Manu
Modified: 2020-03-19 04:16 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2020-03-09 17:46:28 UTC
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 Ketmar Dark 2020-03-10 11:09:14 UTC
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 Manu 2020-03-10 17:13:12 UTC
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 Dennis 2020-03-10 20:31:15 UTC
(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 Manu 2020-03-10 20:36:21 UTC
Right. That's why there is a bug report :)
Comment 5 Dlang Bot 2020-03-18 03:21:02 UTC
@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 Dlang Bot 2020-03-19 04:16:06 UTC
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