about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-08-20 22:21:56 +0200
committerGitHub <noreply@github.com>2024-08-20 22:21:56 +0200
commit552b5c73fb2fca2062f9795eb9c7b7c0daf7e5dd (patch)
treed3b97a2eeffb27ebe430a560623fb85c4dbef9df /tests
parent4d5b3b196284aded6ae99d12bcf149ffdc8ef379 (diff)
parentef25fbd0b4ffa79cdf34a73795bbfbe8ebe6e267 (diff)
downloadrust-552b5c73fb2fca2062f9795eb9c7b7c0daf7e5dd.tar.gz
rust-552b5c73fb2fca2062f9795eb9c7b7c0daf7e5dd.zip
Rollup merge of #128662 - dingxiangfei2009:lint-tail-expr-drop-order, r=jieyouxu
Lint on tail expr drop order change in Edition 2024

This lint warns users to consider extra discretion on the effect of a transposed drop order arising from Edition 2024, which involves temporaries in tail expression location with significant drop implementation.

cc `@traviscross`

Tracking:

- https://github.com/rust-lang/rust/issues/123739
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/drop/lint-tail-expr-drop-order-gated.rs35
-rw-r--r--tests/ui/drop/lint-tail-expr-drop-order.rs71
-rw-r--r--tests/ui/drop/lint-tail-expr-drop-order.stderr42
3 files changed, 148 insertions, 0 deletions
diff --git a/tests/ui/drop/lint-tail-expr-drop-order-gated.rs b/tests/ui/drop/lint-tail-expr-drop-order-gated.rs
new file mode 100644
index 00000000000..b22e72bcfad
--- /dev/null
+++ b/tests/ui/drop/lint-tail-expr-drop-order-gated.rs
@@ -0,0 +1,35 @@
+// This test ensures that `tail_expr_drop_order` does not activate in case Edition 2024 is not used
+// or the feature gate `shorter_tail_lifetimes` is disabled.
+
+//@ revisions: neither no_feature_gate edition_less_than_2024
+//@ check-pass
+//@ [neither] edition: 2021
+//@ [no_feature_gate] compile-flags: -Z unstable-options
+//@ [no_feature_gate] edition: 2024
+//@ [edition_less_than_2024] edition: 2021
+
+#![deny(tail_expr_drop_order)]
+#![cfg_attr(edition_less_than_2024, feature(shorter_tail_lifetimes))]
+
+struct LoudDropper;
+impl Drop for LoudDropper {
+    fn drop(&mut self) {
+        // This destructor should be considered significant because it is a custom destructor
+        // and we will assume that the destructor can generate side effects arbitrarily so that
+        // a change in drop order is visible.
+        println!("loud drop");
+    }
+}
+impl LoudDropper {
+    fn get(&self) -> i32 {
+        0
+    }
+}
+
+fn should_not_lint() -> i32 {
+    let x = LoudDropper;
+    x.get() + LoudDropper.get()
+    // Lint should not action
+}
+
+fn main() {}
diff --git a/tests/ui/drop/lint-tail-expr-drop-order.rs b/tests/ui/drop/lint-tail-expr-drop-order.rs
new file mode 100644
index 00000000000..0aa0ef02610
--- /dev/null
+++ b/tests/ui/drop/lint-tail-expr-drop-order.rs
@@ -0,0 +1,71 @@
+//@ compile-flags: -Z unstable-options
+//@ edition: 2024
+
+// Edition 2024 lint for change in drop order at tail expression
+// This lint is to capture potential change in program semantics
+// due to implementation of RFC 3606 <https://github.com/rust-lang/rfcs/pull/3606>
+
+#![deny(tail_expr_drop_order)]
+#![feature(shorter_tail_lifetimes)]
+
+struct LoudDropper;
+impl Drop for LoudDropper {
+    fn drop(&mut self) {
+        // This destructor should be considered significant because it is a custom destructor
+        // and we will assume that the destructor can generate side effects arbitrarily so that
+        // a change in drop order is visible.
+        println!("loud drop");
+    }
+}
+impl LoudDropper {
+    fn get(&self) -> i32 {
+        0
+    }
+}
+
+fn should_lint() -> i32 {
+    let x = LoudDropper;
+    // Should lint
+    x.get() + LoudDropper.get()
+    //~^ ERROR: these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
+    //~| WARN: this changes meaning in Rust 2024
+}
+
+fn should_lint_closure() -> impl FnOnce() -> i32 {
+    let x = LoudDropper;
+    move || x.get() + LoudDropper.get()
+    //~^ ERROR: these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
+    //~| WARN: this changes meaning in Rust 2024
+}
+
+fn should_not_lint() -> i32 {
+    let x = LoudDropper;
+    // Should not lint
+    x.get()
+}
+
+fn should_not_lint_in_nested_block() -> i32 {
+    let x = LoudDropper;
+    // Should not lint because Edition 2021 drops temporaries in blocks earlier already
+    { LoudDropper.get() }
+}
+
+fn should_not_lint_in_match_arm() -> i32 {
+    let x = LoudDropper;
+    // Should not lint because Edition 2021 drops temporaries in blocks earlier already
+    match &x {
+        _ => LoudDropper.get(),
+    }
+}
+
+fn should_lint_in_nested_items() {
+    fn should_lint_me() -> i32 {
+        let x = LoudDropper;
+        // Should lint
+        x.get() + LoudDropper.get()
+        //~^ ERROR: these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
+        //~| WARN: this changes meaning in Rust 2024
+    }
+}
+
+fn main() {}
diff --git a/tests/ui/drop/lint-tail-expr-drop-order.stderr b/tests/ui/drop/lint-tail-expr-drop-order.stderr
new file mode 100644
index 00000000000..630f0a80f09
--- /dev/null
+++ b/tests/ui/drop/lint-tail-expr-drop-order.stderr
@@ -0,0 +1,42 @@
+error: these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
+  --> $DIR/lint-tail-expr-drop-order.rs:29:15
+   |
+LL |     let x = LoudDropper;
+   |         - these values have significant drop implementation and will observe changes in drop order under Edition 2024
+LL |     // Should lint
+LL |     x.get() + LoudDropper.get()
+   |               ^^^^^^^^^^^
+   |
+   = warning: this changes meaning in Rust 2024
+   = note: for more information, see issue #123739 <https://github.com/rust-lang/rust/issues/123739>
+note: the lint level is defined here
+  --> $DIR/lint-tail-expr-drop-order.rs:8:9
+   |
+LL | #![deny(tail_expr_drop_order)]
+   |         ^^^^^^^^^^^^^^^^^^^^
+
+error: these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
+  --> $DIR/lint-tail-expr-drop-order.rs:36:23
+   |
+LL |     let x = LoudDropper;
+   |         - these values have significant drop implementation and will observe changes in drop order under Edition 2024
+LL |     move || x.get() + LoudDropper.get()
+   |                       ^^^^^^^^^^^
+   |
+   = warning: this changes meaning in Rust 2024
+   = note: for more information, see issue #123739 <https://github.com/rust-lang/rust/issues/123739>
+
+error: these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
+  --> $DIR/lint-tail-expr-drop-order.rs:65:19
+   |
+LL |         let x = LoudDropper;
+   |             - these values have significant drop implementation and will observe changes in drop order under Edition 2024
+LL |         // Should lint
+LL |         x.get() + LoudDropper.get()
+   |                   ^^^^^^^^^^^
+   |
+   = warning: this changes meaning in Rust 2024
+   = note: for more information, see issue #123739 <https://github.com/rust-lang/rust/issues/123739>
+
+error: aborting due to 3 previous errors
+