Skip to content

Commit 604c8a7

Browse files
committed
Report the unpredictable_function_pointer_comparisons lint in macro
1 parent 462cc09 commit 604c8a7

8 files changed

+90
-25
lines changed

compiler/rustc_lint/src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ declare_lint! {
197197
/// same address after being merged together.
198198
UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS,
199199
Warn,
200-
"detects unpredictable function pointer comparisons"
200+
"detects unpredictable function pointer comparisons",
201+
report_in_external_macro
201202
}
202203

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

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 1 deletion
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
//@ check-pass
22

3+
#[derive(PartialEq, Eq)]
4+
struct A {
5+
f: fn(),
6+
//~^ WARN function pointer comparisons
7+
}
8+
9+
#[allow(unpredictable_function_pointer_comparisons)]
10+
#[derive(PartialEq, Eq)]
11+
struct AllowedAbove {
12+
f: fn(),
13+
}
14+
15+
#[derive(PartialEq, Eq)]
16+
#[allow(unpredictable_function_pointer_comparisons)]
17+
struct AllowedBelow {
18+
f: fn(),
19+
}
20+
321
fn main() {
422
let f: fn() = main;
523
let g: fn() = main;
@@ -12,4 +30,8 @@ fn main() {
1230
//~^ WARN function pointer comparisons
1331
let _ = f < g;
1432
//~^ WARN function pointer comparisons
33+
let _ = assert_eq!(g, g);
34+
//~^ WARN function pointer comparisons
35+
let _ = assert_ne!(g, g);
36+
//~^ WARN function pointer comparisons
1537
}
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
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+
15+
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
16+
--> $DIR/fn-ptr-comparisons-weird.rs:25:13
317
|
418
LL | let _ = f > g;
519
| ^^^^^
620
|
721
= note: the address of the same function can vary between different codegen units
822
= note: furthermore, different functions could have the same address after being merged together
923
= 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
1124

1225
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
26+
--> $DIR/fn-ptr-comparisons-weird.rs:27:13
1427
|
1528
LL | let _ = f >= g;
1629
| ^^^^^^
@@ -20,7 +33,7 @@ LL | let _ = f >= g;
2033
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
2134

2235
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
36+
--> $DIR/fn-ptr-comparisons-weird.rs:29:13
2437
|
2538
LL | let _ = f <= g;
2639
| ^^^^^^
@@ -30,7 +43,7 @@ LL | let _ = f <= g;
3043
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
3144

3245
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
46+
--> $DIR/fn-ptr-comparisons-weird.rs:31:13
3447
|
3548
LL | let _ = f < g;
3649
| ^^^^^
@@ -39,5 +52,27 @@ LL | let _ = f < g;
3952
= note: furthermore, different functions could have the same address after being merged together
4053
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
4154

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

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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 12 additions & 12 deletions
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
| ^^^^^^
@@ -15,7 +15,7 @@ LL + let _ = std::ptr::fn_addr_eq(f, a as fn());
1515
|
1616

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

3232
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
33-
--> $DIR/fn-ptr-comparisons.rs:30:13
33+
--> $DIR/fn-ptr-comparisons.rs:29:13
3434
|
3535
LL | let _ = f == g;
3636
| ^^^^^^
@@ -45,7 +45,7 @@ LL + let _ = std::ptr::fn_addr_eq(f, g);
4545
|
4646

4747
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
48-
--> $DIR/fn-ptr-comparisons.rs:32:13
48+
--> $DIR/fn-ptr-comparisons.rs:31:13
4949
|
5050
LL | let _ = f == f;
5151
| ^^^^^^
@@ -60,7 +60,7 @@ LL + let _ = std::ptr::fn_addr_eq(f, f);
6060
|
6161

6262
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
63-
--> $DIR/fn-ptr-comparisons.rs:34:13
63+
--> $DIR/fn-ptr-comparisons.rs:33:13
6464
|
6565
LL | let _ = g == g;
6666
| ^^^^^^
@@ -75,7 +75,7 @@ LL + let _ = std::ptr::fn_addr_eq(g, g);
7575
|
7676

7777
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
78-
--> $DIR/fn-ptr-comparisons.rs:36:13
78+
--> $DIR/fn-ptr-comparisons.rs:35:13
7979
|
8080
LL | let _ = g == g;
8181
| ^^^^^^
@@ -90,7 +90,7 @@ LL + let _ = std::ptr::fn_addr_eq(g, g);
9090
|
9191

9292
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
93-
--> $DIR/fn-ptr-comparisons.rs:38:13
93+
--> $DIR/fn-ptr-comparisons.rs:37:13
9494
|
9595
LL | let _ = &g == &g;
9696
| ^^^^^^^^
@@ -105,7 +105,7 @@ LL + let _ = std::ptr::fn_addr_eq(g, g);
105105
|
106106

107107
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
108-
--> $DIR/fn-ptr-comparisons.rs:40:13
108+
--> $DIR/fn-ptr-comparisons.rs:39:13
109109
|
110110
LL | let _ = a as fn() == g;
111111
| ^^^^^^^^^^^^^^
@@ -120,7 +120,7 @@ LL + let _ = std::ptr::fn_addr_eq(a as fn(), g);
120120
|
121121

122122
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
123-
--> $DIR/fn-ptr-comparisons.rs:44:13
123+
--> $DIR/fn-ptr-comparisons.rs:43:13
124124
|
125125
LL | let _ = cfn == c;
126126
| ^^^^^^^^
@@ -135,7 +135,7 @@ LL + let _ = std::ptr::fn_addr_eq(cfn, c as extern "C" fn());
135135
|
136136

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

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

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

0 commit comments

Comments
 (0)