You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This strip of code causes UB when used on a non-Copy type:
structTest<T>(*constT);impl<T>Test<T>{fnnew(iref:&T) -> Self{Self(iref as*constT)}}fnmain(){let foo = vec![7u8];println!("Address of foo: Vec<u8> -> {:p}", &foo);let test = Test::new(&foo);println!("testref located at {:p}, foo at {:p}", test.0, &foo);let bar = foo;println!("Moved foo into bar");println!("testref located at {:p}, bar at {:p}", test.0, &bar);println!("Contents of foo using testref -> {:?}", unsafe{ &*test.0});}
$ cargo +nightly miri run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
Running `/home/kyleg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner target/miri/x86_64-unknown-linux-gnu/debug/tmiri`
Address of foo: Vec<u8> -> 0x25b68
testref located at 0x25b68, foo at 0x25b68
Moved foo into bar
testref located at 0x25b68, bar at 0x41b68
Contents of foo using testref -> [7]
miri does not complain about dereferencing a moved pointer (moving foo to bar) despite test still holding reference &foo.
This is behaving as intended. There are two ways this could be UB, rust-lang/unsafe-code-guidelines#188 and by foo reaching the end of its lifetime. However, current MIR generation makes foo live until the end of its scope. This is not a guarantee, this program might become UB in future rustc versions, and then Miri will detect it as such.
This unique case of UB was discussed in the Rust Community Discord. Here's a link to the message that started the discussion.
This strip of code causes UB when used on a non-
Copy
type:miri
does not complain about dereferencing a moved pointer (movingfoo
tobar
) despitetest
still holding reference&foo
.Might be related to (or a duplicate of) rust-lang/unsafe-code-guidelines#188?
The text was updated successfully, but these errors were encountered: