New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix Issue 14835 - Constant folding should not affect front end flow a… #12311
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request, @WalterBright! Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#12311" |
Blocked by dlang/druntime#3417 There'll likely be much more of this. Sigh. |
This does not fix the issue I reported. The reported issue was explicitly about |
@Geod24 I know, but it may provide the right solution to the |
Could you add a description about how this fix the reported issue then ? |
Use |
void reachIf(bool x)() | ||
{ | ||
if (!x) | ||
return; | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void reachIf(bool x)() | |
{ | |
if (!x) | |
return; | |
return; | |
} | |
bool reachIf(bool x)() | |
{ | |
static if (!x) | |
return false; | |
int a; | |
assert(a == 0); | |
return true; | |
} |
The issue was explicitly about static if
, but the test here only includes if
.
Additionally, the extra statements are to guarantee that "Statement is not reachable" is triggered, which could stop happening to an empty return
at the end of a void
function due to unrelated changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The solution to the static if
issue is to change the behavior of if
. Hence the test is about if
.
@WalterBright : If I understand correctly, you intend to solve the issue by recommending people to switch from the following: bool isEven(int i)()
{
static if (i % 2) // static or not, the warning will get triggered if i is even
return true;
return false;
} To the following: bool isEven(int i)()
{
if (i % 2) // static or not, the warning will get triggered if i is even
return true;
return false;
} I think that makes sense, but it goes against many developer's instinct which is to reach for |
The concept here seems right to me, although I can't comment on the implementation. I bumped the old forum thread for this issue just in case someone else sees a serious problem with this approach that we don't. |
Yes. |
I agree that an if is to be preferred to a static if whenever possible. |
|
There's a PR for that: dlang/druntime#3417 |
If I understand this reasoning properly (didn't even look at the changes, I don't understand DMD), this doesn't fix the issue. There are reasons to use And in actuality, as I said in the bug report, the "code is unreachable" statement is incorrect. It's reachable, just not in this instantiation. This is going to result in things like: static if(doSomethingAtCTFE())
{
if(1) return retval; // trick the compiler
} Which I don't think is a good look. What happens when foreach on a CT tuple is used? That's another common source of spurious "unreachable" errors. |
My previous comment notwithstanding, as long as the |
Although annoying, "not reachable" warnings caused by By contrast, there is no simple workaround for some of the spurious warnings that could theoretically be triggered by runtime control flow: #5229 (comment) (I'm not sure about |
That depends on your perspective. From mine, static if inserts code. If that insert makes other code unreachable, it is unreachable. This is different from the |
this also probably needs a force push in order to restart all of the CIs |
|
Now it's blocked by dlang/phobos#8180 |
This seems to be green now. Should we merge this? |
It's still not clear whether this fix satisfies users (See Steven's comment #12311 (comment)), but if we go with "Constant folding should not affect front end flow analysis", then flow analysis of |
I don't love that static if will still complain about unreachable code, but if there is a workaround like I said in #12311 (comment), then we can consider the issue resolved, and perhaps open another issue on making that workaround obsolete. Note the original title of the bug was "Statement is not reachable doesn't play along generic code", which describes more accurately the problem I was having, and would not be fixed by this PR. |
I'm not sure if the rebase was done correctly. Have to re-evaluate this. |
a89ad48
to
2998b6f
Compare
…nalysis
Users should use
static if
if they need compile time conditional compilation.