rust-lang / miri

An interpreter for Rust's mid-level intermediate representation
Apache License 2.0
4.44k stars 340 forks source link

atomic_xadd::<f64> fails with rustc, succeeds with miri #181

Closed dwrensha closed 5 years ago

dwrensha commented 7 years ago

Consider the following program:

// test.rs

#![feature(core_intrinsics)]

pub fn main() {
    let mut z: f64 = 1.0;
    unsafe {
        ::std::intrinsics::atomic_xadd(&mut z, 2.0);
    }
}

When I try to run test.rs with rustc, I get a monomorphization error:

$ rustc test.rs
error[E0511]: invalid monomorphization of `atomic_xadd` intrinsic: expected basic integer type, found `f64`
 --> test.rs:8:9
  |
8 |         ::std::intrinsics::atomic_xadd(&mut z, 2.0);
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error(s)

However, when I run test.rs with miri, I expect to see a similar error, but instead the program finishes successfully:

$ cargo run --bin miri -- test.rs 
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/miri test.rs`

A possible solution might be to add some typechecking in intrinsic.rs to match the logic in rustc_trans::intrinsic.

It would be even better if rustc could somehow catch these errors during typechecking, so that miri could inherit the error handling.

eddyb commented 7 years ago

To catch these errors during type-checking we have to change how we do intrinsics and that's a bit tricky.

oli-obk commented 7 years ago

Is the tricky part the platform dependence? Some intrinsics for specific types work only on selected platforms?

eddyb commented 7 years ago

Nah, it's about changing intrinsics from abusing FFI to annotating free functions and methods instead.