digitalmars.D - Problem with integral promotions
- Walter Bright (21/21) Jul 22 2017 They are supposed to match C. But they don't for unary operators + - ~, ...
- ketmar (7/10) Jul 22 2017 i'd say "codify, and add warning". since i patched the warning into the
- Guillaume Piolat (6/12) Jul 22 2017 I'd vote for the ketmar solution if workable.
- Jerry (5/17) Jul 22 2017 Good luck adding a warning into DMD. After years there still
- ketmar (5/7) Jul 22 2017 basically, any template that has small integral type T and does unary +/...
- Walter Bright (7/7) Jul 22 2017 This affects:
- Vladimir Panteleev (3/8) Jul 22 2017 Git blames you:
- Walter Bright (4/7) Jul 22 2017 Ah, thanks for finding this. It was in response to:
- Kagamin (2/2) Jul 24 2017 OT: https://issues.dlang.org/show_bug.cgi?id=17417 can we also
- Walter Bright (2/3) Jul 24 2017 The bugzilla page was pretty clear about it.
- Steven Schveighoffer (26/28) Jul 24 2017 My opinion is that it should behave unsurprisingly.
- Walter Bright (2/2) Jul 24 2017 Yes, I think a switch to enable the correct behavior, and a warning when...
- Walter Bright (2/4) Jul 24 2017 https://github.com/dlang/dmd/pull/7013
- Steven Schveighoffer (9/15) Jul 25 2017 Yes, that should be workable.
- Walter Bright (2/4) Jul 25 2017 That's right. Also for C/C++ programmers who are used to how C works.
- Random D user (6/8) Jul 24 2017 If D was my language I'd fix it, since it's a bug.
They are supposed to match C. But they don't for unary operators + - ~, and as far as I can tell never did. https://issues.dlang.org/show_bug.cgi?id=16997 https://issues.dlang.org/show_bug.cgi?id=17637 Note that the spec says: "Note: unlike in C and C++, the usual integral promotions are not performed prior to the complement operation." http://dlang.org/spec/expression.html#complement_expressions Where did that come from? And the spec says nothing about unary - or unary +. http://dlang.org/spec/expression.html#unary-expression It's been like this at least since D1, both the code and the Spec. So, we have a choice: 1. Fix it so it matches C, as has been generally promised. Fixing it will break existing code such as: https://github.com/dlang/phobos/pull/5646 but worse, it may silently break code. I don't know of a reasonable way to detect this. 2. Codify existing practice, since it has been that way forever. Not matching C has caused problems, see 16997 and 17637. It may cause more serious silent problems for people converting C code to D code.
Jul 22 2017
Walter Bright wrote:2. Codify existing practice, since it has been that way forever. Not matching C has caused problems, see 16997 and 17637. It may cause more serious silent problems for people converting C code to D code.i'd say "codify, and add warning". since i patched the warning into the compiler, i have no more problems with the current rules: compiler tells me about possible incompatibilities with C, and i can either add `()` to silent the warning, or explicitly cast to the type i want. i think most other people will prefer to get warning instead of code breakage too. at least i hope so. ;-)
Jul 22 2017
On Saturday, 22 July 2017 at 10:51:05 UTC, ketmar wrote:Walter Bright wrote:I'd vote for the ketmar solution if workable. Another argument for this is that C code that has already been converted to D, and is currently silently broken, would benefit from a new warning as a way of gathering attention. However matching C promotion might make this code work too but silently.2. Codify existing practice, since it has been that way forever. Not matching C has caused problems, see 16997 and 17637. It may cause more serious silent problems for people converting C code to D code.i'd say "codify, and add warning".
Jul 22 2017
On Saturday, 22 July 2017 at 10:51:05 UTC, ketmar wrote:Walter Bright wrote:Good luck adding a warning into DMD. After years there still isn't a warning for unsigned/signed comparisons. What code would break? Are there any examples of D code that would break as a result of the change?2. Codify existing practice, since it has been that way forever. Not matching C has caused problems, see 16997 and 17637. It may cause more serious silent problems for people converting C code to D code.i'd say "codify, and add warning". since i patched the warning into the compiler, i have no more problems with the current rules: compiler tells me about possible incompatibilities with C, and i can either add `()` to silent the warning, or explicitly cast to the type i want. i think most other people will prefer to get warning instead of code breakage too. at least i hope so. ;-)
Jul 22 2017
Jerry wrote:What code would break? Are there any examples of D code that would break as a result of the change?basically, any template that has small integral type T and does unary +/-/~ on it. and there *is* such code in phobos, and it's not explicitly tested for different integral sizes. when i added a warning, i had to fix phobos in several places, but i'm pretty sure that there are ALOT more.
Jul 22 2017
This affects: ~ubyte: all values -ubyte: all values -byte: -128 (0x80) ~ushort: all values -ushort: all values -short: -32768 (0x8000)
Jul 22 2017
On Saturday, 22 July 2017 at 10:44:04 UTC, Walter Bright wrote:Note that the spec says: "Note: unlike in C and C++, the usual integral promotions are not performed prior to the complement operation." http://dlang.org/spec/expression.html#complement_expressions Where did that come from?Git blames you: https://github.com/dlang/dlang.org/commit/4cd3f38bbabdde30b280738dab4f4184c06f05f9
Jul 22 2017
On 7/22/2017 1:07 PM, Vladimir Panteleev wrote:Git blames you: https://github.com/dlang/dlang.org/commit/4cd3f38bbabdde30b280738dab4f4184c06f05f9Ah, thanks for finding this. It was in response to: https://issues.dlang.org/show_bug.cgi?id=5132 i.e. it was documenting existing behavior.
Jul 22 2017
OT: https://issues.dlang.org/show_bug.cgi?id=17417 can we also reconsider this?
Jul 24 2017
On 7/24/2017 1:50 AM, Kagamin wrote:OT: https://issues.dlang.org/show_bug.cgi?id=17417 can we also reconsider this?The bugzilla page was pretty clear about it.
Jul 24 2017
On 7/22/17 6:44 AM, Walter Bright wrote:They are supposed to match C. But they don't for unary operators + - ~, and as far as I can tell never did.My opinion is that it should behave unsurprisingly. It's very surprising behavior for: ubyte x = 1; int y = -x; To make y = 255. My recommendation: 1. issue a warning for using any of those operators an unsigned type without an unambiguous cast. In other words: ubyte x = 1; -x; // warning cast(ubyte)-x; // OK, not changing size cast(int)-x; // warning -cast(int)x; // OK 2. Change the warning to a deprecation. 3. Change to an error optionally, you could finally implement the C behavior after this and remove the error, but I'm actually thinking that the number of times this comes up is probably so few that it's worth requiring the cast. Note, the negation of byte.min or short.min is not as surprising as the negation of unsigned types, as int or long have the same behavior, and it's a well-known corner case. Also note, you did not include char/wchar in your list of affected types, though they have the same behavior with those operators as ubyte and ushort. -Steve
Jul 24 2017
Yes, I think a switch to enable the correct behavior, and a warning when the cases are encountered and the switch isn't thrown, is likely the best approach.
Jul 24 2017
On 7/24/2017 2:07 PM, Walter Bright wrote:Yes, I think a switch to enable the correct behavior, and a warning when the cases are encountered and the switch isn't thrown, is likely the best approach.https://github.com/dlang/dmd/pull/7013
Jul 24 2017
On 7/24/17 6:46 PM, Walter Bright wrote:On 7/24/2017 2:07 PM, Walter Bright wrote:Yes, that should be workable. I'm looking at some of the code that you have to update, and I'm wondering if ~ should still operate the way it does. While - on an unsigned is somewhat clear that you want a negative (and therefore a different type), ~ on an unsigned actually makes sense for the same type. However, if C operates this way, this would be a huge trap for people who are porting code. -SteveYes, I think a switch to enable the correct behavior, and a warning when the cases are encountered and the switch isn't thrown, is likely the best approach.https://github.com/dlang/dmd/pull/7013
Jul 25 2017
On 7/25/2017 6:43 AM, Steven Schveighoffer wrote:However, if C operates this way, this would be a huge trap for people who are porting code.That's right. Also for C/C++ programmers who are used to how C works.
Jul 25 2017
On Saturday, 22 July 2017 at 10:44:04 UTC, Walter Bright wrote:1. Fix it so it matches C, as has been generally promised. Fixing it will break existing code such as:If D was my language I'd fix it, since it's a bug. D's fluidity and effortlessness comes from a lot of small compounding convenience features. The converse is true as well. A lot of small annoyances accumulate into frustration.
Jul 24 2017