Skip to content

Commit af6c7e0

Browse files
committed
also lint against fn ptr and raw ptr nested inside the const
1 parent bec88ad commit af6c7e0

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,15 @@ impl<'tcx> ConstToPat<'tcx> {
239239
}
240240
}
241241
} else if !self.saw_const_match_lint.get() {
242-
match cv.ty().kind() {
243-
ty::RawPtr(..) if have_valtree => {
244-
// This is a good raw pointer, it was accepted by valtree construction.
245-
}
246-
ty::FnPtr(..) | ty::RawPtr(..) => {
247-
self.tcx().emit_spanned_lint(
248-
lint::builtin::POINTER_STRUCTURAL_MATCH,
249-
self.id,
250-
self.span,
251-
PointerPattern,
252-
);
253-
}
254-
_ => {}
242+
if !have_valtree {
243+
// The only way valtree construction can fail without the structural match
244+
// checker finding a violation is if there is a pointer somewhere.
245+
self.tcx().emit_spanned_lint(
246+
lint::builtin::POINTER_STRUCTURAL_MATCH,
247+
self.id,
248+
self.span,
249+
PointerPattern,
250+
);
255251
}
256252
}
257253

tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![deny(pointer_structural_match)]
22
#![allow(dead_code)]
3+
34
const C: *const u8 = &0;
5+
// Make sure we also find pointers nested in other types.
6+
const C_INNER: (*const u8, u8) = (C, 0);
47

58
fn foo(x: *const u8) {
69
match x {
@@ -10,6 +13,14 @@ fn foo(x: *const u8) {
1013
}
1114
}
1215

16+
fn foo2(x: *const u8) {
17+
match (x, 1) {
18+
C_INNER => {} //~ERROR: behave unpredictably
19+
//~| previously accepted
20+
_ => {}
21+
}
22+
}
23+
1324
const D: *const [u8; 4] = b"abcd";
1425

1526
fn main() {

tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: 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.
2-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:7:9
2+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:10:9
33
|
44
LL | C => {}
55
| ^
@@ -13,13 +13,22 @@ LL | #![deny(pointer_structural_match)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: 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.
16-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:17:9
16+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:18:9
17+
|
18+
LL | C_INNER => {}
19+
| ^^^^^^^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
23+
24+
error: 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.
25+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:28:9
1726
|
1827
LL | D => {}
1928
| ^
2029
|
2130
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2231
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
2332

24-
error: aborting due to 2 previous errors
33+
error: aborting due to 3 previous errors
2534

tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,22 @@ fn my_fn(_args: &[A]) {
2626
}
2727

2828
const TEST: Fn = my_fn;
29+
const TEST2: (Fn, u8) = (TEST, 0);
2930

3031
struct B(Fn);
3132

3233
fn main() {
3334
let s = B(my_fn);
3435
match s {
3536
B(TEST) => println!("matched"),
36-
//~^ WARN behave unpredictably
37+
//~^ WARN behave unpredictably
3738
//~| WARN this was previously accepted by the compiler but is being phased out
3839
_ => panic!("didn't match")
3940
};
41+
match (s.0, 0) {
42+
TEST2 => println!("matched"),
43+
//~^ WARN behave unpredictably
44+
//~| WARN this was previously accepted by the compiler but is being phased out
45+
_ => panic!("didn't match")
46+
}
4047
}

tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-63479-match-fnptr.stderr

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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.
2-
--> $DIR/issue-63479-match-fnptr.rs:35:7
2+
--> $DIR/issue-63479-match-fnptr.rs:36:7
33
|
44
LL | B(TEST) => println!("matched"),
55
| ^^^^
@@ -12,5 +12,14 @@ note: the lint level is defined here
1212
LL | #![warn(pointer_structural_match)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
warning: 1 warning emitted
15+
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.
16+
--> $DIR/issue-63479-match-fnptr.rs:42:5
17+
|
18+
LL | TEST2 => println!("matched"),
19+
| ^^^^^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
23+
24+
warning: 2 warnings emitted
1625

0 commit comments

Comments
 (0)