Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

brson
Copy link
Contributor

@brson brson commented Feb 22, 2012

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.

@nikomatsakis
Copy link
Contributor

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.

@brson
Copy link
Contributor Author

brson commented Feb 22, 2012

Yeah. It seems like there must be cases where we can't infer something, but if so the same issue exists with vector indexes.

@nikomatsakis
Copy link
Contributor

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:

fn to_any_type<A>() -> A { fail; }
fn shift() {
    let s = to_any_type();
    3 >> s
}

conceivably this might have type checked before with s as type int. It certainly won't now... but do we really care?

@brson
Copy link
Contributor Author

brson commented Feb 22, 2012

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).

@nikomatsakis
Copy link
Contributor

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:

unsafe fn foo(x: *T) {
    let y = unsafe::reinterpret_cast::<int>(x);
    32 >> y
}

Here the type annotation is now required. This is sufficiently bizarre that I would hope you would annotate your type anyhow! :)

@graydon
Copy link
Contributor

graydon commented Feb 23, 2012

Yeah, this is ok. Requiring the LHS and RHS operands be "of the same type" makes sense when the operator is either:

  • Calculating a result bit-by-bit from LHS bits and RHS bits (and/or/xor)
  • A ring operator, subject to expression-reasoning by ring axioms (add,sub,mul,div,mod,comparisons)

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.)

@brson brson closed this Feb 23, 2012
celinval added a commit to celinval/rust-dev that referenced this pull request Jun 4, 2024
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.
Kobzol pushed a commit to Kobzol/rust that referenced this pull request Dec 30, 2024
bors pushed a commit to rust-lang-ci/rust that referenced this pull request Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants