From 9aec5c5bcecad566a76fc7dbb819aa0e58a23671 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 9 Apr 2024 23:17:45 +0000 Subject: [PATCH 1/3] Add test for fn pointer duplication. --- tests/ui/mir/auxiliary/static_fnptr.rs | 10 ++++++++++ tests/ui/mir/static_fnptr.rs | 14 ++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/ui/mir/auxiliary/static_fnptr.rs create mode 100644 tests/ui/mir/static_fnptr.rs diff --git a/tests/ui/mir/auxiliary/static_fnptr.rs b/tests/ui/mir/auxiliary/static_fnptr.rs new file mode 100644 index 0000000000000..a48ee6c012105 --- /dev/null +++ b/tests/ui/mir/auxiliary/static_fnptr.rs @@ -0,0 +1,10 @@ +//@ compile-flags:-O + +fn foo() {} + +pub static ADDR: fn() = foo; + +#[inline(always)] +pub fn bar(x: fn()) -> bool { + x == ADDR +} diff --git a/tests/ui/mir/static_fnptr.rs b/tests/ui/mir/static_fnptr.rs new file mode 100644 index 0000000000000..571dcf4463e12 --- /dev/null +++ b/tests/ui/mir/static_fnptr.rs @@ -0,0 +1,14 @@ +//@ run-pass +//@ compile-flags:-Cno-prepopulate-passes -Copt-level=0 +//@ aux-build:static_fnptr.rs + +extern crate static_fnptr; +use static_fnptr::{ADDR, bar}; + +fn baz() -> bool { + bar(ADDR) +} + +fn main() { + assert!(baz()) +} From d223ba6233455544818e0998d3352de8e237c896 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 10 Apr 2024 10:15:26 +0000 Subject: [PATCH 2/3] Document test. --- tests/ui/mir/static_fnptr.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/ui/mir/static_fnptr.rs b/tests/ui/mir/static_fnptr.rs index 571dcf4463e12..6b1ec021629b8 100644 --- a/tests/ui/mir/static_fnptr.rs +++ b/tests/ui/mir/static_fnptr.rs @@ -1,3 +1,10 @@ +//! Verify that we correctly handle fn pointer provenance in MIR optimizations. +//! By asking to inline `static_fnptr::bar`, we have two copies of `static_fnptr::foo`, one in the +//! auxiliary crate and one in the local crate CGU. +//! `baz` must only consider the versions from upstream crate, and not try to compare with the +//! address of the CGU-local copy. +//! Related issue: #123670 + //@ run-pass //@ compile-flags:-Cno-prepopulate-passes -Copt-level=0 //@ aux-build:static_fnptr.rs From 4c779d75bff7c7c83f4955c3f448198f6dd3c1db Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 4 May 2024 19:27:27 +0000 Subject: [PATCH 3/3] Mark `foo` as explicitly inline. --- tests/ui/mir/auxiliary/static_fnptr.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ui/mir/auxiliary/static_fnptr.rs b/tests/ui/mir/auxiliary/static_fnptr.rs index a48ee6c012105..8c7347175f4b8 100644 --- a/tests/ui/mir/auxiliary/static_fnptr.rs +++ b/tests/ui/mir/auxiliary/static_fnptr.rs @@ -1,5 +1,6 @@ //@ compile-flags:-O +#[inline] fn foo() {} pub static ADDR: fn() = foo;