Skip to content

Commit

Permalink
Turn pointer_structural_match forward compat lint into normal lint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
est31 committed May 9, 2023
1 parent 4b94c23 commit 86b8a21
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 44 deletions.
19 changes: 8 additions & 11 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/rust-lang/rust/issues/70861>",
};
Warn,
"pointers are not structural-match"
}

declare_lint! {
Expand Down
1 change: 1 addition & 0 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub trait StructuralPartialEq {
///
/// const CFN: Wrap<fn(&())> = Wrap(higher_order);
///
/// # #[allow(pointer_structural_match)]
/// fn main() {
/// match CFN {
/// CFN => {}
Expand Down
4 changes: 0 additions & 4 deletions tests/ui/consts/const_in_pattern/issue-44333.rs
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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!(),
}
}
15 changes: 3 additions & 12 deletions tests/ui/consts/const_in_pattern/issue-44333.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/70861>
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 <https://github.com/rust-lang/rust/issues/70861>

warning: 2 warnings emitted

2 changes: 1 addition & 1 deletion tests/ui/consts/issue-34784.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// run-pass

#![warn(pointer_structural_match)]

#![allow(dead_code)]
const C: *const u8 = &0;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/pattern/usefulness/consts-opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// run-pass

#![warn(pointer_structural_match)]
#![deny(pointer_structural_match)]

struct NoDerive(#[allow(unused_tuple_struct_fields)] i32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// run-pass

#![warn(pointer_structural_match)]
#![deny(pointer_structural_match)]

struct NoDerive(#[allow(unused_tuple_struct_fields)] i32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// run-pass

#![warn(pointer_structural_match)]
#![deny(pointer_structural_match)]

struct NoDerive(#[allow(unused_tuple_struct_fields)] i32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// run-pass

#![warn(pointer_structural_match)]
#![deny(pointer_structural_match)]

struct NoDerive(#[allow(unused_tuple_struct_fields)] i32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
};
}
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/70861>
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

0 comments on commit 86b8a21

Please sign in to comment.