[Issue 12919] New: disallow implicit signed/unsigned integer conversions
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sat Jun 14 04:10:10 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=12919
Issue ID: 12919
Summary: disallow implicit signed/unsigned integer conversions
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: DMD
Assignee: nobody at puremagic.com
Reporter: code at dawg.eu
import std.conv, std.stdio;
void foo(uint ua) { writefln("foo(%s)", ua); }
void bar(int ia) { writefln("bar(%s)", ia); }
void main(string[] args)
{
int sa = to!int(args[1]);
uint ua = sa;
foo(ua); bar(ua);
foo(sa); bar(sa);
foo(int.min); bar(uint.max);
}
http://dpaste.dzfl.pl/04bbf332f26b
This behavior is explicitly documented in http://dlang.org/type.html#Usual
Arithmetic Conversions.
The page arguments that those conversions are OK, because the representation of
signed and unsigned ints/longs is the same.
Integer values cannot be implicitly converted to another type that cannot
represent the integer bit pattern after integral promotion. For example:
ubyte u1 = cast(byte)-1; // error, -1 cannot be represented in a ubyte
ushort u2 = cast(short)-1; // error, -1 cannot be represented in a ushort
uint u3 = cast(int)-1; // ok, -1 can be represented in a uint
ulong u4 = cast(long)-1; // ok, -1 can be represented in a ulong
The representation is not a very compelling argument to allow those conversion
to be implicit, because signed/unsigned problems occur when interpretating the
value.
Problems with signed/unsigned integer promotion rules for binary operators have
been discussed in bug 259 and there is a sane proposal for safe conversions in
bug 239 comment 39. Time to fix this.
--
More information about the Digitalmars-d-bugs
mailing list