Skip to content

[Coding Guideline]: Do Not Depend on Function Pointer Identity#256

Open
rcseacord wants to merge 13 commits into
Safety-Critical-Rust-Consortium:mainfrom
rcseacord:feat/fpident
Open

[Coding Guideline]: Do Not Depend on Function Pointer Identity#256
rcseacord wants to merge 13 commits into
Safety-Critical-Rust-Consortium:mainfrom
rcseacord:feat/fpident

Conversation

@rcseacord

Copy link
Copy Markdown
Collaborator

Coding guidelines based on #255

@netlify

netlify Bot commented Dec 6, 2025

Copy link
Copy Markdown

Deploy Preview for scrc-coding-guidelines ready!

Name Link
🔨 Latest commit 3242afc
🔍 Latest deploy log https://app.netlify.com/projects/scrc-coding-guidelines/deploys/697fe7ca8312120008ec298f
😎 Deploy Preview https://deploy-preview-256--scrc-coding-guidelines.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated
Comment thread src/coding-guidelines/types-and-traits.rst Outdated

- Comparing function pointers for equality (``fn1 == fn2``)
- Assuming a unique function address
- Using function pointers as identity keys (e.g., in maps, registries, matchers)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can still use function pointers as keys, they just don't have a one-to-one relationship to function items, so you can't rely on a function item giving the same function pointer or different items giving different pointers. (ignoring #[no_mangle]). so e.g. you can have a HashSet<fn()> and it works just fine as long as you don't assume inserting any particular function item does anything other than guaranteeing calling one or more of the entries is equivalent to calling that function item.

@rcseacord rcseacord changed the title [Coding Guideline]: Do Not Depend on Function Pointer Identity Across Crates [Coding Guideline]: Do Not Depend on Function Pointer Identity Dec 7, 2025
@felix91gr

felix91gr commented Dec 15, 2025

Copy link
Copy Markdown
Collaborator

@rcseacord you'll want to consider this discussion before the guideline is ready for merge rust-lang/unsafe-code-guidelines#589, since it addresses the same topic

@PLeVasseur

Copy link
Copy Markdown
Collaborator

FYI @rcseacord @manhatsu -- I got this building again, aside from the code examples, see: #287

Feel free to just yoink the commit from there and replace the commits on this feature branch (i.e. cherry-pick that commit I linked above onto your local updated main, then force-push to your remote branch).

That way we keep all the comment history from on here.

(The coding examples also need to be updated to compile)

Comment thread src/coding-guidelines/types-and-traits/gui_QbvIknd9qNF6.rst.inc Outdated
Comment thread src/coding-guidelines/types-and-traits/gui_QbvIknd9qNF6.rst.inc Outdated
PLeVasseur and others added 12 commits February 2, 2026 08:03
Clarify function pointer stability issues and provide examples of potential problems and compliant solutions.
Added a non-compliant example demonstrating issues with function address stability and uninitialized memory in Rust.
Updated references and clarified text regarding function pointer stability and the implications of using `#[no_mangle]` functions. Added citations for better context and understanding.
Refactor handler registration to pass mutable vector.
Clarify guideline on function pointer comparison and provide examples of indirect comparisons.
@plaindocs

Copy link
Copy Markdown
Collaborator

No worries @vccjgust, we're just ironing out some kinks in the process and I wasn't sure you'd received the notification.

@vccjgust vccjgust left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I have a few small comments. I'm not sure what to do about many of the non-compliant examples since they actually work fine when run as-is in Rust Playground. It would of course be best if we could come up with demonstrators that actually show case the unexpected behaviour, but I'm not sure how to do that given that it relies on cross-crate linking behaviour etc.

:status: draft

In this noncompliant example, the ``write_first`` and ``write_second`` functions each
initialize one field within a ``MaybeUninit`` and write ``uninit`` to the other. If

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MaybeUninit -> MyMaybeUninit? It is easy for the reader to confuse this with Rust's own MaybeUninit. Perhaps rename to something a bit more unambiguous?

#[no_mangle]
fn write_first(a: &mut MyMaybeUninit) {
*a = MyMaybeUninit { init: (0, 1, 2, 3) };
*a = MyMaybeUninit { uninit: () };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused what this is intended to show since the uninit field is zero sized, so writing to it won't actually change the bit pattern (i.e. this gets compiled down to nothing)

function or alter the address at which it is emitted.

Consequently, the following operations are unreliable for functions which are not
``#[no_mangle]``:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The non-compliant example with MyMaybeUninit below shows that even #[no_mangle] can get merged into one

a.init.3 = 3;
}

#[no_mangle]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the 2024 edition will insist to use unsafe all over the place here. How do we show that this only compiles for <2024 edition?

Comment on lines +388 to +404
#![allow(unpredictable_function_pointer_comparisons)]

mod crate_a {
pub fn handler_a() {}
pub fn handler_b() {}
}

fn dispatch(f: fn()) {
if f == crate_a::handler_a {
println!("Handled by A");
} else if f == crate_a::handler_b {
println!("Handled by B");
}
}

fn main() {
dispatch(crate_a::handler_a);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to demonstrate this issue properly here, but this example as it is currently written, behaves exactly as expected when running in Rust Playground (at least for me)

@github-actions

Copy link
Copy Markdown
Contributor

✅ Rectified PR #256: latest review by @vccjgust is COMMENTED; refreshed reviewer activity.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Review Reminder

Hey @vccjgust, it's been more than 14 days since you were assigned to review this.

Please take one of the following actions:

  1. Begin your review - Post a comment with your feedback
  2. Pass the review - Use @guidelines-bot /pass [reason] to assign the next reviewer
  3. Step away temporarily - Use @guidelines-bot /away YYYY-MM-DD [reason] if you need time off

If no action is taken within 14 days, you may be transitioned from Producer to Observer status per our contribution guidelines.

Life happens! If you're dealing with something, just let us know.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

12 similar comments
@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@github-actions

Copy link
Copy Markdown
Contributor

🔔 Transition Period Ended

@vccjgust, the 14-day transition period has passed without activity on this review.

Per our contribution guidelines, this may result in a transition from Producer to Observer status.

The review will now be reassigned to the next person in the queue.

If you believe this is in error or have extenuating circumstances, please reach out to the subcommittee.

@programmerjake

programmerjake commented Mar 26, 2026

Copy link
Copy Markdown

please fix the broken bot, I'm unsubscribing from this PR due to the bot spam.

@github-actions github-actions Bot added the status: awaiting reviewer response Reviewer-bot is waiting on reviewer freshness or current-head review label Mar 26, 2026
@PLeVasseur

Copy link
Copy Markdown
Collaborator

Hey @programmerjake -- can appreciate that. Sorry for the spam.

Also know that the bot was started as a best-faith effort to make it easier for us to queue up reviewers who said they'd not have time for full writing efforts, but would like to review. Unfortunately it was broken for 1, maybe 2 days.

It's stopped as of midnight or so last night when I quelled it. ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chapter: types-and-traits coding guideline An issue related to a suggestion for a coding guideline status: awaiting contributor response Reviewer-bot is waiting on contributor follow-up or completion

Development

Successfully merging this pull request may close these issues.

8 participants