-
Notifications
You must be signed in to change notification settings - Fork 13.4k
MaybeUninit::copy/clone_from_slice #79607
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use core::mem::*; | ||
|
||
use std::rc::Rc; | ||
|
||
#[test] | ||
fn size_of_basic() { | ||
assert_eq!(size_of::<u8>(), 1); | ||
|
@@ -137,3 +139,125 @@ fn assume_init_good() { | |
|
||
assert!(TRUE); | ||
} | ||
|
||
#[test] | ||
fn uninit_write_slice() { | ||
let mut dst = [MaybeUninit::new(255); 64]; | ||
let src = [0; 64]; | ||
|
||
assert_eq!(MaybeUninit::write_slice(&mut dst, &src), &src); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "source slice length (32) does not match destination slice length (64)")] | ||
fn uninit_write_slice_panic_lt() { | ||
let mut dst = [MaybeUninit::uninit(); 64]; | ||
let src = [0; 32]; | ||
|
||
MaybeUninit::write_slice(&mut dst, &src); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "source slice length (128) does not match destination slice length (64)")] | ||
fn uninit_write_slice_panic_gt() { | ||
let mut dst = [MaybeUninit::uninit(); 64]; | ||
let src = [0; 128]; | ||
|
||
MaybeUninit::write_slice(&mut dst, &src); | ||
} | ||
|
||
#[test] | ||
fn uninit_clone_from_slice() { | ||
let mut dst = [MaybeUninit::new(255); 64]; | ||
let src = [0; 64]; | ||
|
||
assert_eq!(MaybeUninit::write_slice_cloned(&mut dst, &src), &src); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "destination and source slices have different lengths")] | ||
fn uninit_write_slice_cloned_panic_lt() { | ||
let mut dst = [MaybeUninit::uninit(); 64]; | ||
let src = [0; 32]; | ||
|
||
MaybeUninit::write_slice_cloned(&mut dst, &src); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "destination and source slices have different lengths")] | ||
fn uninit_write_slice_cloned_panic_gt() { | ||
let mut dst = [MaybeUninit::uninit(); 64]; | ||
let src = [0; 128]; | ||
|
||
MaybeUninit::write_slice_cloned(&mut dst, &src); | ||
} | ||
|
||
#[test] | ||
#[cfg(panic = "unwind")] | ||
fn uninit_write_slice_cloned_mid_panic() { | ||
use std::panic; | ||
|
||
enum IncrementOrPanic { | ||
Increment(Rc<()>), | ||
ExpectedPanic, | ||
UnexpectedPanic, | ||
} | ||
|
||
impl Clone for IncrementOrPanic { | ||
fn clone(&self) -> Self { | ||
match self { | ||
Self::Increment(rc) => Self::Increment(rc.clone()), | ||
Self::ExpectedPanic => panic!("expected panic on clone"), | ||
Self::UnexpectedPanic => panic!("unexpected panic on clone"), | ||
} | ||
} | ||
} | ||
|
||
let rc = Rc::new(()); | ||
|
||
let mut dst = [ | ||
MaybeUninit::uninit(), | ||
MaybeUninit::uninit(), | ||
MaybeUninit::uninit(), | ||
MaybeUninit::uninit(), | ||
]; | ||
|
||
let src = [ | ||
IncrementOrPanic::Increment(rc.clone()), | ||
IncrementOrPanic::Increment(rc.clone()), | ||
IncrementOrPanic::ExpectedPanic, | ||
IncrementOrPanic::UnexpectedPanic, | ||
]; | ||
|
||
let err = panic::catch_unwind(panic::AssertUnwindSafe(|| { | ||
MaybeUninit::write_slice_cloned(&mut dst, &src); | ||
})); | ||
|
||
drop(src); | ||
|
||
match err { | ||
Ok(_) => unreachable!(), | ||
Err(payload) => { | ||
payload | ||
.downcast::<&'static str>() | ||
.and_then(|s| if *s == "expected panic on clone" { Ok(s) } else { Err(s) }) | ||
.unwrap_or_else(|p| panic::resume_unwind(p)); | ||
|
||
assert_eq!(Rc::strong_count(&rc), 1) | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn uninit_write_slice_cloned_no_drop() { | ||
let rc = Rc::new(()); | ||
|
||
let mut dst = [MaybeUninit::uninit()]; | ||
let src = [rc.clone()]; | ||
|
||
MaybeUninit::write_slice_cloned(&mut dst, &src); | ||
|
||
drop(src); | ||
|
||
assert_eq!(Rc::strong_count(&rc), 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test made Miri's run on the test suite fail, because it introduced a memory leak... can the test be adjusted to not leak memory? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the goal is just to test that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I confirmed that this function is the culprit, so I opened an issue: #80116 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RalfJung Thanks for debugging this! I'll watch for leaks in tests more closely in future reviews. :) @drmeepster Do you feel like changing it to a panic-on-drop to avoid the leak? Otherwise I can also make some time for this. ^^ |
||
} |
Uh oh!
There was an error while loading. Please reload this page.