You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.. guideline:: Do Not Depend on Function Pointer Identity Across Crates
67
+
:id: gui_QbvIknd9qNF6
68
+
:category: required
69
+
:status: draft
70
+
:release: unclear-latest
71
+
:fls: fls_1kg1mknf4yx7
72
+
:decidability: decidable
73
+
:scope: system
74
+
:tags: surprising-behavior
75
+
76
+
Do not rely on the equality or stable identity of function pointers originating from different crates or that may be inlined, duplicated, or instantiated differently across compilation units, codegen units, or optimization profiles.
77
+
78
+
Avoid assumptions about low-level metadata (such as symbol addresses) unless explicitly guaranteed by the Ferrocene Language Specification (FLS). Function address identity is not guaranteed by Rust and must not be treated as stable. Rust’s fn type is a zero-sized function item promoted to a function pointer, whose address is determined by the compiler backend. When a function resides in a different crate, or when optimizations such as inlining, link-time optimization, or codegen-unit partitioning are enabled, the compiler may generate multiple distinct code instances for the same function or alter the address at which it is emitted.
79
+
80
+
Consequently, the following operations are not reliable:
81
+
82
+
- Comparing function pointers for equality (``fn1 == fn2``)
83
+
- Assuming a unique function address
84
+
- Using function pointers as identity keys (e.g., in maps, registries, matchers)
85
+
- Matching behavior based on function address
86
+
87
+
This rule applies even when the functions are semantically identical, exported as ``pub``, or defined once in source form.
88
+
89
+
.. rationale::
90
+
:id: rat_xcVE5Hfnbb2u
91
+
:status: draft
92
+
93
+
Compiler optimizations may cause function pointers originating from different crates to lose stable identity. Observed behaviors include:
- Nondeterministic behavior: correctness depending on build flags or incremental state.
109
+
- Test-only correctness: function pointer equality passing in debug builds but failing in release/LTO builds.
110
+
111
+
In short, dependence on function address stability introduces non-portable, build-profile-dependent behavior, which is incompatible with high-integrity Rust.
112
+
113
+
.. non_compliant_example::
114
+
:id: non_compl_ex_MkAkFxjRTijx
115
+
:status: draft
116
+
117
+
Due to cross-crate inlining or codegen-unit partitioning,
118
+
the address of ``handler_a`` in crate ``B`` may differ from its address in crate A,
119
+
causing comparisons to fail as shown in this noncompliant code example:
120
+
121
+
.. code-block:: rust
122
+
123
+
// crate A
124
+
pub fn handler_a() {}
125
+
pub fn handler_b() {}
126
+
rust
127
+
128
+
// crate B
129
+
use crate_a::{handler_a, handler_b};
130
+
131
+
fn dispatch(f: fn()) {
132
+
if f == handler_a {
133
+
println!("Handled by A");
134
+
} else if f == handler_b {
135
+
println!("Handled by B");
136
+
}
137
+
}
138
+
139
+
dispatch(handler_a);
140
+
141
+
// Error: This may fail unpredictably if handler_a is inlined or duplicated.
142
+
143
+
.. compliant_example::
144
+
:id: compl_ex_oiqSSclTXmIi
145
+
:status: draft
146
+
147
+
Replace function pointer comparison with an explicit enum as shown in this compliant example:
0 commit comments