-
Notifications
You must be signed in to change notification settings - Fork 13.4k
RFC: rustc: Allow any integral types on rhs of shift ops #1880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This sounds great. I was a bit nervous about the effect on inference (we are no long unifying with an expected type) but I couldn't come up with any examples that wouldn't work. |
Yeah. It seems like there must be cases where we can't infer something, but if so the same issue exists with vector indexes. |
it seems like, for any legit program, the shift amount will have been defined somewhere else and it will get its type from that location. the only problem would be if the type came from the shift operator itself, which only seems to occur in bogus programs like:
conceivably this might have type checked before with |
I think requiring an explicit type hint here is fine since it's the less common case by far. This change does add just a tiny bit more complexity to the type checker, and every bit counts. The person who actually tries to write that code is going to be confounded as to why it doesn't type check. I think our inference algorithm is already complex to the point that you just have to believe the compiler when it says it can't infer the types, then add annotations until it compiles (like Scala I gather). |
Agreed. I am fine with this. Also, the annoyance of writing "foo >> 16u32" far outweights a theoretical concern about a theoretical program that could never successfully execute anyway. I guess a "real world" example might involve reinterpret case:
Here the type annotation is now required. This is sufficiently bizarre that I would hope you would annotate your type anyhow! :) |
Yeah, this is ok. Requiring the LHS and RHS operands be "of the same type" makes sense when the operator is either:
The shifts don't really correspond to either. At best you can think of them as a funny notation for repeated application of mul2 and div2, except you still have sign extension to explain. They're pretty much a peculiarity of computers, and don't have any pretty algebraic axioms we're trying to preserve (associativity, commutativity, transitivity, etc. etc.) |
Fix issues with how we are generating code for casting (rust-lang#566, and rust-lang#1528). Restructure the unsize casting to be done in one pass instead with deep recursion (rust-lang#1531). This also reuses the code from the reachability analysis, so we don't have to keep two ways of traversing the same structure.
Fix for #1570. Making this an RFC since it's a language change.
The RHS of shift ops can be any integral type. This does nothing to address the various undefined situations that crop up with shifting.