[rust-dev] Warn about implicit integer conversion

Lindsey Kuper lindsey at rockstargirl.org
Tue Dec 18 20:17:41 PST 2012

On Tue, Dec 18, 2012 at 7:15 PM, Graydon Hoare <graydon at mozilla.com> wrote:
> On 12-12-18 10:35 AM, Michael Neumann wrote:
>> Hi,
>>   let a: u8 = 10_000; // this does not fit inside u8!
>>   io::println(fmt!("%?", a)); // print "10"
>> I would have expected that rust would warn me if I try to assign an
>> integer constant that doesn't fit into the types range.
>> Maybe there exists a warning that I can enable?
> Yikes. Unfortunate. This is another of those cases I dreaded the
> occurrence of when finally argued into accepting literal-suffix
> inference. Oh well.
> I think I know what's going on here, and it's a bit of a sad story, but
> one we can probably salvage some of. I'll file a bug.
> Note that if you write "10_000u8" (explicitly giving a suffix), rustc
> complains as it should.
> What's happening instead is that you're invoking literal-suffix
> inference[1] by not providing a suffix, and it's deciding that you mean
> u8 (correctly), and it's _then_ accepting that in 2s complement
> arithmetic, overflow of that sort is completely legitimate. Which, you
> know, it is. [...]
> [...]
> Probably lkuper has more to say about this, but for the time being,
> watch this bug and we'll try to fix it up at least a little more in the
> next release cycle:
>   https://github.com/mozilla/rust/issues/4220

Our thinking at the time we implemented suffix inference was that if
you went so far as to write the optional type annotation, then you
should get the type you asked for, no questions asked, overflows and
all.  After all, sometimes people do want intentional overflow.  But
if it's causing nasty surprises, then, yeah, it should be revisited.


More information about the Rust-dev mailing list