www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiler switch for integer comparison/promotion to catch a simple

reply frame <frame86 live.com> writes:
Is there a compiler switch to catch this kind of error?

```d
ulong v = 1;
writeln(v > -1);
```

IMHO the compiler should bail a warning if it sees a logic 
comparison between signed and unsigned / different integer sizes. 
There is 50% chance that a implicit conversion was not intended.
May 28 2022
next sibling parent matheus <matheus gmail.com> writes:
On Sunday, 29 May 2022 at 01:35:23 UTC, frame wrote:
 Is there a compiler switch to catch this kind of error?

 ```d
 ulong v = 1;
 writeln(v > -1);
 ```

 IMHO the compiler should bail a warning if it sees a logic 
 comparison between signed and unsigned / different integer 
 sizes. There is 50% chance that a implicit conversion was not 
 intended.
Well I don't know about this, but of course I think (That if not) we should have at least a flag like we have with GCC (-Wextra). Searching about I found in this topic: https://forum.dlang.org/post/hhpacodmcibejatqzdfg forum.dlang.org "Good luck adding a warning into DMD. After years there still isn't a warning for unsigned/signed comparisons." This is from 2017, so let's wait for experienced weighting in. Matheus.
May 29 2022
prev sibling next sibling parent reply bauss <jj_1337 live.dk> writes:
On Sunday, 29 May 2022 at 01:35:23 UTC, frame wrote:
 Is there a compiler switch to catch this kind of error?

 ```d
 ulong v = 1;
 writeln(v > -1);
 ```

 IMHO the compiler should bail a warning if it sees a logic 
 comparison between signed and unsigned / different integer 
 sizes. There is 50% chance that a implicit conversion was not 
 intended.
Good luck convincing Walter that this is a mistake :)
May 30 2022
next sibling parent frame <frame86 live.com> writes:
On Monday, 30 May 2022 at 13:15:12 UTC, bauss wrote:

 Good luck convincing Walter that this is a mistake :)
Well, I'm not talking about this is a mistake, just a C-thing I think. I wouldn't even ask him about that since it's in the spec. If I could I would just clone a DMD build, disable output of the binary and just add flags to analyze this and other things to check wanted by the community. Just a tool to check for additional bugs running before release - that of course would produce also false positives and should not be in the official compiler. But I'm far too unexperienced to understand the compiler and implement this - I wonder if anyone else ever had this idea?
May 30 2022
prev sibling parent matheus <matheus gmail.com> writes:
On Monday, 30 May 2022 at 13:15:12 UTC, bauss wrote:
 Good luck convincing Walter that this is a mistake :)
I don't think this is a matter of convincing or changing the behavior, I think that a flag for this case (If not exist) should be added as a warning. A language where some people use to spread the phrase "D as better C" should at least have this as flag/warning too. Matheus.
May 30 2022
prev sibling parent Dom Disc <dominikus scherkl.de> writes:
On Sunday, 29 May 2022 at 01:35:23 UTC, frame wrote:
 Is there a compiler switch to catch this kind of error?

 ```d
 ulong v = 1;
 writeln(v > -1);
 ```

 IMHO the compiler should bail a warning if it sees a logic 
 comparison between signed and unsigned / different integer 
 sizes. There is 50% chance that a implicit conversion was not 
 intended.
We have a solution for this problem in bugzilla: https://issues.dlang.org/show_bug.cgi?id=259 This would allow for the above compare to simply work with all integer types signed or unsigned and produce always the correct result. It was rejected because it would break old code that rely on this misbehaviour (and even worse: the behaviour would be different from what C does, oh my god!!)
May 30 2022