diff options
| author | Kivooeo <Kivooeo123@gmail.com> | 2025-07-24 18:29:09 +0500 |
|---|---|---|
| committer | Kivooeo <Kivooeo123@gmail.com> | 2025-07-25 15:34:28 +0500 |
| commit | 90bb5cacb5c1a5fe20ba821d28e7eb7a21e35d09 (patch) | |
| tree | fa18c4e8fc12592084b8d16f71a8838e44a116ac /tests/ui/drop | |
| parent | 3c30dbbe31bfbf6029f4534170165ba573ff0fd1 (diff) | |
| download | rust-90bb5cacb5c1a5fe20ba821d28e7eb7a21e35d09.tar.gz rust-90bb5cacb5c1a5fe20ba821d28e7eb7a21e35d09.zip | |
moved 34 tests to organized locations
Diffstat (limited to 'tests/ui/drop')
| -rw-r--r-- | tests/ui/drop/early-return-drop-order.rs | 88 | ||||
| -rw-r--r-- | tests/ui/drop/enum-drop-impl-15063.rs | 12 | ||||
| -rw-r--r-- | tests/ui/drop/generic-drop-trait-bound-15858.rs | 35 | ||||
| -rw-r--r-- | tests/ui/drop/struct-field-drop-order.rs | 67 | ||||
| -rw-r--r-- | tests/ui/drop/vec-replace-skip-while-drop.rs | 32 |
5 files changed, 234 insertions, 0 deletions
diff --git a/tests/ui/drop/early-return-drop-order.rs b/tests/ui/drop/early-return-drop-order.rs new file mode 100644 index 00000000000..0ebadd80541 --- /dev/null +++ b/tests/ui/drop/early-return-drop-order.rs @@ -0,0 +1,88 @@ +//@ run-pass +#![allow(unreachable_code)] + +#[derive(PartialEq, Debug)] +struct Bar { + x: isize +} +impl Drop for Bar { + fn drop(&mut self) { + assert_eq!(self.x, 22); + } +} + +#[derive(PartialEq, Debug)] +struct Foo { + x: Bar, + a: isize +} + +fn foo() -> Result<Foo, isize> { + return Ok(Foo { + x: Bar { x: 22 }, + a: return Err(32) + }); +} + +fn baz() -> Result<Foo, isize> { + Ok(Foo { + x: Bar { x: 22 }, + a: return Err(32) + }) +} + +// explicit immediate return +fn aa() -> isize { + return 3; +} + +// implicit immediate return +fn bb() -> isize { + 3 +} + +// implicit outptr return +fn cc() -> Result<isize, isize> { + Ok(3) +} + +// explicit outptr return +fn dd() -> Result<isize, isize> { + return Ok(3); +} + +trait A { + fn aaa(&self) -> isize { + 3 + } + fn bbb(&self) -> isize { + return 3; + } + fn ccc(&self) -> Result<isize, isize> { + Ok(3) + } + fn ddd(&self) -> Result<isize, isize> { + return Ok(3); + } +} + +impl A for isize {} + +fn main() { + assert_eq!(foo(), Err(32)); + assert_eq!(baz(), Err(32)); + + assert_eq!(aa(), 3); + assert_eq!(bb(), 3); + assert_eq!(cc().unwrap(), 3); + assert_eq!(dd().unwrap(), 3); + + let i = Box::new(32isize) as Box<dyn A>; + assert_eq!(i.aaa(), 3); + let i = Box::new(32isize) as Box<dyn A>; + assert_eq!(i.bbb(), 3); + let i = Box::new(32isize) as Box<dyn A>; + assert_eq!(i.ccc().unwrap(), 3); + let i = Box::new(32isize) as Box<dyn A>; + assert_eq!(i.ddd().unwrap(), 3); +} diff --git a/tests/ui/drop/enum-drop-impl-15063.rs b/tests/ui/drop/enum-drop-impl-15063.rs new file mode 100644 index 00000000000..969dbe5fad2 --- /dev/null +++ b/tests/ui/drop/enum-drop-impl-15063.rs @@ -0,0 +1,12 @@ +//@ run-pass +#![allow(dead_code)] +#![allow(unused_variables)] +enum Two { A, B} +impl Drop for Two { + fn drop(&mut self) { + println!("Dropping!"); + } +} +fn main() { + let k = Two::A; +} diff --git a/tests/ui/drop/generic-drop-trait-bound-15858.rs b/tests/ui/drop/generic-drop-trait-bound-15858.rs new file mode 100644 index 00000000000..27887d49e3e --- /dev/null +++ b/tests/ui/drop/generic-drop-trait-bound-15858.rs @@ -0,0 +1,35 @@ +//@ run-pass +// FIXME(static_mut_refs): this could use an atomic +#![allow(static_mut_refs)] +static mut DROP_RAN: bool = false; + +trait Bar { + fn do_something(&mut self); //~ WARN method `do_something` is never used +} + +struct BarImpl; + +impl Bar for BarImpl { + fn do_something(&mut self) {} +} + + +struct Foo<B: Bar>(#[allow(dead_code)] B); + +impl<B: Bar> Drop for Foo<B> { + fn drop(&mut self) { + unsafe { + DROP_RAN = true; + } + } +} + + +fn main() { + { + let _x: Foo<BarImpl> = Foo(BarImpl); + } + unsafe { + assert_eq!(DROP_RAN, true); + } +} diff --git a/tests/ui/drop/struct-field-drop-order.rs b/tests/ui/drop/struct-field-drop-order.rs new file mode 100644 index 00000000000..cfdba5fda35 --- /dev/null +++ b/tests/ui/drop/struct-field-drop-order.rs @@ -0,0 +1,67 @@ +//@ run-pass +#![allow(non_snake_case)] + +use std::rc::Rc; +use std::cell::Cell; + +struct Field { + number: usize, + state: Rc<Cell<usize>> +} + +impl Field { + fn new(number: usize, state: Rc<Cell<usize>>) -> Field { + Field { + number: number, + state: state + } + } +} + +impl Drop for Field { + fn drop(&mut self) { + println!("Dropping field {}", self.number); + assert_eq!(self.state.get(), self.number); + self.state.set(self.state.get()+1); + } +} + +struct NoDropImpl { + _one: Field, + _two: Field, + _three: Field +} + +struct HasDropImpl { + _one: Field, + _two: Field, + _three: Field +} + +impl Drop for HasDropImpl { + fn drop(&mut self) { + println!("HasDropImpl.drop()"); + assert_eq!(self._one.state.get(), 0); + self._one.state.set(1); + } +} + +pub fn main() { + let state = Rc::new(Cell::new(1)); + let noImpl = NoDropImpl { + _one: Field::new(1, state.clone()), + _two: Field::new(2, state.clone()), + _three: Field::new(3, state.clone()) + }; + drop(noImpl); + assert_eq!(state.get(), 4); + + state.set(0); + let hasImpl = HasDropImpl { + _one: Field::new(1, state.clone()), + _two: Field::new(2, state.clone()), + _three: Field::new(3, state.clone()) + }; + drop(hasImpl); + assert_eq!(state.get(), 4); +} diff --git a/tests/ui/drop/vec-replace-skip-while-drop.rs b/tests/ui/drop/vec-replace-skip-while-drop.rs new file mode 100644 index 00000000000..b18108e0a8a --- /dev/null +++ b/tests/ui/drop/vec-replace-skip-while-drop.rs @@ -0,0 +1,32 @@ +//@ run-pass + +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + +use std::mem; + +static mut DROP_COUNT: usize = 0; + +struct Fragment; + +impl Drop for Fragment { + fn drop(&mut self) { + unsafe { + DROP_COUNT += 1; + } + } +} + +fn main() { + { + let mut fragments = vec![Fragment, Fragment, Fragment]; + let _new_fragments: Vec<Fragment> = mem::replace(&mut fragments, vec![]) + .into_iter() + .skip_while(|_fragment| { + true + }).collect(); + } + unsafe { + assert_eq!(DROP_COUNT, 3); + } +} |
