D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4558 - To spot a possible bug in code that doesn't change a value
Summary: To spot a possible bug in code that doesn't change a value
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2010-08-01 14:20 UTC by bearophile_hugs
Modified: 2022-08-15 12:00 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 bearophile_hugs 2010-08-01 14:20:33 UTC
This is a semantically wrong D2 program, the programmer has used a "&=" instead of "|=" to build a flag result:


enum Flags { NONE   = 0b00,
             FIRST  = 0b01,
             SECOND = 0b10
}
Flags foo(int x) {
    Flags result = Flags.NONE;
    if (x < 10)
        result |= Flags.FIRST;
    else
        result &= Flags.SECOND; // line 10
    return result;
}
void main() {}


The D compiler can find this problem in the code at line 10 because in that program the variable "result" is certantly zero after the assignment with Flags.SECOND, so it's an error or it's useless code.
Comment 1 Infiltrator 2017-06-10 06:54:07 UTC
So, if I understand correctly, you want the compiler to statically calculate results where all the inputs are known at compile-time and detect when no change has occurred in a "&=" (and presumably in other operator-assignment operators?)  

In such cases, should the compiler raise a warning or an error?

This all fits in nicely with CTFE.
Comment 2 RazvanN 2022-08-15 12:00:41 UTC
Walter has repeatedly stated that dmd-fe does not and will not do dataflow analysis. The problem with these sort of examples is that they seem trivial to implement for the simple cases, but once you start to think about more complex scenarios, the implementation becomes very complicated very fast.

I will close this as WONTFIX.