about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-06-28 22:04:15 +0200
committerGitHub <noreply@github.com>2024-06-28 22:04:15 +0200
commitc96c1bdd875413650cff8fcaabe6d1615a5c5c17 (patch)
tree2cd8e5978bda574fbd0817fb69ff1f39ed8f2550
parentc4c0897a262d42f26f89aaca16ed52457f6a4f05 (diff)
parent4c779d75bff7c7c83f4955c3f448198f6dd3c1db (diff)
downloadrust-c96c1bdd875413650cff8fcaabe6d1615a5c5c17.tar.gz
rust-c96c1bdd875413650cff8fcaabe6d1615a5c5c17.zip
Rollup merge of #123714 - cjgillot:static-fnptr, r=wesleywiser
Add test for fn pointer duplication.

I managed to make it fail when removing provenance checks in GVN.

cc https://github.com/rust-lang/rust/issues/123670

r? ``````@oli-obk``````
-rw-r--r--tests/ui/mir/auxiliary/static_fnptr.rs11
-rw-r--r--tests/ui/mir/static_fnptr.rs21
2 files changed, 32 insertions, 0 deletions
diff --git a/tests/ui/mir/auxiliary/static_fnptr.rs b/tests/ui/mir/auxiliary/static_fnptr.rs
new file mode 100644
index 00000000000..8c7347175f4
--- /dev/null
+++ b/tests/ui/mir/auxiliary/static_fnptr.rs
@@ -0,0 +1,11 @@
+//@ compile-flags:-O
+
+#[inline]
+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 00000000000..6b1ec021629
--- /dev/null
+++ b/tests/ui/mir/static_fnptr.rs
@@ -0,0 +1,21 @@
+//! 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
+
+extern crate static_fnptr;
+use static_fnptr::{ADDR, bar};
+
+fn baz() -> bool {
+    bar(ADDR)
+}
+
+fn main() {
+    assert!(baz())
+}