Skip to content

check extern "custom" function pointers#159780

Open
folkertdev wants to merge 8 commits into
rust-lang:mainfrom
folkertdev:extern-custom-check-fn-ptrs
Open

check extern "custom" function pointers#159780
folkertdev wants to merge 8 commits into
rust-lang:mainfrom
folkertdev:extern-custom-check-fn-ptrs

Conversation

@folkertdev

@folkertdev folkertdev commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

tracking issue: #140829
related RFC: rust-lang/rfcs#3980

Best reviewed commit-by-commit.

This PR makes 3 changes

  • extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various interupt ABIs and extern "custom".
  • remove the ability for extern "custom" to return !
  • improve the suggestion when safe is used in a function pointer type

@folkertdev folkertdev added the F-abi_custom `#![feature(abi_custom)]` label Jul 23, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 23, 2026
@folkertdev
folkertdev force-pushed the extern-custom-check-fn-ptrs branch 5 times, most recently from b71966b to a8807aa Compare July 23, 2026 19:41
Comment on lines 685 to 689
&& match &ret_ty.kind {
TyKind::Never => false,
TyKind::Never if abi != ExternAbi::Custom => false,
TyKind::Tup(tup) if tup.is_empty() => false,
_ => true,
}

@folkertdev folkertdev Jul 23, 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.

@WaffleLapkin As requested, ! is now no longer allowed as a return type to extern "custom". Reading this code now, I think the ability to return ! just accidentally fell out of the logic here, and it was less of a deliberate inclusion.

We can always allow it again later, if a compelling reason comes up.

View changes since the review

@folkertdev
folkertdev marked this pull request as ready for review July 23, 2026 20:02
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 23, 2026
@rustbot

rustbot commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
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 19 candidates

@WaffleLapkin WaffleLapkin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

&self,
abi: ExternAbi,
ctxt: FnCtxt,
opt_ident: Option<&Ident>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you document what the ident means/is used for?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe function_name: ... + "function_name is none iff this is called for a function pointer type"?

Comment thread compiler/rustc_ast/src/ast.rs Outdated
self.header.ext.span().unwrap_or(self.safety_span().shrink_to_hi())
}

pub fn as_borrowed<'a>(&'a self) -> BorrowedFnSig<'a> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
pub fn as_borrowed<'a>(&'a self) -> BorrowedFnSig<'a> {
pub fn as_borrowed(&self) -> BorrowedFnSig<'_> {

Comment on lines +2487 to +2490

pub fn as_borrowed_fn_sig<'a>(&'a self) -> BorrowedFnSig<'a> {
BorrowedFnSig { header: self.header(), decl: &self.decl, span: self.decl_span }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit; this (and some other unexpected changes) is in the "update tests now that we check the ABI of function pointer types" commit, seems like something got messed up during a rebase or something...

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.

the changes were intentional, I've updated the commit name

Comment on lines 681 to 685
&& match &ret_ty.kind {
TyKind::Never => false,
TyKind::Never if abi != ExternAbi::Custom => false,
TyKind::Tup(tup) if tup.is_empty() => false,
_ => true,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe this could be a bit more readable?

Suggested change
&& match &ret_ty.kind {
TyKind::Never => false,
TyKind::Never if abi != ExternAbi::Custom => false,
TyKind::Tup(tup) if tup.is_empty() => false,
_ => true,
}
let allowed_return = |ty| match &ret_ty.kind {
TyKind::Never if abi != ExternAbi::Custom => true,
TyKind::Tup(tup) if tup.is_empty() => true,
_ => false,
};
if let FnRetTy::Ty(ref ret_ty) = sig.decl.output
&& !allowed_return(ret_ty)

Comment on lines +577 to +578
// An `extern "custom"` function must be unsafe.
self.reject_safe_fn(abi, ctxt, sig);
self.reject_safe_fn(abi, ctxt, sig, opt_ident.is_none());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think instead of special casing function pointers inside of reject_safe_fn, just not call it for them

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.

For extern "custom" an error message should be generated on a missing unsafe also: extern "custom" must always be unsafe.

@rustbot rustbot assigned WaffleLapkin and unassigned nnethercote Jul 24, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 24, 2026
@rustbot

rustbot commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 24, 2026
@WaffleLapkin

Copy link
Copy Markdown
Member

extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various interupt ABIs and extern "custom".

Are any of these stable? i.e. is this technically a breaking change we should worry about?

@folkertdev
folkertdev force-pushed the extern-custom-check-fn-ptrs branch from 5d6144e to d0dde60 Compare July 24, 2026 19:04
@rustbot

rustbot commented Jul 24, 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.

@folkertdev

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 24, 2026

@WaffleLapkin WaffleLapkin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rust-bors

rust-bors Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

📌 Commit d0dde60 has been approved by WaffleLapkin

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 1. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious CI failures

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 24, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 24, 2026
…ptrs, r=WaffleLapkin

check `extern "custom"` function pointers

tracking issue: rust-lang#140829
related RFC: rust-lang/rfcs#3980

Best reviewed commit-by-commit.

This PR makes 3 changes

- extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various `interupt` ABIs and `extern "custom"`.
- remove the ability for `extern "custom"` to return `!`
- improve the suggestion when `safe` is used in a function pointer type
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 24, 2026
…ptrs, r=WaffleLapkin

check `extern "custom"` function pointers

tracking issue: rust-lang#140829
related RFC: rust-lang/rfcs#3980

Best reviewed commit-by-commit.

This PR makes 3 changes

- extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various `interupt` ABIs and `extern "custom"`.
- remove the ability for `extern "custom"` to return `!`
- improve the suggestion when `safe` is used in a function pointer type
rust-bors Bot pushed a commit that referenced this pull request Jul 24, 2026
Rollup of 17 pull requests

Successful merges:

 - #158168 (Added implementation on `set_permissions_nofollow` for all primary platforms)
 - #138618 (Support using const pointers in asm `const` operand)
 - #157962 (Function item should not be used as const arg)
 - #158404 (trait_solver: normalize next-gen region constraints)
 - #158709 (rustdoc: warn on improperly interleaved HTML/MD)
 - #159720 (document #[global_allocator] constraints)
 - #159732 (optimization: don't look for diagnostic/canonical items without rustc_attrs enabled)
 - #159740 (reuse regular exported_non_generic_symbols logic in Miri)
 - #159780 (check `extern "custom"` function pointers)
 - #159786 (rustdoc-js: ignore editor temp files in test folder discovery)
 - #159819 (std::sync::poison: disable auto_cfg on PoisonError::new)
 - #155388 (stepping into where-clauses during normalization may be productive)
 - #155914 (when bailing on ambiguity, don't force other results to ambig)
 - #159411 ([rustdoc] Correctly handle output options with --show-coverage)
 - #159439 (Fix(lib/fs/win): Fall back on Win32 delete for `Dir::remove_file`)
 - #159809 (Avoid `#[target_features]`)
 - #159826 (Remove redundant `#[rustc_paren_sugar]` feature gate)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 25, 2026
…ptrs, r=WaffleLapkin

check `extern "custom"` function pointers

tracking issue: rust-lang#140829
related RFC: rust-lang/rfcs#3980

Best reviewed commit-by-commit.

This PR makes 3 changes

- extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various `interupt` ABIs and `extern "custom"`.
- remove the ability for `extern "custom"` to return `!`
- improve the suggestion when `safe` is used in a function pointer type
rust-bors Bot pushed a commit that referenced this pull request Jul 25, 2026
Rollup of 16 pull requests

Successful merges:

 - #138618 (Support using const pointers in asm `const` operand)
 - #157962 (Function item should not be used as const arg)
 - #158404 (trait_solver: normalize next-gen region constraints)
 - #158709 (rustdoc: warn on improperly interleaved HTML/MD)
 - #159720 (document #[global_allocator] constraints)
 - #159732 (optimization: don't look for diagnostic/canonical items without rustc_attrs enabled)
 - #159740 (reuse regular exported_non_generic_symbols logic in Miri)
 - #159780 (check `extern "custom"` function pointers)
 - #159786 (rustdoc-js: ignore editor temp files in test folder discovery)
 - #159819 (std::sync::poison: disable auto_cfg on PoisonError::new)
 - #155388 (stepping into where-clauses during normalization may be productive)
 - #155914 (when bailing on ambiguity, don't force other results to ambig)
 - #159411 ([rustdoc] Correctly handle output options with --show-coverage)
 - #159439 (Fix(lib/fs/win): Fall back on Win32 delete for `Dir::remove_file`)
 - #159809 (Avoid `#[target_features]`)
 - #159826 (Remove redundant `#[rustc_paren_sugar]` feature gate)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 25, 2026
…ptrs, r=WaffleLapkin

check `extern "custom"` function pointers

tracking issue: rust-lang#140829
related RFC: rust-lang/rfcs#3980

Best reviewed commit-by-commit.

This PR makes 3 changes

- extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various `interupt` ABIs and `extern "custom"`.
- remove the ability for `extern "custom"` to return `!`
- improve the suggestion when `safe` is used in a function pointer type
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 25, 2026
…ptrs, r=WaffleLapkin

check `extern "custom"` function pointers

tracking issue: rust-lang#140829
related RFC: rust-lang/rfcs#3980

Best reviewed commit-by-commit.

This PR makes 3 changes

- extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various `interupt` ABIs and `extern "custom"`.
- remove the ability for `extern "custom"` to return `!`
- improve the suggestion when `safe` is used in a function pointer type
rust-bors Bot pushed a commit that referenced this pull request Jul 25, 2026
Rollup of 20 pull requests

Successful merges:

 - #138618 (Support using const pointers in asm `const` operand)
 - #157962 (Lower paths to functions in const args as ConstKind::Error)
 - #158404 (trait_solver: normalize next-gen region constraints)
 - #158709 (rustdoc: warn on improperly interleaved HTML/MD)
 - #159720 (document #[global_allocator] constraints)
 - #159732 (optimization: don't look for diagnostic/canonical items without rustc_attrs enabled)
 - #159738 (implement `CovariantUnsafeCell`)
 - #159740 (reuse regular exported_non_generic_symbols logic in Miri)
 - #159780 (check `extern "custom"` function pointers)
 - #159786 (rustdoc-js: ignore editor temp files in test folder discovery)
 - #159819 (std::sync::poison: disable auto_cfg on PoisonError::new)
 - #155388 (stepping into where-clauses during normalization may be productive)
 - #155914 (when bailing on ambiguity, don't force other results to ambig)
 - #159439 (Fix(lib/fs/win): Fall back on Win32 delete for `Dir::remove_file`)
 - #159676 (Update wasm-component-ld to 0.5.27)
 - #159730 (allow accessing the contents of UnsafeCell without going through get)
 - #159809 (Avoid `#[target_features]`)
 - #159826 (Remove redundant `#[rustc_paren_sugar]` feature gate)
 - #159853 (Updated expect messages for `CString` struct and method documentation)
 - #159877 (Revert "Export `derive` at `core::derive` and `std::derive`")
rust-bors Bot pushed a commit that referenced this pull request Jul 25, 2026
Rollup of 20 pull requests

Successful merges:

 - #138618 (Support using const pointers in asm `const` operand)
 - #157962 (Lower paths to functions in const args as ConstKind::Error)
 - #158404 (trait_solver: normalize next-gen region constraints)
 - #158709 (rustdoc: warn on improperly interleaved HTML/MD)
 - #159720 (document #[global_allocator] constraints)
 - #159732 (optimization: don't look for diagnostic/canonical items without rustc_attrs enabled)
 - #159738 (implement `CovariantUnsafeCell`)
 - #159740 (reuse regular exported_non_generic_symbols logic in Miri)
 - #159780 (check `extern "custom"` function pointers)
 - #159786 (rustdoc-js: ignore editor temp files in test folder discovery)
 - #159819 (std::sync::poison: disable auto_cfg on PoisonError::new)
 - #155388 (stepping into where-clauses during normalization may be productive)
 - #155914 (when bailing on ambiguity, don't force other results to ambig)
 - #159439 (Fix(lib/fs/win): Fall back on Win32 delete for `Dir::remove_file`)
 - #159676 (Update wasm-component-ld to 0.5.27)
 - #159730 (allow accessing the contents of UnsafeCell without going through get)
 - #159809 (Avoid `#[target_features]`)
 - #159826 (Remove redundant `#[rustc_paren_sugar]` feature gate)
 - #159853 (Updated expect messages for `CString` struct and method documentation)
 - #159877 (Revert "Export `derive` at `core::derive` and `std::derive`")
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jul 25, 2026
…ptrs, r=WaffleLapkin

check `extern "custom"` function pointers

tracking issue: rust-lang#140829
related RFC: rust-lang/rfcs#3980

Best reviewed commit-by-commit.

This PR makes 3 changes

- extend the ABI checks we already performed on function definitions and trait and foreign declarations to function pointer types. This touches the various `interupt` ABIs and `extern "custom"`.
- remove the ability for `extern "custom"` to return `!`
- improve the suggestion when `safe` is used in a function pointer type
rust-bors Bot pushed a commit that referenced this pull request Jul 25, 2026
Rollup of 23 pull requests

Successful merges:

 - #159720 (document #[global_allocator] constraints)
 - #159732 (optimization: don't look for diagnostic/canonical items without rustc_attrs enabled)
 - #159738 (implement `CovariantUnsafeCell`)
 - #159740 (reuse regular exported_non_generic_symbols logic in Miri)
 - #159780 (check `extern "custom"` function pointers)
 - #159786 (rustdoc-js: ignore editor temp files in test folder discovery)
 - #159819 (std::sync::poison: disable auto_cfg on PoisonError::new)
 - #155388 (stepping into where-clauses during normalization may be productive)
 - #155914 (when bailing on ambiguity, don't force other results to ambig)
 - #159204 (Add support to caller_location to rustc_public)
 - #159411 ([rustdoc] Correctly handle output options with --show-coverage)
 - #159439 (Fix(lib/fs/win): Fall back on Win32 delete for `Dir::remove_file`)
 - #159676 (Update wasm-component-ld to 0.5.27)
 - #159730 (allow accessing the contents of UnsafeCell without going through get)
 - #159809 (Avoid `#[target_features]`)
 - #159810 (Add tuple never coercion collection regression test)
 - #159826 (Remove redundant `#[rustc_paren_sugar]` feature gate)
 - #159853 (Updated expect messages for `CString` struct and method documentation)
 - #159877 (Revert "Export `derive` at `core::derive` and `std::derive`")
 - #159878 (bootstrap: Remove obsolete option `build.compiletest-use-stage0-libtest`)
 - #159882 (Update expect messages in library/alloc/boxed.rs and library/alloc/string.rs to follow the style guide)
 - #159891 (Split multiline derives into std/rustc macros)
 - #159895 (rustc-dev-guide subtree update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-abi_custom `#![feature(abi_custom)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants