Liste des Groupes | Revenir à c arch |
On 9/5/2024 8:27 AM, David Brown wrote:A programming language has to work as it is defined. And people should not be relying on code doing things that are /not/ defined.On 05/09/2024 10:51, Niklas Holsti wrote:A lot of time, implicit promotion will be the "safer" option than first doing an operation that overflows and then promoting.On 2024-09-05 10:54, David Brown wrote:>On 05/09/2024 02:56, MitchAlsup1 wrote:>On Thu, 5 Sep 2024 0:41:36 +0000, BGB wrote:>
>On 9/4/2024 3:59 PM, Scott Lurndal wrote:>
>
Say:
long z;
int x, y;
...
z=x*y;
Would auto-promote to long before the multiply.
\I may have to use this as an example of C allowing the programmer
to shoot himself in the foot; promotion or no promotion.
You snipped rather unfortunately here - it makes it look like this was code that Scott wrote, and you've removed essential context by BGB.
>
>
While I agree it is an example of the kind of code that people sometimes write when they don't understand C arithmetic, I don't think it is C-specific. I can't think of any language off-hand where expressions are evaluated differently depending on types used further out in the expression. Can you give any examples of languages where the equivalent code would either do the multiplication as "long", or give an error so that the programmer would be informed of their error?
>
The Ada language can work in both ways. If you just have:
>
z : Long_Integer; -- Not a standard Ada type, but often provided.
x, y : Integer;
...
z := x * y;
>
the compiler will inform you that the types in the assignment do not match: using the standard (predefined) operator "*", the product of two Integers gives an Integer, not a Long_Integer.
That seems like a safe choice. C's implicit promotion of int to long int can be convenient, but convenience is sometimes at odds with safety.
>
Annoyingly, one can't really do the implicit promotion first and then promote afterwards, as there may be programs that actually rely on this particular bit of overflow behavior.
In effect, in my case, the promotion behavior ends up needing to depend on the language-mode (it is either this or maybe internally split the operators into widening or non-widening variants, which are selected when translating the AST into the IR stage).Dependency on a "language mode" does not sound "safe" to me.
Well, as opposed to dealing with the widening cases by emitting IR with an implicit casts added into the IR.
Les messages affichés proviennent d'usenet.