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

Target feature unavailable only in doctest #3942

Closed
DaniPopes opened this issue Oct 4, 2024 · 2 comments
Closed

Target feature unavailable only in doctest #3942

DaniPopes opened this issue Oct 4, 2024 · 2 comments

Comments

@DaniPopes
Copy link

DaniPopes commented Oct 4, 2024

Using std::arch::is_<arch>_feature_detected!("feature") + #[target_feature = "feature"] doesn't work in doctests:

/// ```
/// let result = miri_doctest::add(2, 2);
/// assert_eq!(result, 4);
/// ```
pub fn add(left: u64, right: u64) -> u64 {
    if !std::arch::is_x86_feature_detected!("ssse3") {
        panic!("SSSE3 is not supported");
    }
    unsafe { add_with_ssse3(left, right) }
}

#[target_feature(enable = "ssse3")]
pub unsafe fn add_with_ssse3(left: u64, right: u64) -> u64 {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

The #[test] it_works works fine, but the add doctest doesn't; looks like the is_feature_detected call returns true but Miri still errors out:

$ RUSTFLAGS=-Ctarget-feature=+ssse3 cargo miri test --target x86_64-unknown-linux-gnu
   Compiling miri-doctest v0.1.0 (/tmp/miri-doctest)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.08s
     Running unittests src/lib.rs (target/miri/x86_64-unknown-linux-gnu/debug/deps/miri_doctest-0d5da2e0254807af)

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.17s

   Doc-tests miri_doctest

running 1 test
test src/lib.rs - add (line 1) ... FAILED

failures:

---- src/lib.rs - add (line 1) stdout ----
Test executable failed (exit status: 1).

stderr:
error: Undefined Behavior: calling a function that requires unavailable target features: ssse3
 --> /tmp/miri-doctest/src/lib.rs:9:14
  |
9 |     unsafe { add_with_ssse3(left, right) }
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function that requires unavailable target features: ssse3
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
  = note: BACKTRACE:
  = note: inside `miri_doctest::add` at /tmp/miri-doctest/src/lib.rs:9:14: 9:41
note: inside `main::_doctest_main_src_lib_rs_1_0`
 --> src/lib.rs:3:14
  |
5 | let result = miri_doctest::add(2, 2);
  |              ^^^^^^^^^^^^^^^^^^^^^^^
note: inside `main`
 --> src/lib.rs:5:3
  |
7 | } _doctest_main_src_lib_rs_1_0() }
  |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error




failures:
    src/lib.rs - add (line 1)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.08s

error: doctest failed, to rerun pass `--doc`
@saethlin
Copy link
Member

saethlin commented Oct 4, 2024

This is a rustdoc footgun, not a Miri issue. RUSTFLAGS are not passed to rustdoc, you need to set RUSTDOCFLAGS as well. For example: https://github.com/saethlin/crater-at-home/blob/e8145d70bb1a3243d882887b15910a45f4940dec/docker/run.sh#L37

I do not know why rustdoc behaves like this, but I suspect there was a good reason for it at some point. If you figure out the reason, don't hesitate to give a shout on this issue, it's probably buried in the rust-lang/rust issue tracker.

@saethlin saethlin closed this as completed Oct 4, 2024
@RalfJung
Copy link
Member

RalfJung commented Oct 5, 2024

Also see #3869, of which this is a duplicate.

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

No branches or pull requests

3 participants