Skip to content

Commit

Permalink
make pointer_structural_match warn-by-default
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 28, 2023
1 parent af6c7e0 commit 70a8e15
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 26 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,7 @@ declare_lint! {
/// in different crates and not deduplicated again via LTO. Pointer identity for memory
/// created by `const` is similarly unreliable.
pub POINTER_STRUCTURAL_MATCH,
Allow,
Warn,
"pointers are not structural-match",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
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 @@ -247,6 +247,7 @@ marker_impls! {
///
/// const CFN: Wrap<fn(&())> = Wrap(higher_order);
///
/// #[allow(pointer_structural_match)]
/// fn main() {
/// match CFN {
/// CFN => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub fn edge_case_str(event: String) {
pub fn edge_case_raw_ptr(event: *const i32) {
let _ = || {
match event {
NUMBER_POINTER => (),
NUMBER_POINTER => (), //~WARN behave unpredictably
//~| previously accepted
_ => (),
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
warning: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/match-edge-cases_1.rs:29:13
|
LL | NUMBER_POINTER => (),
| ^^^^^^^^^^^^^^
|
= 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: `#[warn(pointer_structural_match)]` on by default

warning: 1 warning emitted

24 changes: 16 additions & 8 deletions tests/ui/pattern/usefulness/consts-opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ fn main() {
const QUUX: Quux = quux;

match QUUX {
QUUX => {}
QUUX => {}
QUUX => {} //~WARN behave unpredictably
//~| previously accepted
QUUX => {} //~WARN behave unpredictably
//~| previously accepted
_ => {}
}

Expand All @@ -105,14 +107,17 @@ fn main() {
const WRAPQUUX: Wrap<Quux> = Wrap(quux);

match WRAPQUUX {
WRAPQUUX => {}
WRAPQUUX => {}
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
}

match WRAPQUUX {
Wrap(_) => {}
WRAPQUUX => {}
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
}

match WRAPQUUX {
Expand All @@ -121,7 +126,8 @@ fn main() {

match WRAPQUUX {
//~^ ERROR: non-exhaustive patterns: `Wrap(_)` not covered
WRAPQUUX => {}
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
}

#[derive(PartialEq, Eq)]
Expand All @@ -132,9 +138,11 @@ fn main() {
const WHOKNOWSQUUX: WhoKnows<Quux> = WhoKnows::Yay(quux);

match WHOKNOWSQUUX {
WHOKNOWSQUUX => {}
WHOKNOWSQUUX => {} //~WARN behave unpredictably
//~| previously accepted
WhoKnows::Yay(_) => {}
WHOKNOWSQUUX => {}
WHOKNOWSQUUX => {} //~WARN behave unpredictably
//~| previously accepted
WhoKnows::Nope => {}
}
}
84 changes: 78 additions & 6 deletions tests/ui/pattern/usefulness/consts-opaque.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,96 @@ LL | BAZ => {}
= note: the traits must be derived, manual `impl`s are not sufficient
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details

warning: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:98:9
|
LL | QUUX => {}
| ^^^^
|
= 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: `#[warn(pointer_structural_match)]` on by default

warning: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:100:9
|
LL | QUUX => {}
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:110:9
|
LL | WRAPQUUX => {}
| ^^^^^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:112:9
|
LL | WRAPQUUX => {}
| ^^^^^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:119:9
|
LL | WRAPQUUX => {}
| ^^^^^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:129:9
|
LL | WRAPQUUX => {}
| ^^^^^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:141:9
|
LL | WHOKNOWSQUUX => {}
| ^^^^^^^^^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/consts-opaque.rs:144:9
|
LL | WHOKNOWSQUUX => {}
| ^^^^^^^^^^^^
|
= 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>

error[E0004]: non-exhaustive patterns: `Wrap(_)` not covered
--> $DIR/consts-opaque.rs:122:11
--> $DIR/consts-opaque.rs:127:11
|
LL | match WRAPQUUX {
| ^^^^^^^^ pattern `Wrap(_)` not covered
|
note: `Wrap<fn(usize, usize) -> usize>` defined here
--> $DIR/consts-opaque.rs:104:12
--> $DIR/consts-opaque.rs:106:12
|
LL | struct Wrap<T>(T);
| ^^^^
= note: the matched value is of type `Wrap<fn(usize, usize) -> usize>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ WRAPQUUX => {},
LL + Wrap(_) => todo!()
|
LL | WRAPQUUX => {}, Wrap(_) => todo!()
| ++++++++++++++++++++

error: aborting due to 10 previous errors; 1 warning emitted
error: aborting due to 10 previous errors; 9 warnings emitted

For more information about this error, try `rustc --explain E0004`.
Original file line number Diff line number Diff line change
Expand Up @@ -40,71 +40,80 @@ fn main() {
const CFN1: Wrap<fn()> = Wrap(trivial);
let input: Wrap<fn()> = Wrap(trivial);
match Wrap(input) {
Wrap(CFN1) => count += 1,
Wrap(CFN1) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn(T) is structural-match when T is too.
const CFN2: Wrap<fn(SM)> = Wrap(sm_to);
let input: Wrap<fn(SM)> = Wrap(sm_to);
match Wrap(input) {
Wrap(CFN2) => count += 1,
Wrap(CFN2) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn() -> T is structural-match when T is too.
const CFN3: Wrap<fn() -> SM> = Wrap(to_sm);
let input: Wrap<fn() -> SM> = Wrap(to_sm);
match Wrap(input) {
Wrap(CFN3) => count += 1,
Wrap(CFN3) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn(T) is structural-match even if T is not.
const CFN4: Wrap<fn(NotSM)> = Wrap(not_sm_to);
let input: Wrap<fn(NotSM)> = Wrap(not_sm_to);
match Wrap(input) {
Wrap(CFN4) => count += 1,
Wrap(CFN4) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn() -> T is structural-match even if T is not.
const CFN5: Wrap<fn() -> NotSM> = Wrap(to_not_sm);
let input: Wrap<fn() -> NotSM> = Wrap(to_not_sm);
match Wrap(input) {
Wrap(CFN5) => count += 1,
Wrap(CFN5) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn(&T) is structural-match when T is too.
const CFN6: Wrap<fn(&SM)> = Wrap(r_sm_to);
let input: Wrap<fn(&SM)> = Wrap(r_sm_to);
match Wrap(input) {
Wrap(CFN6) => count += 1,
Wrap(CFN6) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn() -> &T is structural-match when T is too.
const CFN7: Wrap<fn(&()) -> &SM> = Wrap(r_to_r_sm);
let input: Wrap<fn(&()) -> &SM> = Wrap(r_to_r_sm);
match Wrap(input) {
Wrap(CFN7) => count += 1,
Wrap(CFN7) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn(T) is structural-match even if T is not.
const CFN8: Wrap<fn(&NotSM)> = Wrap(r_not_sm_to);
let input: Wrap<fn(&NotSM)> = Wrap(r_not_sm_to);
match Wrap(input) {
Wrap(CFN8) => count += 1,
Wrap(CFN8) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

// Check that fn() -> T is structural-match even if T is not.
const CFN9: Wrap<fn(&()) -> &NotSM> = Wrap(r_to_r_not_sm);
let input: Wrap<fn(&()) -> &NotSM> = Wrap(r_to_r_not_sm);
match Wrap(input) {
Wrap(CFN9) => count += 1,
Wrap(CFN9) => count += 1, //~WARN behave unpredictably
//~| previously accepted
Wrap(_) => {}
};

Expand All @@ -126,7 +135,8 @@ fn main() {

let input = Foo { alpha: not_sm_to, beta: to_not_sm, gamma: sm_to, delta: to_sm };
match input {
CFOO => count += 1,
CFOO => count += 1, //~WARN behave unpredictably
//~| previously accepted
Foo { .. } => {}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
warning: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:43:14
|
LL | Wrap(CFN1) => count += 1,
| ^^^^
|
= 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: `#[warn(pointer_structural_match)]` on by default

warning: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:52:14
|
LL | Wrap(CFN2) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:61:14
|
LL | Wrap(CFN3) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:70:14
|
LL | Wrap(CFN4) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:79:14
|
LL | Wrap(CFN5) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:88:14
|
LL | Wrap(CFN6) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:97:14
|
LL | Wrap(CFN7) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:106:14
|
LL | Wrap(CFN8) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:115:14
|
LL | Wrap(CFN9) => count += 1,
| ^^^^
|
= 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: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
--> $DIR/fn-ptr-is-structurally-matchable.rs:138:9
|
LL | CFOO => count += 1,
| ^^^^
|
= 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: 10 warnings emitted

0 comments on commit 70a8e15

Please sign in to comment.