November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #9 from bearophile_hugs@eml.cc 2012-11-29 18:22:17 PST --- Maybe you want to temporarily turn your new error message into a warning (that later will become an error again, if you want), so if Phobos gets entirely compiled with "-wi" it will not stop the compilation and the log will show the possible bugs spotted by this warning. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #10 from Walter Bright <bugzilla@digitalmars.com> 2012-11-29 19:50:14 PST --- (In reply to comment #6) > How about code like this? > > void main() { > uint i = 0; > if (i == -2) > assert(0, "never"); > } Equality has nothing to do with sign, i.e. there is no "signed equality" and "unsigned equality". There's just equality. I don't think there's any getting away from the fact that in languages like D and C, signed and unsigned are simply different ways of viewing the same data. For example, you can offset a pointer with both signed and unsigned values. There is no clean separation between the two. Some languages, such as Java, deal with this duality by defining the unsigned type out of existence. I've made more comments in the pull request. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #11 from bearophile_hugs@eml.cc 2012-11-29 20:15:15 PST --- (In reply to comment #10) > Some languages, such as Java, deal with this duality by defining the unsigned type out of existence. There are languages like Ada, that have both signed and unsigned native integrals, but behave differently from C/C++/D. (Even if this patch is eventually refused, I suggest to turn it into a warning and compile all phobos one time again with "-wi" to look for possible bugs spotted by this.) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #12 from bearophile_hugs@eml.cc 2012-11-30 04:17:15 PST --- Regarding a comment by Don, is it possible to generate a warning/error only in foo() and not in bar()? void foo(uint x) { if (x < 0) {} // error or warning here } void bar(T)(T x) { if (x < 0) {} // OK } void main() { foo(5U); bar(5U); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 Andrej Mitrovic <andrej.mitrovich@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords|pull | AssignedTo|andrej.mitrovich@gmail.com |nobody@puremagic.com --- Comment #13 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-11-30 04:43:25 PST --- (In reply to comment #12) > Regarding a comment by Don, is it possible to generate a warning/error only in > foo() and not in bar()? > > > void foo(uint x) { > if (x < 0) {} // error or warning here > } > void bar(T)(T x) { > if (x < 0) {} // OK > } > void main() { > foo(5U); > bar(5U); > } I doubt it. Anyway I've closed the pull for now since it's controversial when (or if) we should have warnings/errors for this. If we had a switch like GCC's `-Wtype-limits` then we could implement this check for all cases when the switch is enabled and not worry about it. But Walter is against switches so... Maybe LDC/GDC can or already do support this though. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #14 from bearophile_hugs@eml.cc 2012-11-30 05:25:50 PST --- (In reply to comment #13) > (In reply to comment #12) > > Regarding a comment by Don, is it possible to generate a warning/error only in > > foo() and not in bar()? > > > > > > void foo(uint x) { > > if (x < 0) {} // error or warning here > > } > > void bar(T)(T x) { > > if (x < 0) {} // OK > > } > > void main() { > > foo(5U); > > bar(5U); > > } > > I doubt it. I'd like a better answer about this, maybe from Don or Hara or someone that knows this answer better. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #15 from Don <clugdbug@yahoo.com.au> 2012-11-30 06:31:17 PST --- (In reply to comment #14) > (In reply to comment #13) > > (In reply to comment #12) > > > Regarding a comment by Don, is it possible to generate a warning/error only in > > > foo() and not in bar()? > > > > > > > > > void foo(uint x) { > > > if (x < 0) {} // error or warning here > > > } > > > void bar(T)(T x) { > > > if (x < 0) {} // OK > > > } > > > void main() { > > > foo(5U); > > > bar(5U); > > > } > > > > I doubt it. > > I'd like a better answer about this, maybe from Don or Hara or someone that knows this answer better. It's easy. DSymbol.inTemplateInstance() returns non-NULL if you are inside a template. You just need to call it on the function you're in. BTW std.bigint has a lot of examples of valid comparisons of unsigned < 0 . -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 30, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #16 from bearophile_hugs@eml.cc 2012-11-30 09:36:58 PST --- (In reply to comment #15) > BTW std.bigint has a lot of examples of valid comparisons of unsigned < 0 . The comparisons in std.bigint are like this, all of them are inside templated functions: BigInt opAssign(T: long)(T x) { data = cast(ulong)((x < 0) ? -x : x); sign = (x < 0); return this; } BigInt opOpAssign(string op, T)(T y) if ((op=="+" || op=="-" || op=="*" || op=="/" || op=="%" || op==">>" || op=="<<" || op=="^^") && is (T: long)) { ulong u = cast(ulong)(y < 0 ? -y : y); ... > It's easy. > DSymbol.inTemplateInstance() returns non-NULL if you are inside a template. > You just need to call it on the function you're in. Thank you Don. If it's not hard to do then I suggest to add that in this line to exclude templates from this test, and take a look at what the Phobos autotester says: // Error when unsigned type is compared less-than to zero, but not in enum declarations or in static if if ((!(sc->flags & SCOPEstaticif)) && sc->parent && !sc->parent->isEnumDeclaration()) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
December 18, 2012 [Issue 6949] no warning or error if unsigned variable is compared to 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | http://d.puremagic.com/issues/show_bug.cgi?id=6949 --- Comment #17 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-18 09:19:32 PST --- *** Issue 5539 has been marked as a duplicate of this issue. *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation