Skip to content

Make pointer_structural_match normal and warn #110166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Use of function pointers and wide raw pointers in patterns works in many cases as
/// Use of function pointers and `dyn Trait` raw pointers in patterns works in many cases as

I assume this doesn't fire on slice raw pointers?

/// 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 and vtables 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