Skip to content

Commit cbd682b

Browse files
committed
turn pointer_structural_match into a hard error
1 parent 179a6a0 commit cbd682b

23 files changed

+131
-811
lines changed

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,11 @@ fn register_builtins(store: &mut LintStore) {
544544
"converted into hard error, see RFC #3535 \
545545
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
546546
);
547+
store.register_removed(
548+
"pointer_structural_match",
549+
"converted into hard error, see RFC #3535 \
550+
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
551+
);
547552
}
548553

549554
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

-40
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ declare_lint_pass! {
7474
ORDER_DEPENDENT_TRAIT_OBJECTS,
7575
OVERLAPPING_RANGE_ENDPOINTS,
7676
PATTERNS_IN_FNS_WITHOUT_BODY,
77-
POINTER_STRUCTURAL_MATCH,
7877
PRIVATE_BOUNDS,
7978
PRIVATE_INTERFACES,
8079
PROC_MACRO_BACK_COMPAT,
@@ -2371,45 +2370,6 @@ declare_lint! {
23712370
report_in_external_macro
23722371
}
23732372

2374-
declare_lint! {
2375-
/// The `pointer_structural_match` lint detects pointers used in patterns whose behaviour
2376-
/// cannot be relied upon across compiler versions and optimization levels.
2377-
///
2378-
/// ### Example
2379-
///
2380-
/// ```rust,compile_fail
2381-
/// #![deny(pointer_structural_match)]
2382-
/// fn foo(a: usize, b: usize) -> usize { a + b }
2383-
/// const FOO: fn(usize, usize) -> usize = foo;
2384-
/// fn main() {
2385-
/// match FOO {
2386-
/// FOO => {},
2387-
/// _ => {},
2388-
/// }
2389-
/// }
2390-
/// ```
2391-
///
2392-
/// {{produces}}
2393-
///
2394-
/// ### Explanation
2395-
///
2396-
/// Previous versions of Rust allowed function pointers and all raw pointers in patterns.
2397-
/// While these work in many cases as expected by users, it is possible that due to
2398-
/// optimizations pointers are "not equal to themselves" or pointers to different functions
2399-
/// compare as equal during runtime. This is because LLVM optimizations can deduplicate
2400-
/// functions if their bodies are the same, thus also making pointers to these functions point
2401-
/// to the same location. Additionally functions may get duplicated if they are instantiated
2402-
/// in different crates and not deduplicated again via LTO. Pointer identity for memory
2403-
/// created by `const` is similarly unreliable.
2404-
pub POINTER_STRUCTURAL_MATCH,
2405-
Warn,
2406-
"pointers are not structural-match",
2407-
@future_incompatible = FutureIncompatibleInfo {
2408-
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2409-
reference: "issue #120362 <https://github.com/rust-lang/rust/issues/120362>",
2410-
};
2411-
}
2412-
24132373
declare_lint! {
24142374
/// The `ambiguous_associated_items` lint detects ambiguity between
24152375
/// [associated items] and [enum variants].

compiler/rustc_mir_build/src/errors.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,12 @@ pub struct NaNPattern {
788788
pub span: Span,
789789
}
790790

791-
#[derive(LintDiagnostic)]
791+
#[derive(Diagnostic)]
792792
#[diag(mir_build_pointer_pattern)]
793-
pub struct PointerPattern;
793+
pub struct PointerPattern {
794+
#[primary_span]
795+
pub span: Span,
796+
}
794797

795798
#[derive(Diagnostic)]
796799
#[diag(mir_build_non_empty_never_pattern)]

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_infer::traits::Obligation;
66
use rustc_middle::mir;
77
use rustc_middle::thir::{FieldPat, Pat, PatKind};
88
use rustc_middle::ty::{self, Ty, TyCtxt, ValTree};
9-
use rustc_session::lint;
109
use rustc_span::{ErrorGuaranteed, Span};
1110
use rustc_target::abi::{FieldIdx, VariantIdx};
1211
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
@@ -189,12 +188,9 @@ impl<'tcx> ConstToPat<'tcx> {
189188
} else if !have_valtree {
190189
// The only way valtree construction can fail without the structural match
191190
// checker finding a violation is if there is a pointer somewhere.
192-
self.tcx().emit_node_span_lint(
193-
lint::builtin::POINTER_STRUCTURAL_MATCH,
194-
self.id,
195-
self.span,
196-
PointerPattern,
197-
);
191+
let e = self.tcx().dcx().emit_err(PointerPattern { span: self.span });
192+
let kind = PatKind::Error(e);
193+
return Box::new(Pat { span: self.span, ty: cv.ty(), kind });
198194
}
199195

200196
// Always check for `PartialEq` if we had no other errors yet.

tests/ui/closures/2229_closure_analysis/match/match-edge-cases_1.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
//@ edition:2021
33

44
const PATTERN_REF: &str = "Hello World";
5-
const NUMBER: i32 = 30;
6-
const NUMBER_POINTER: *const i32 = &NUMBER;
5+
const NUMBER_POINTER: *const i32 = 30 as *const i32;
76

87
pub fn edge_case_ref(event: &str) {
98
let _ = || {
@@ -26,8 +25,7 @@ pub fn edge_case_str(event: String) {
2625
pub fn edge_case_raw_ptr(event: *const i32) {
2726
let _ = || {
2827
match event {
29-
NUMBER_POINTER => (), //~WARN behave unpredictably
30-
//~| previously accepted
28+
NUMBER_POINTER => (),
3129
_ => (),
3230
};
3331
};

tests/ui/closures/2229_closure_analysis/match/match-edge-cases_1.stderr

-23
This file was deleted.

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

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![deny(pointer_structural_match)]
21
#![allow(dead_code)]
32

43
const C: *const u8 = &0;
@@ -8,15 +7,13 @@ const C_INNER: (*const u8, u8) = (C, 0);
87
fn foo(x: *const u8) {
98
match x {
109
C => {} //~ERROR: behave unpredictably
11-
//~| previously accepted
1210
_ => {}
1311
}
1412
}
1513

1614
fn foo2(x: *const u8) {
1715
match (x, 1) {
1816
C_INNER => {} //~ERROR: behave unpredictably
19-
//~| previously accepted
2017
_ => {}
2118
}
2219
}
@@ -28,13 +25,11 @@ const STR: *const str = "abcd";
2825
fn main() {
2926
match D {
3027
D => {} //~ERROR: behave unpredictably
31-
//~| previously accepted
3228
_ => {}
3329
}
3430

3531
match STR {
3632
STR => {} //~ERROR: behave unpredictably
37-
//~| previously accepted
3833
_ => {}
3934
}
4035
}
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,26 @@
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:10:9
2+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:9:9
33
|
44
LL | C => {}
55
| ^
6-
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
9-
note: the lint level is defined here
10-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:1:9
11-
|
12-
LL | #![deny(pointer_structural_match)]
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^
146

157
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:18:9
8+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:16:9
179
|
1810
LL | C_INNER => {}
1911
| ^^^^^^^
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 #120362 <https://github.com/rust-lang/rust/issues/120362>
2312

2413
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:30:9
14+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:27:9
2615
|
2716
LL | D => {}
2817
| ^
29-
|
30-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
31-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
3218

3319
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.
34-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:36:9
20+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:32:9
3521
|
3622
LL | STR => {}
3723
| ^^^
38-
|
39-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
40-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
4124

4225
error: aborting due to 4 previous errors
4326

44-
Future incompatibility report: Future breakage diagnostic:
45-
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.
46-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:10:9
47-
|
48-
LL | C => {}
49-
| ^
50-
|
51-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
52-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
53-
note: the lint level is defined here
54-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:1:9
55-
|
56-
LL | #![deny(pointer_structural_match)]
57-
| ^^^^^^^^^^^^^^^^^^^^^^^^
58-
59-
Future breakage diagnostic:
60-
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.
61-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:18:9
62-
|
63-
LL | C_INNER => {}
64-
| ^^^^^^^
65-
|
66-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
67-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
68-
note: the lint level is defined here
69-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:1:9
70-
|
71-
LL | #![deny(pointer_structural_match)]
72-
| ^^^^^^^^^^^^^^^^^^^^^^^^
73-
74-
Future breakage diagnostic:
75-
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.
76-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:30:9
77-
|
78-
LL | D => {}
79-
| ^
80-
|
81-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
82-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
83-
note: the lint level is defined here
84-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:1:9
85-
|
86-
LL | #![deny(pointer_structural_match)]
87-
| ^^^^^^^^^^^^^^^^^^^^^^^^
88-
89-
Future breakage diagnostic:
90-
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.
91-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:36:9
92-
|
93-
LL | STR => {}
94-
| ^^^
95-
|
96-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
97-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
98-
note: the lint level is defined here
99-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:1:9
100-
|
101-
LL | #![deny(pointer_structural_match)]
102-
| ^^^^^^^^^^^^^^^^^^^^^^^^
103-
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//@ run-pass
2-
3-
#![warn(pointer_structural_match)]
4-
51
type Func = fn(usize, usize) -> usize;
62

73
fn foo(a: usize, b: usize) -> usize { a + b }
@@ -16,10 +12,8 @@ const BAR: Func = bar;
1612

1713
fn main() {
1814
match test(std::env::consts::ARCH.len()) {
19-
FOO => println!("foo"), //~ WARN behave unpredictably
20-
//~^ WARN will become a hard error
21-
BAR => println!("bar"), //~ WARN behave unpredictably
22-
//~^ WARN will become a hard error
15+
FOO => println!("foo"), //~ ERROR behave unpredictably
16+
BAR => println!("bar"), //~ ERROR behave unpredictably
2317
_ => unreachable!(),
2418
}
2519
}
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,14 @@
1-
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-44333.rs:19:9
1+
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-44333.rs:15:9
33
|
44
LL | FOO => println!("foo"),
55
| ^^^
6-
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
9-
note: the lint level is defined here
10-
--> $DIR/issue-44333.rs:3:9
11-
|
12-
LL | #![warn(pointer_structural_match)]
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^
146

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-44333.rs:21:9
7+
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.
8+
--> $DIR/issue-44333.rs:16:9
179
|
1810
LL | BAR => println!("bar"),
1911
| ^^^
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 #120362 <https://github.com/rust-lang/rust/issues/120362>
23-
24-
warning: 2 warnings emitted
2512

26-
Future incompatibility report: Future breakage diagnostic:
27-
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.
28-
--> $DIR/issue-44333.rs:19:9
29-
|
30-
LL | FOO => println!("foo"),
31-
| ^^^
32-
|
33-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
34-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
35-
note: the lint level is defined here
36-
--> $DIR/issue-44333.rs:3:9
37-
|
38-
LL | #![warn(pointer_structural_match)]
39-
| ^^^^^^^^^^^^^^^^^^^^^^^^
40-
41-
Future breakage diagnostic:
42-
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.
43-
--> $DIR/issue-44333.rs:21:9
44-
|
45-
LL | BAR => println!("bar"),
46-
| ^^^
47-
|
48-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
49-
= note: for more information, see issue #120362 <https://github.com/rust-lang/rust/issues/120362>
50-
note: the lint level is defined here
51-
--> $DIR/issue-44333.rs:3:9
52-
|
53-
LL | #![warn(pointer_structural_match)]
54-
| ^^^^^^^^^^^^^^^^^^^^^^^^
13+
error: aborting due to 2 previous errors
5514

0 commit comments

Comments
 (0)