Skip to content
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

checked arithmetic not treated as constant in debug builds #38074

Closed
BurntSushi opened this issue Nov 29, 2016 · 3 comments · Fixed by #38837
Closed

checked arithmetic not treated as constant in debug builds #38074

BurntSushi opened this issue Nov 29, 2016 · 3 comments · Fixed by #38837
Assignees

Comments

@BurntSushi
Copy link
Member

This code produces a compiler error with rustc foo.rs but compiles successfully with rustc -O foo.rs:

#![feature(platform_intrinsics, repr_simd)]

extern "platform-intrinsic" {
    fn simd_shuffle2<T, U>(x: T, y: T, idx: [u32; 2]) -> U;
}

#[repr(simd)]
#[derive(Clone, Copy)]
#[allow(non_camel_case_types)]
struct u64x2(u64, u64);

fn main() {
    let a = u64x2(1, 2);
    let r: u64x2 = unsafe { simd_shuffle2(a, a, [0-0, 0-0]) };
    assert_eq!(r.0, 1);
    assert_eq!(r.1, 1);
}

The error is:

$ rustc shuffle.rs 
error[E0526]: shuffle indices are not constant
  --> shuffle.rs:14:29
   |
14 |     let r: u64x2 = unsafe { simd_shuffle2(a, a, [0-0, 0-0]) };
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
@BurntSushi
Copy link
Member Author

cc @eddyb

@eddyb
Copy link
Member

eddyb commented Dec 11, 2016

This has to do with how the result of a checked op is a tuple and the 2's complement wrapping value is accessed through temp.0: the fix would be to track an initialization with a checked op in a field of TempState::Defined, then treat temp.0 as constant if temp is promotable and has that flag.

I believe that the promoter will work as-is, it's just the constant qualifier that needs to understand this.

@eddyb
Copy link
Member

eddyb commented Jan 4, 2017

Looking at it again, the const qualifier works fine with fields, but the promoter's initial set of potentially promotable temporaries doesn't include the tuple because one of the uses is by-field.

bors added a commit that referenced this issue Jan 8, 2017
Allow projections to be promoted to constants in MIR.

This employs the `LvalueContext` additions by @pcwalton to properly extend the MIR promotion of temporaries to allow projections (field accesses, indexing and dereferences) on said temporaries.

It's needed both parity with the old constant qualification logic (for current borrowck) and it fixes #38074.
The former is *required for soundness* if we accept the RFC for promoting rvalues to `'static` constants.
That is, until we get MIR borrowck and the same source of truth will be used for both checks and codegen.
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 a pull request may close this issue.

2 participants