summaryrefslogtreecommitdiff
path: root/tests/ui/drop
diff options
context:
space:
mode:
authorDing Xiang Fei <dingxiangfei2009@protonmail.ch>2024-04-12 23:24:45 +0800
committerDing Xiang Fei <dingxiangfei2009@protonmail.ch>2024-06-18 04:14:43 +0800
commit0f8c3f78825a1b6eb765b97f658b92252b55c5df (patch)
tree825b35aea3cd1ebbe53c2e9aaeec6012c30cb494 /tests/ui/drop
parent11380368dc53d0b2fc3a627408818eff1973ce9a (diff)
tail expression behind terminating scope
Diffstat (limited to 'tests/ui/drop')
-rw-r--r--tests/ui/drop/auxiliary/edition-2021-macros.rs8
-rw-r--r--tests/ui/drop/auxiliary/edition-2024-macros.rs9
-rw-r--r--tests/ui/drop/tail-expr-drop-order-negative.edition2024.stderr16
-rw-r--r--tests/ui/drop/tail-expr-drop-order-negative.rs17
-rw-r--r--tests/ui/drop/tail-expr-drop-order.rs108
5 files changed, 158 insertions, 0 deletions
diff --git a/tests/ui/drop/auxiliary/edition-2021-macros.rs b/tests/ui/drop/auxiliary/edition-2021-macros.rs
new file mode 100644
index 00000000000..8a6444f8614
--- /dev/null
+++ b/tests/ui/drop/auxiliary/edition-2021-macros.rs
@@ -0,0 +1,8 @@
+//@ edition:2021
+
+#[macro_export]
+macro_rules! edition_2021_block {
+ ($($c:tt)*) => {
+ { $($c)* }
+ }
+}
diff --git a/tests/ui/drop/auxiliary/edition-2024-macros.rs b/tests/ui/drop/auxiliary/edition-2024-macros.rs
new file mode 100644
index 00000000000..236340bfed4
--- /dev/null
+++ b/tests/ui/drop/auxiliary/edition-2024-macros.rs
@@ -0,0 +1,9 @@
+//@ edition:2024
+//@ compile-flags: -Zunstable-options
+
+#[macro_export]
+macro_rules! edition_2024_block {
+ ($($c:tt)*) => {
+ { $($c)* }
+ }
+}
diff --git a/tests/ui/drop/tail-expr-drop-order-negative.edition2024.stderr b/tests/ui/drop/tail-expr-drop-order-negative.edition2024.stderr
new file mode 100644
index 00000000000..75fc34e409b
--- /dev/null
+++ b/tests/ui/drop/tail-expr-drop-order-negative.edition2024.stderr
@@ -0,0 +1,16 @@
+error[E0716]: temporary value dropped while borrowed
+ --> $DIR/tail-expr-drop-order-negative.rs:11:15
+ |
+LL | x.replace(std::cell::RefCell::new(123).borrow()).is_some()
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
+ | |
+ | creates a temporary value which is freed while still in use
+LL |
+LL | }
+ | - borrow might be used here, when `x` is dropped and runs the destructor for type `Option<Ref<'_, i32>>`
+ |
+ = note: consider using a `let` binding to create a longer lived value
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0716`.
diff --git a/tests/ui/drop/tail-expr-drop-order-negative.rs b/tests/ui/drop/tail-expr-drop-order-negative.rs
new file mode 100644
index 00000000000..c570b3a1ee2
--- /dev/null
+++ b/tests/ui/drop/tail-expr-drop-order-negative.rs
@@ -0,0 +1,17 @@
+//@ revisions: edition2021 edition2024
+//@ [edition2024] compile-flags: -Zunstable-options
+//@ [edition2024] edition: 2024
+//@ [edition2021] check-pass
+
+#![feature(shorter_tail_lifetimes)]
+
+fn why_would_you_do_this() -> bool {
+ let mut x = None;
+ // Make a temporary `RefCell` and put a `Ref` that borrows it in `x`.
+ x.replace(std::cell::RefCell::new(123).borrow()).is_some()
+ //[edition2024]~^ ERROR: temporary value dropped while borrowed
+}
+
+fn main() {
+ why_would_you_do_this();
+}
diff --git a/tests/ui/drop/tail-expr-drop-order.rs b/tests/ui/drop/tail-expr-drop-order.rs
new file mode 100644
index 00000000000..5d87f980b15
--- /dev/null
+++ b/tests/ui/drop/tail-expr-drop-order.rs
@@ -0,0 +1,108 @@
+//@ aux-build:edition-2021-macros.rs
+//@ aux-build:edition-2024-macros.rs
+//@ compile-flags: -Z validate-mir -Zunstable-options
+//@ edition: 2024
+//@ run-pass
+
+#![feature(shorter_tail_lifetimes)]
+#![allow(unused_imports)]
+#![allow(dead_code)]
+#![allow(unused_variables)]
+
+#[macro_use]
+extern crate edition_2021_macros;
+#[macro_use]
+extern crate edition_2024_macros;
+use std::cell::RefCell;
+use std::convert::TryInto;
+
+#[derive(Default)]
+struct DropOrderCollector(RefCell<Vec<u32>>);
+
+struct LoudDrop<'a>(&'a DropOrderCollector, u32);
+
+impl Drop for LoudDrop<'_> {
+ fn drop(&mut self) {
+ println!("{}", self.1);
+ self.0.0.borrow_mut().push(self.1);
+ }
+}
+
+impl DropOrderCollector {
+ fn option_loud_drop(&self, n: u32) -> Option<LoudDrop> {
+ Some(LoudDrop(self, n))
+ }
+
+ fn loud_drop(&self, n: u32) -> LoudDrop {
+ LoudDrop(self, n)
+ }
+
+ fn assert_sorted(&self, expected: usize) {
+ let result = self.0.borrow();
+ assert_eq!(result.len(), expected);
+ for i in 1..result.len() {
+ assert!(
+ result[i - 1] < result[i],
+ "inversion at {} ({} followed by {})",
+ i - 1,
+ result[i - 1],
+ result[i]
+ );
+ }
+ }
+}
+
+fn edition_2021_around_2021() {
+ let c = DropOrderCollector::default();
+ let _ = edition_2021_block! {
+ let a = c.loud_drop(1);
+ edition_2021_block! {
+ let b = c.loud_drop(0);
+ c.loud_drop(2).1
+ }
+ };
+ c.assert_sorted(3);
+}
+
+fn edition_2021_around_2024() {
+ let c = DropOrderCollector::default();
+ let _ = edition_2021_block! {
+ let a = c.loud_drop(2);
+ edition_2024_block! {
+ let b = c.loud_drop(1);
+ c.loud_drop(0).1
+ }
+ };
+ c.assert_sorted(3);
+}
+
+fn edition_2024_around_2021() {
+ let c = DropOrderCollector::default();
+ let _ = edition_2024_block! {
+ let a = c.loud_drop(2);
+ edition_2021_block! {
+ let b = c.loud_drop(0);
+ c.loud_drop(1).1
+ }
+ };
+ c.assert_sorted(3);
+}
+
+fn edition_2024_around_2024() {
+ let c = DropOrderCollector::default();
+ let _ = edition_2024_block! {
+ let a = c.loud_drop(2);
+ edition_2024_block! {
+ let b = c.loud_drop(1);
+ c.loud_drop(0).1
+ }
+ };
+ c.assert_sorted(3);
+}
+
+fn main() {
+ edition_2021_around_2021();
+ edition_2021_around_2024();
+ edition_2024_around_2021();
+ edition_2024_around_2024();
+}