Skip to content

Commit 3c1ffb9

Browse files
committed
Report the unpredictable_function_pointer_comparisons lint in macro
1 parent 54dcff1 commit 3c1ffb9

8 files changed

+79
-25
lines changed

compiler/rustc_lint/src/types.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ declare_lint! {
193193
/// same address after being merged together.
194194
UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS,
195195
Warn,
196-
"detects unpredictable function pointer comparisons"
196+
"detects unpredictable function pointer comparisons",
197+
report_in_external_macro
197198
}
198199

199200
#[derive(Copy, Clone, Default)]

tests/ui/lint/fn-ptr-comparisons-some.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ fn main() {
1212
let _ = Some::<FnPtr>(func) == Some(func as unsafe extern "C" fn());
1313
//~^ WARN function pointer comparisons
1414

15-
// Undecided as of https://github.com/rust-lang/rust/pull/134536
1615
assert_eq!(Some::<FnPtr>(func), Some(func as unsafe extern "C" fn()));
16+
//~^ WARN function pointer comparisons
1717
}

tests/ui/lint/fn-ptr-comparisons-some.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,16 @@ LL | let _ = Some::<FnPtr>(func) == Some(func as unsafe extern "C" fn());
99
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
1010
= note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default
1111

12-
warning: 1 warning emitted
12+
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
13+
--> $DIR/fn-ptr-comparisons-some.rs:15:5
14+
|
15+
LL | assert_eq!(Some::<FnPtr>(func), Some(func as unsafe extern "C" fn()));
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
|
18+
= note: the address of the same function can vary between different codegen units
19+
= note: furthermore, different functions could have the same address after being merged together
20+
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
21+
= note: this warning originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
warning: 2 warnings emitted
1324

tests/ui/lint/fn-ptr-comparisons-weird.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
//@ check-pass
22

3+
#[derive(PartialEq, Eq)]
4+
struct A {
5+
f: fn(),
6+
//~^ WARN function pointer comparisons
7+
}
8+
39
fn main() {
410
let f: fn() = main;
511
let g: fn() = main;
@@ -12,4 +18,8 @@ fn main() {
1218
//~^ WARN function pointer comparisons
1319
let _ = f < g;
1420
//~^ WARN function pointer comparisons
21+
let _ = assert_eq!(g, g);
22+
//~^ WARN function pointer comparisons
23+
let _ = assert_ne!(g, g);
24+
//~^ WARN function pointer comparisons
1525
}
+42-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
2-
--> $DIR/fn-ptr-comparisons-weird.rs:7:13
2+
--> $DIR/fn-ptr-comparisons-weird.rs:5:5
3+
|
4+
LL | #[derive(PartialEq, Eq)]
5+
| --------- in this derive macro expansion
6+
LL | struct A {
7+
LL | f: fn(),
8+
| ^^^^^^^
9+
|
10+
= note: the address of the same function can vary between different codegen units
11+
= note: furthermore, different functions could have the same address after being merged together
12+
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
13+
= note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default
14+
= note: this warning originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
16+
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
17+
--> $DIR/fn-ptr-comparisons-weird.rs:13:13
318
|
419
LL | let _ = f > g;
520
| ^^^^^
621
|
722
= note: the address of the same function can vary between different codegen units
823
= note: furthermore, different functions could have the same address after being merged together
924
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
10-
= note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default
1125

1226
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
13-
--> $DIR/fn-ptr-comparisons-weird.rs:9:13
27+
--> $DIR/fn-ptr-comparisons-weird.rs:15:13
1428
|
1529
LL | let _ = f >= g;
1630
| ^^^^^^
@@ -20,7 +34,7 @@ LL | let _ = f >= g;
2034
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
2135

2236
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
23-
--> $DIR/fn-ptr-comparisons-weird.rs:11:13
37+
--> $DIR/fn-ptr-comparisons-weird.rs:17:13
2438
|
2539
LL | let _ = f <= g;
2640
| ^^^^^^
@@ -30,7 +44,7 @@ LL | let _ = f <= g;
3044
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
3145

3246
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
33-
--> $DIR/fn-ptr-comparisons-weird.rs:13:13
47+
--> $DIR/fn-ptr-comparisons-weird.rs:19:13
3448
|
3549
LL | let _ = f < g;
3650
| ^^^^^
@@ -39,5 +53,27 @@ LL | let _ = f < g;
3953
= note: furthermore, different functions could have the same address after being merged together
4054
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
4155

42-
warning: 4 warnings emitted
56+
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
57+
--> $DIR/fn-ptr-comparisons-weird.rs:21:13
58+
|
59+
LL | let _ = assert_eq!(g, g);
60+
| ^^^^^^^^^^^^^^^^
61+
|
62+
= note: the address of the same function can vary between different codegen units
63+
= note: furthermore, different functions could have the same address after being merged together
64+
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
65+
= note: this warning originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
66+
67+
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
68+
--> $DIR/fn-ptr-comparisons-weird.rs:23:13
69+
|
70+
LL | let _ = assert_ne!(g, g);
71+
| ^^^^^^^^^^^^^^^^
72+
|
73+
= note: the address of the same function can vary between different codegen units
74+
= note: furthermore, different functions could have the same address after being merged together
75+
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
76+
= note: this warning originates in the macro `assert_ne` (in Nightly builds, run with -Z macro-backtrace for more info)
77+
78+
warning: 7 warnings emitted
4379

tests/ui/lint/fn-ptr-comparisons.fixed

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ extern "C" fn c() {}
1111

1212
extern "C" fn args(_a: i32) -> i32 { 0 }
1313

14-
#[derive(PartialEq, Eq)]
1514
struct A {
1615
f: fn(),
1716
}
@@ -52,7 +51,6 @@ fn main() {
5251
let _ = std::ptr::fn_addr_eq(t, test as unsafe extern "C" fn());
5352
//~^ WARN function pointer comparisons
5453

55-
let _ = a1 == a2; // should not warn
5654
let _ = std::ptr::fn_addr_eq(a1.f, a2.f);
5755
//~^ WARN function pointer comparisons
5856
}

tests/ui/lint/fn-ptr-comparisons.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ extern "C" fn c() {}
1111

1212
extern "C" fn args(_a: i32) -> i32 { 0 }
1313

14-
#[derive(PartialEq, Eq)]
1514
struct A {
1615
f: fn(),
1716
}
@@ -52,7 +51,6 @@ fn main() {
5251
let _ = t == test;
5352
//~^ WARN function pointer comparisons
5453

55-
let _ = a1 == a2; // should not warn
5654
let _ = a1.f == a2.f;
5755
//~^ WARN function pointer comparisons
5856
}

tests/ui/lint/fn-ptr-comparisons.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
2-
--> $DIR/fn-ptr-comparisons.rs:26:13
2+
--> $DIR/fn-ptr-comparisons.rs:25:13
33
|
44
LL | let _ = f == a;
55
| ^^^^^^
@@ -14,7 +14,7 @@ LL | let _ = std::ptr::fn_addr_eq(f, a as fn());
1414
| +++++++++++++++++++++ ~ ++++++++
1515

1616
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
17-
--> $DIR/fn-ptr-comparisons.rs:28:13
17+
--> $DIR/fn-ptr-comparisons.rs:27:13
1818
|
1919
LL | let _ = f != a;
2020
| ^^^^^^
@@ -28,7 +28,7 @@ LL | let _ = !std::ptr::fn_addr_eq(f, a as fn());
2828
| ++++++++++++++++++++++ ~ ++++++++
2929

3030
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
31-
--> $DIR/fn-ptr-comparisons.rs:30:13
31+
--> $DIR/fn-ptr-comparisons.rs:29:13
3232
|
3333
LL | let _ = f == g;
3434
| ^^^^^^
@@ -42,7 +42,7 @@ LL | let _ = std::ptr::fn_addr_eq(f, g);
4242
| +++++++++++++++++++++ ~ +
4343

4444
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
45-
--> $DIR/fn-ptr-comparisons.rs:32:13
45+
--> $DIR/fn-ptr-comparisons.rs:31:13
4646
|
4747
LL | let _ = f == f;
4848
| ^^^^^^
@@ -56,7 +56,7 @@ LL | let _ = std::ptr::fn_addr_eq(f, f);
5656
| +++++++++++++++++++++ ~ +
5757

5858
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
59-
--> $DIR/fn-ptr-comparisons.rs:34:13
59+
--> $DIR/fn-ptr-comparisons.rs:33:13
6060
|
6161
LL | let _ = g == g;
6262
| ^^^^^^
@@ -70,7 +70,7 @@ LL | let _ = std::ptr::fn_addr_eq(g, g);
7070
| +++++++++++++++++++++ ~ +
7171

7272
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
73-
--> $DIR/fn-ptr-comparisons.rs:36:13
73+
--> $DIR/fn-ptr-comparisons.rs:35:13
7474
|
7575
LL | let _ = g == g;
7676
| ^^^^^^
@@ -84,7 +84,7 @@ LL | let _ = std::ptr::fn_addr_eq(g, g);
8484
| +++++++++++++++++++++ ~ +
8585

8686
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
87-
--> $DIR/fn-ptr-comparisons.rs:38:13
87+
--> $DIR/fn-ptr-comparisons.rs:37:13
8888
|
8989
LL | let _ = &g == &g;
9090
| ^^^^^^^^
@@ -98,7 +98,7 @@ LL | let _ = std::ptr::fn_addr_eq(g, g);
9898
| ~~~~~~~~~~~~~~~~~~~~~ ~ +
9999

100100
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
101-
--> $DIR/fn-ptr-comparisons.rs:40:13
101+
--> $DIR/fn-ptr-comparisons.rs:39:13
102102
|
103103
LL | let _ = a as fn() == g;
104104
| ^^^^^^^^^^^^^^
@@ -112,7 +112,7 @@ LL | let _ = std::ptr::fn_addr_eq(a as fn(), g);
112112
| +++++++++++++++++++++ ~ +
113113

114114
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
115-
--> $DIR/fn-ptr-comparisons.rs:44:13
115+
--> $DIR/fn-ptr-comparisons.rs:43:13
116116
|
117117
LL | let _ = cfn == c;
118118
| ^^^^^^^^
@@ -126,7 +126,7 @@ LL | let _ = std::ptr::fn_addr_eq(cfn, c as extern "C" fn());
126126
| +++++++++++++++++++++ ~ +++++++++++++++++++
127127

128128
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
129-
--> $DIR/fn-ptr-comparisons.rs:48:13
129+
--> $DIR/fn-ptr-comparisons.rs:47:13
130130
|
131131
LL | let _ = argsfn == args;
132132
| ^^^^^^^^^^^^^^
@@ -140,7 +140,7 @@ LL | let _ = std::ptr::fn_addr_eq(argsfn, args as extern "C" fn(i32) -> i32)
140140
| +++++++++++++++++++++ ~ +++++++++++++++++++++++++++++
141141

142142
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
143-
--> $DIR/fn-ptr-comparisons.rs:52:13
143+
--> $DIR/fn-ptr-comparisons.rs:51:13
144144
|
145145
LL | let _ = t == test;
146146
| ^^^^^^^^^
@@ -154,7 +154,7 @@ LL | let _ = std::ptr::fn_addr_eq(t, test as unsafe extern "C" fn());
154154
| +++++++++++++++++++++ ~ ++++++++++++++++++++++++++
155155

156156
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
157-
--> $DIR/fn-ptr-comparisons.rs:56:13
157+
--> $DIR/fn-ptr-comparisons.rs:54:13
158158
|
159159
LL | let _ = a1.f == a2.f;
160160
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)