Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd safe integral auto-coercions #225
Conversation
lilyball
reviewed
Sep 3, 2014
|
|
||
| # Drawbacks | ||
|
|
||
| ? |
This comment has been minimized.
This comment has been minimized.
lilyball
Sep 3, 2014
Contributor
Drawback: silent conversions are confusing.
fn bar(x: u32) { /* ... */ }
fn foo(x: u32) {
let y = x + mystery_func(); // clearly a u32, right? It's derived from x
// ... code goes here
bar(y); // error, argument must be u32?
}
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
lilyball
Sep 3, 2014
Contributor
Oops, I re-used the name bar(), that's a bit confusing. I'll fix it. I meant for the first call to bar() to be some unknown function with an unknown return value. The expectation is if the line let y = x + bar() compiles, then bar()'s return value must be compatible with x, which is a u32, and addition on integral types is known to return the receiver type.
This comment has been minimized.
This comment has been minimized.
tommit
Sep 3, 2014
Author
Ok, but let's say that mystery_func() returns u64. Today the compiler would give the error (mismatched types: expected u32, found u64) on the line: let y = x + mystery_func();. If this RFC lands, then the compiler would give the same error on the line: bar(y);. I don't see this (the location of the error) as a big issue; after all, you can fix your code by down-casting either mystery_func() or y to u32, so it's not that clear what the best place for the error is anyway.
This comment has been minimized.
This comment has been minimized.
lilyball
Sep 4, 2014
Contributor
The problem is it divorces the reported error from the actual error. The line bar(y); is not the bad code. The line let y = x + mystery_func(); is the bad code. Yes, you can get the same issue today merely by using a bad type, e.g. if bar() took x: i32 instead of x: u32. But in the cited example, it looks straightforward, it looks like the typing should all match, and the error on bar(y); is therefore confusing.
This comment has been minimized.
This comment has been minimized.
|
@zwarich and I discussed this at some length a few weeks ago. We came to the conclusion that we didn't think there would be any problems with widening within signedness, but that changing signedness could be confusing, especially in the face of overflow. I can't find the IRC logs from it, he may recall more, but I remember wanting to find numbers of something. We were discussing it in the context of int/uint always being u32/i32 (ie, using an LP64 model). Perhaps we wanted to know how many uses would need explicit casts. |
This comment has been minimized.
This comment has been minimized.
|
Regarding my "Unresolved Questions" section, at least the following auto-coercions should be safe: from |
This comment has been minimized.
This comment has been minimized.
|
An example where the conversions, if allowed, lead to unintended result:
Anyway, the idea probably comes to mind to every person who encounters Rust for the first time, but the conversions are still prohibited. Wasn't it discussed at least for a dozen times and rejected? |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov How does your example demonstrate my RFC leading to an unintended result? Just to break it down... your example's Today you'd have to write |
This comment has been minimized.
This comment has been minimized.
|
@tommit If compiler wants you to write |
This comment has been minimized.
This comment has been minimized.
|
Example 2: Interaction with traits.
Of course, it is possible to specify all the cases, but then built-in types become special, built-in operators become special, initializations of variables and function arguments may become different, rules become more complex, hello C++.
but I'm not sure about it either. |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Ok, I can see how getting the error message in the That said, I do think the interplay between traits and auto-coercible integral types could work and that some more magic in the built-in integral types wouldn't make the language that much more complicated. Numeric types are already a bit special in that they, unlike user-defined types, can be cast using the But, since I do think that helping to catch that
|
This comment has been minimized.
This comment has been minimized.
|
@tommit I'd suggest just using uint for sizes and indexes, as the standard library does. Of course, there are some exceptions, but it reduces the conversion noise to minimum. |
alexcrichton
force-pushed the
rust-lang:master
branch
from
6357402
to
e0acdf4
Sep 11, 2014
aturon
force-pushed the
rust-lang:master
branch
from
4c0bebf
to
b1d1bfd
Sep 16, 2014
This comment has been minimized.
This comment has been minimized.
|
Thank you for the RFC @tommit but we're not interested in adding such coercions right now. |
brson
closed this
Sep 18, 2014
This comment has been minimized.
This comment has been minimized.
Yamakaky
commented
Nov 13, 2014
|
At least why not allow all unsigned number types to index an array ? See the issue above. |
tommit commentedSep 3, 2014
View