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.

@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.

@jieyouxu

This comment was marked as outdated.

@rustbot rustbot assigned TaKO8Ki and unassigned jieyouxu Aug 24, 2026
@rustbot rustbot added the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 25, 2026
@traviscross traviscross added the P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang label Aug 26, 2026
@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Sep 23, 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.

@rust-log-analyzer

This comment has been minimized.

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

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 I-lang-nominated Nominated for discussion during a lang team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang 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.