Skip to content

Implementing Drop leads to SIGILL: Illegal instruction in tests but not in normal run #82850

Open
@bluescreen303

Description

@bluescreen303

I tried this code:

pub struct Droppy<T, F: FnMut(&mut T)> {
    inner: T,
    callback: F,
}

impl<T, F: FnMut(&mut T)> Drop for Droppy<T, F> {
    fn drop(&mut self) {
        (self.callback)(&mut self.inner);
    }
}

impl<T, F: FnMut(&mut T)> Droppy<T, F> {
    pub fn new(inner: T, callback: F) -> Self {
        Droppy { inner, callback }
    }
    pub fn force(&mut self) {
        (self.callback)(&mut self.inner);
    }
}

fn main(){
    let mut d = Droppy::new(42, |_| assert_eq!(1,2));
    d.force();
}

#[test]
fn droppy() {
    main()
}

running this with cargo run works fine. I get an assertion failure because 1 isn't 2.

But when I run cargo test

I expected to see this happen:

assertion failure because 1 isn't 2

Instead, this happened:

running 1 test
thread panicked while panicking. aborting.
error: test failed, to rerun pass '--bin broken-case'

Caused by:
  process didn't exit successfully: `.../target/debug/deps/broken_case-77184cc870a0dee1` (signal: 4, SIGILL: illegal instruction)

Meta

rustc --version --verbose:

rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: x86_64-unknown-linux-gnu
release: 1.50.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions