Skip to content

Implement Fn traits for safe functions of any ABI - #160186

Open
Jules-Bertholet wants to merge 1 commit into
rust-lang:mainfrom
Jules-Bertholet:fn-traits-any-abi
Open

Jules-Bertholet wants to merge 1 commit into
rust-lang:mainfrom
Jules-Bertholet:fn-traits-any-abi

Conversation

@Jules-Bertholet

@Jules-Bertholet Jules-Bertholet commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This brings the behavior of the language in line with its documentation:

/// In addition, all *safe* function pointers implement [`Fn`], [`FnMut`], and [`FnOnce`], because
/// these traits are specially known to the compiler.

ABIs that are not callable, or that are internal implementation details, are excluded.

Reference PR: rust-lang/reference#2383

@rustbot label T-lang needs-fcp A-ABI A-extern-fn I-lang-nominated

@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 29, 2026
@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 74 candidates
  • Random selection from 16 candidates

@rustbot rustbot added A-ABI Area: Concerning the application binary interface (ABI) A-extern-fn Area: `extern` functions needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-lang Relevant to the language team labels Jul 29, 2026
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Don't you need to generate shims or Happy Things will happen?

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

I don't think so? The basic tests I added work fine, and Miri doesn't complain. But I don't really know that part of the codebase, am relying on Cunningham's Law

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

I suggest you check with &dyn Fn(), things are more likely to break there.

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Nothing broke…

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

🤷 Maybe it is fine...

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Although the practical ABI for &isize and isize is identical for C and Rust, so it will compile fine and I don't know if Miri can detect a mismatch - maybe try something like [usize; 2], that appears to have a different ABI between Rust and C?

@ds84182

ds84182 commented Jul 30, 2026

Copy link
Copy Markdown

Probably should make sure the function that calls the &dyn Fn is not inlined.

Comment thread src/tools/miri/tests/pass/fn-traits-abi.rs
@RalfJung

Copy link
Copy Markdown
Member

Yeah I'd expect the rust-call / argument (un)tupling to go wrong here as that needs the Rust ABI.

Does this actually solve a problem? If not I'd rather update the docs.

@Jules-Bertholet

Jules-Bertholet commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Modified the tests to use a function that I confirmed with Godbolt has a completely different "Rust" vs. "C" ABI. And everything still works

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Does this actually solve a problem?

With new ABIs like "tail", extern is not just for FFI anymore.

@RalfJung

Copy link
Copy Markdown
Member

I guess what happens is that we synthesize a call/call_once body for the traits when implementing them for a fn item, which discards the self argument, and which in this case also serves as an ABI adapter?

But someone should dig into where that happens.

Comment thread compiler/rustc_abi/src/extern_abi.rs

pub fn is_fn_trait_compatible(self) -> bool {
!self.c_variadic() && self.safety().is_safe() && self.abi() == ExternAbi::Rust
!self.c_variadic() && self.safety().is_safe() && self.abi().is_fn_trait_compatible()

@Jules-Bertholet Jules-Bertholet Jul 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
!self.c_variadic() && self.safety().is_safe() && self.abi().is_fn_trait_compatible()
self.safety().is_safe() && self.abi().is_fn_trait_compatible()

We could try to remove the !self.c_variadic() condition. But there's not much point, as any actual use of variadics is necessarily unsafe. It would also be a bunch of work, as the trait solver currently assumes no type has more than one impl of each Fn trait.

View changes since the review

@Jules-Bertholet

This comment was marked as outdated.

@rustbot rustbot assigned chenyukang and unassigned jackh726 Aug 15, 2026
@chenyukang

This comment was marked as outdated.

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Oct 11, 2026
@teor2345

Copy link
Copy Markdown
Member

Does this need an update to the Rust Reference, which currently says:

Fn, FnMut, and FnOnce are implemented unless the function has any of the following:
…

  • an ABI other than "Rust"

https://doc.rust-lang.org/stable/reference/types/function-item.html#r-type.fn-item.traits

Or do we do those kinds of updates later?

@theemathas theemathas added the needs-reference-pr This language change needs an approved Reference PR to proceed. label Oct 11, 2026
@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Reference PR: rust-lang/reference#2383

@rust-bors

This comment has been minimized.

This brings the behavior of the language in line with the documentation
in `library/core/src/primitive_docs.rs`.
@rustbot

rustbot commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@tmandry

tmandry commented Oct 14, 2026

Copy link
Copy Markdown
Member

@rfcbot fcp merge lang

We discussed this in the lang meeting today, and we were in agreement that this is a positive change. We should do it if we can get away with it (pending crater results).

@rfcbot concern wait for crater results

@rust-rfcbot

rust-rfcbot commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

@tmandry has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. and removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Oct 14, 2026
@traviscross

Copy link
Copy Markdown
Contributor

Makes sense to me. Thanks @Jules-Bertholet (and thanks for filing the Reference PR).

@rfcbot reviewed

@joshtriplett

Copy link
Copy Markdown
Member

@rfcbot reviewed

@joshtriplett

Copy link
Copy Markdown
Member

I remember we talked about tracking this in some fashion, but I don't think that applies to the Fn traits. We want it for lowercase fn types, to distinguish "this is a pointer to a cdecl function" vs "this is a pointer to a stdcall function". But for capital-F Fn, we only care whether Rust knows how to call it.

@lcnr

lcnr commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

I see some discussion about whether this causes ABI compatability problems. Why does it not cause any such issues, can this reasoning be written down somewhere?

@Jules-Bertholet

Jules-Bertholet commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Why does it not cause any such issues, can this reasoning be written down somewhere?

@teor2345 tracked it down in #160186 (comment). Essentially, nothing in build_call_shim() relies on a specific ABI

@RalfJung

Copy link
Copy Markdown
Member

Or put differently, IIUC this ends up generating a shim that serves as an ABI conversion trampoline.

@theemathas

Copy link
Copy Markdown
Contributor

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-160186 created and queued.
🤖 Automatically detected try build 895980e
⚠️ Try build based on commit d9d97c4, but latest commit is 4621dfe. Did you forget to make a new try build?
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 18, 2026
@craterbot

Copy link
Copy Markdown
Collaborator

🚧 Experiment pr-160186 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot

Copy link
Copy Markdown
Collaborator

🎉 Experiment pr-160186 is completed!
📊 0 regressed and 0 fixed (28882 total)
📊 1388 spurious results on the retry-regressed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-160186/retry-regressed-list.txt ↩

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Oct 19, 2026
@theemathas

Copy link
Copy Markdown
Contributor

@tmandry Could you resolve your concern? Thanks.

This branch has not been deployed

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

Labels

A-ABI Area: Concerning the application binary interface (ABI) A-extern-fn Area: `extern` functions disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-reference-pr This language change needs an approved Reference PR to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.