From 86b8a2198ad3e75d906431b9eb675ea99a6de84e Mon Sep 17 00:00:00 2001 From: est31 Date: Tue, 11 Apr 2023 01:21:00 +0200 Subject: [PATCH 1/2] Turn pointer_structural_match forward compat lint into normal lint This commit removes the pointer_structural_match forward compat lint from the forward compatibility warning mechanism, as there is no concrete plan for removal of matching on function pointers. It also turns the lint from allow-by-default to warn-by-default, as the behaviour might be surprising to users and they should specifically opt into the dangers. As the lint is recognized on older compilers, users can easily allow it after having made the descision on whether they want to keep it in their code or not. --- compiler/rustc_lint_defs/src/builtin.rs | 19 ++++++++----------- library/core/src/marker.rs | 1 + .../ui/consts/const_in_pattern/issue-44333.rs | 4 ---- .../const_in_pattern/issue-44333.stderr | 15 +++------------ tests/ui/consts/issue-34784.rs | 2 +- tests/ui/pattern/usefulness/consts-opaque.rs | 2 +- ...-hide-behind-direct-unsafe-ptr-embedded.rs | 2 +- ...low-hide-behind-direct-unsafe-ptr-param.rs | 2 +- ...ide-behind-indirect-unsafe-ptr-embedded.rs | 2 +- ...w-hide-behind-indirect-unsafe-ptr-param.rs | 2 +- .../fn-ptr-is-structurally-matchable.rs | 2 ++ .../issue-63479-match-fnptr.rs | 3 --- .../issue-63479-match-fnptr.stderr | 10 ++-------- 13 files changed, 22 insertions(+), 44 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 6fe15e21d948d..93d82e8971ecd 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -2277,19 +2277,16 @@ declare_lint! { /// /// ### Explanation /// - /// Previous versions of Rust allowed function pointers and wide raw pointers in patterns. - /// While these work in many cases as expected by users, it is possible that due to - /// optimizations pointers are "not equal to themselves" or pointers to different functions - /// compare as equal during runtime. This is because LLVM optimizations can deduplicate - /// functions if their bodies are the same, thus also making pointers to these functions point - /// to the same location. Additionally functions may get duplicated if they are instantiated + /// Use of function pointers and wide raw pointers in patterns works in many cases as + /// expected by users. However, it is possible that due to optimizations pointers are "not equal + /// to themselves" or pointers to different functions compare as equal during runtime. + /// This is because LLVM optimizations can deduplicate functions if their bodies are the same, + /// thus also making pointers to these functions point to the same location. + /// Additionally, functions may get duplicated if they are instantiated /// in different crates and not deduplicated again via LTO. pub POINTER_STRUCTURAL_MATCH, - Allow, - "pointers are not structural-match", - @future_incompatible = FutureIncompatibleInfo { - reference: "issue #62411 ", - }; + Warn, + "pointers are not structural-match" } declare_lint! { diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 52f3d208aba4e..d931c2fb193cf 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -243,6 +243,7 @@ pub trait StructuralPartialEq { /// /// const CFN: Wrap = Wrap(higher_order); /// +/// # #[allow(pointer_structural_match)] /// fn main() { /// match CFN { /// CFN => {} diff --git a/tests/ui/consts/const_in_pattern/issue-44333.rs b/tests/ui/consts/const_in_pattern/issue-44333.rs index 96e8795e52d56..de9cea053c9fb 100644 --- a/tests/ui/consts/const_in_pattern/issue-44333.rs +++ b/tests/ui/consts/const_in_pattern/issue-44333.rs @@ -1,7 +1,5 @@ // run-pass -#![warn(pointer_structural_match)] - type Func = fn(usize, usize) -> usize; fn foo(a: usize, b: usize) -> usize { a + b } @@ -17,9 +15,7 @@ const BAR: Func = bar; fn main() { match test(std::env::consts::ARCH.len()) { FOO => println!("foo"), //~ WARN pointers in patterns behave unpredictably - //~^ WARN will become a hard error BAR => println!("bar"), //~ WARN pointers in patterns behave unpredictably - //~^ WARN will become a hard error _ => unreachable!(), } } diff --git a/tests/ui/consts/const_in_pattern/issue-44333.stderr b/tests/ui/consts/const_in_pattern/issue-44333.stderr index 731ef509ccad4..15c3917fffce2 100644 --- a/tests/ui/consts/const_in_pattern/issue-44333.stderr +++ b/tests/ui/consts/const_in_pattern/issue-44333.stderr @@ -1,25 +1,16 @@ warning: function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. - --> $DIR/issue-44333.rs:19:9 + --> $DIR/issue-44333.rs:17:9 | LL | FOO => println!("foo"), | ^^^ | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #62411 -note: the lint level is defined here - --> $DIR/issue-44333.rs:3:9 - | -LL | #![warn(pointer_structural_match)] - | ^^^^^^^^^^^^^^^^^^^^^^^^ + = note: `#[warn(pointer_structural_match)]` on by default warning: function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. - --> $DIR/issue-44333.rs:21:9 + --> $DIR/issue-44333.rs:18:9 | LL | BAR => println!("bar"), | ^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #62411 warning: 2 warnings emitted diff --git a/tests/ui/consts/issue-34784.rs b/tests/ui/consts/issue-34784.rs index 98d943470a7f9..f454acf877399 100644 --- a/tests/ui/consts/issue-34784.rs +++ b/tests/ui/consts/issue-34784.rs @@ -1,6 +1,6 @@ // run-pass -#![warn(pointer_structural_match)] + #![allow(dead_code)] const C: *const u8 = &0; diff --git a/tests/ui/pattern/usefulness/consts-opaque.rs b/tests/ui/pattern/usefulness/consts-opaque.rs index ca4fcd85bb6df..70945603b4c20 100644 --- a/tests/ui/pattern/usefulness/consts-opaque.rs +++ b/tests/ui/pattern/usefulness/consts-opaque.rs @@ -2,7 +2,7 @@ // unnecessary warnings because const_to_pat.rs converts a constant pattern to a wildcard when the // constant is not allowed as a pattern. This is an edge case so we may not care to fix it. // See also https://github.com/rust-lang/rust/issues/78057 - +#![allow(pointer_structural_match)] #![deny(unreachable_patterns)] #[derive(PartialEq)] diff --git a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-embedded.rs b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-embedded.rs index 1914e15549307..995a6ea9b5387 100644 --- a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-embedded.rs +++ b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-embedded.rs @@ -3,7 +3,7 @@ // run-pass -#![warn(pointer_structural_match)] +#![deny(pointer_structural_match)] struct NoDerive(#[allow(unused_tuple_struct_fields)] i32); diff --git a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-param.rs b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-param.rs index e713b003b0059..a0c0c69a84b6a 100644 --- a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-param.rs +++ b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-direct-unsafe-ptr-param.rs @@ -3,7 +3,7 @@ // run-pass -#![warn(pointer_structural_match)] +#![deny(pointer_structural_match)] struct NoDerive(#[allow(unused_tuple_struct_fields)] i32); diff --git a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-embedded.rs b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-embedded.rs index 04da14c54194a..18e9200940e56 100644 --- a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-embedded.rs +++ b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-embedded.rs @@ -3,7 +3,7 @@ // run-pass -#![warn(pointer_structural_match)] +#![deny(pointer_structural_match)] struct NoDerive(#[allow(unused_tuple_struct_fields)] i32); diff --git a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-param.rs b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-param.rs index 8313c25e7538d..f0f87de664253 100644 --- a/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-param.rs +++ b/tests/ui/rfc-1445-restrict-constants-in-patterns/allow-hide-behind-indirect-unsafe-ptr-param.rs @@ -3,7 +3,7 @@ // run-pass -#![warn(pointer_structural_match)] +#![deny(pointer_structural_match)] struct NoDerive(#[allow(unused_tuple_struct_fields)] i32); diff --git a/tests/ui/rfc-1445-restrict-constants-in-patterns/fn-ptr-is-structurally-matchable.rs b/tests/ui/rfc-1445-restrict-constants-in-patterns/fn-ptr-is-structurally-matchable.rs index 2b3fbd2a4d28a..0fc6b368bc554 100644 --- a/tests/ui/rfc-1445-restrict-constants-in-patterns/fn-ptr-is-structurally-matchable.rs +++ b/tests/ui/rfc-1445-restrict-constants-in-patterns/fn-ptr-is-structurally-matchable.rs @@ -3,6 +3,8 @@ // This file checks that fn ptrs are considered structurally matchable. // See also rust-lang/rust#63479. +#![allow(pointer_structural_match)] + fn main() { let mut count = 0; diff --git a/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.rs b/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.rs index 567685950e9e4..85fd7a8501513 100644 --- a/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.rs +++ b/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.rs @@ -5,8 +5,6 @@ // cover the case this hit; I've since expanded it accordingly, but the // experience left me wary of leaving this regression test out.) -#![warn(pointer_structural_match)] - #[derive(Eq)] struct A { a: i64 @@ -34,7 +32,6 @@ fn main() { match s { B(TEST) => println!("matched"), //~^ WARN pointers in patterns behave unpredictably - //~| WARN this was previously accepted by the compiler but is being phased out _ => panic!("didn't match") }; } diff --git a/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.stderr b/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.stderr index d6afc0255ec4c..6daad78e35435 100644 --- a/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.stderr +++ b/tests/ui/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.stderr @@ -1,16 +1,10 @@ warning: function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. - --> $DIR/issue-63479-match-fnptr.rs:35:7 + --> $DIR/issue-63479-match-fnptr.rs:33:7 | LL | B(TEST) => println!("matched"), | ^^^^ | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #62411 -note: the lint level is defined here - --> $DIR/issue-63479-match-fnptr.rs:8:9 - | -LL | #![warn(pointer_structural_match)] - | ^^^^^^^^^^^^^^^^^^^^^^^^ + = note: `#[warn(pointer_structural_match)]` on by default warning: 1 warning emitted From e8f39a4d26cf726d2b89c353d839cf11b5f940c5 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 20 May 2023 17:11:22 +0200 Subject: [PATCH 2/2] Also mention vtables Co-authored-by: Ralf Jung --- compiler/rustc_lint_defs/src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 93d82e8971ecd..acc7b7ac03fc8 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -2282,7 +2282,7 @@ declare_lint! { /// to themselves" or pointers to different functions compare as equal during runtime. /// This is because LLVM optimizations can deduplicate functions if their bodies are the same, /// thus also making pointers to these functions point to the same location. - /// Additionally, functions may get duplicated if they are instantiated + /// Additionally, functions and vtables may get duplicated if they are instantiated /// in different crates and not deduplicated again via LTO. pub POINTER_STRUCTURAL_MATCH, Warn,