-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warn-by-default lint against unpredictable fn pointer comparisons
- Loading branch information
Showing
8 changed files
with
513 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//@ check-pass | ||
|
||
fn main() { | ||
let f: fn() = main; | ||
let g: fn() = main; | ||
|
||
let _ = f > g; | ||
//~^ WARN function pointer comparisons | ||
let _ = f >= g; | ||
//~^ WARN function pointer comparisons | ||
let _ = f <= g; | ||
//~^ WARN function pointer comparisons | ||
let _ = f < g; | ||
//~^ WARN function pointer comparisons | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique | ||
--> $DIR/fn-ptr-comparisons-weird.rs:7:13 | ||
| | ||
LL | let _ = f > g; | ||
| ^^^^^ | ||
| | ||
= note: the address of the same function can vary between different codegen units | ||
= note: furthermore, different functions could have the same address after being merged together | ||
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html> | ||
= note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default | ||
|
||
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique | ||
--> $DIR/fn-ptr-comparisons-weird.rs:9:13 | ||
| | ||
LL | let _ = f >= g; | ||
| ^^^^^^ | ||
| | ||
= note: the address of the same function can vary between different codegen units | ||
= note: furthermore, different functions could have the same address after being merged together | ||
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html> | ||
|
||
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique | ||
--> $DIR/fn-ptr-comparisons-weird.rs:11:13 | ||
| | ||
LL | let _ = f <= g; | ||
| ^^^^^^ | ||
| | ||
= note: the address of the same function can vary between different codegen units | ||
= note: furthermore, different functions could have the same address after being merged together | ||
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html> | ||
|
||
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique | ||
--> $DIR/fn-ptr-comparisons-weird.rs:13:13 | ||
| | ||
LL | let _ = f < g; | ||
| ^^^^^ | ||
| | ||
= note: the address of the same function can vary between different codegen units | ||
= note: furthermore, different functions could have the same address after being merged together | ||
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html> | ||
|
||
warning: 4 warnings emitted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//@ check-pass | ||
//@ run-rustfix | ||
|
||
#![feature(ptr_fn_addr_eq)] | ||
|
||
extern "C" { | ||
fn test(); | ||
} | ||
|
||
fn a() {} | ||
|
||
extern "C" fn c() {} | ||
|
||
extern "C" fn args(_a: i32) -> i32 { 0 } | ||
|
||
#[derive(PartialEq, Eq)] | ||
struct A { | ||
f: fn(), | ||
} | ||
|
||
fn main() { | ||
let f: fn() = a; | ||
let g: fn() = f; | ||
|
||
let a1 = A { f }; | ||
let a2 = A { f }; | ||
|
||
let _ = std::ptr::fn_addr_eq(f, a as fn()); | ||
//~^ WARN function pointer comparisons | ||
let _ = !std::ptr::fn_addr_eq(f, a as fn()); | ||
//~^ WARN function pointer comparisons | ||
let _ = std::ptr::fn_addr_eq(f, g); | ||
//~^ WARN function pointer comparisons | ||
let _ = std::ptr::fn_addr_eq(f, f); | ||
//~^ WARN function pointer comparisons | ||
let _ = std::ptr::fn_addr_eq(g, g); | ||
//~^ WARN function pointer comparisons | ||
let _ = std::ptr::fn_addr_eq(g, g); | ||
//~^ WARN function pointer comparisons | ||
let _ = std::ptr::fn_addr_eq(g, g); | ||
//~^ WARN function pointer comparisons | ||
let _ = std::ptr::fn_addr_eq(a as fn(), g); | ||
//~^ WARN function pointer comparisons | ||
|
||
let cfn: extern "C" fn() = c; | ||
let _ = std::ptr::fn_addr_eq(cfn, c as extern "C" fn()); | ||
//~^ WARN function pointer comparisons | ||
|
||
let argsfn: extern "C" fn(i32) -> i32 = args; | ||
let _ = std::ptr::fn_addr_eq(argsfn, args as extern "C" fn(i32) -> i32); | ||
//~^ WARN function pointer comparisons | ||
|
||
let t: unsafe extern "C" fn() = test; | ||
let _ = std::ptr::fn_addr_eq(t, test as unsafe extern "C" fn()); | ||
//~^ WARN function pointer comparisons | ||
|
||
let _ = a1 == a2; // should not warn | ||
let _ = std::ptr::fn_addr_eq(a1.f, a2.f); | ||
//~^ WARN function pointer comparisons | ||
} |
Oops, something went wrong.