Skip to content

Add support for splatted function pointers - #159643

Open
teor2345 wants to merge 3 commits into
rust-lang:mainfrom
teor2345:splat-fn-ptr
Open

Add support for splatted function pointers#159643
teor2345 wants to merge 3 commits into
rust-lang:mainfrom
teor2345:splat-fn-ptr

Conversation

@teor2345

@teor2345 teor2345 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Tracking issue: #153629

This PR adds support for splatted function pointers.

Currently splatted function pointers ICE due to unpopulated side-tables. This PR fixes the ICE by populating the side-table correctly, and fixes the MIR lowering code. It also refactors the surrounding code to reduce code duplication.

Generic function pointers also work. I'm sure if there's something extra we need to do to support all generic function pointers?

Close #158603

@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 21, 2026
@teor2345

This comment was marked as resolved.

@rustbot rustbot added F-splat `#![feature(splat)]` https://github.com/rust-lang/rust/issues/153629 I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Jul 21, 2026
@teor2345

Copy link
Copy Markdown
Contributor Author

@rustbot label +C-bug

@rustbot rustbot added the C-bug Category: This is a bug. label Jul 21, 2026
@rust-log-analyzer

This comment has been minimized.

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

rustbot commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

r? @petrochenkov

rustbot has assigned @petrochenkov.
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 20 candidates

@petrochenkov

Copy link
Copy Markdown
Contributor

@rustbot reroll

@rustbot rustbot assigned folkertdev and unassigned petrochenkov Jul 23, 2026
Comment thread compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs Outdated

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR got quite big and seemingly fixes a bunch of different things, is there any chance you can split it up? At least the refactoring into using the custom enums seems like it could be split out, just stubbing out the branches for FnPtr and errors. If possible a separate PR would be nice (just assign me as a reviewer to preserve context), otherwise separate commits would also work.

It's just really tough to review like this.

View changes since this review

Comment thread tests/ui/splat/splat-fn-ptr-generic.rs Outdated

fn main() {
// FIXME(rustfmt): the attribute gets deleted by rustfmt
#[rustfmt::skip]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might be better to put the #[rustfmt::skip] on the function to reduce noise?

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.

Happy to do that. It's better for readability, but slightly worse for maintenance because now every line is unformatted, not just the splatted type lines.

Ultimately we'll fix this in rustfmt if function argument attributes become a thing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We usually just use assert_eq!, is there any particular reason to deviate from that? Your generic<T: Tuple + Debug>(#[splat] a: T) could use format! and return a String?

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.

No particular reason, it will be much more readable to use assert_eq! across all the splat UI tests. The generic case can just return the argument.

A codegen bug could swap the tupled argument and return value, or overwrite the return value, but the multi-argument case will catch that. Or it might be worth having a test that swaps the order of the arguments in the return value. I'll have a think about it.

receiver: Option<&'tcx hir::Expr<'tcx>>,
has_receiver: bool,
// This is the method receiver, or the function path
receiver_or_func: &'tcx hir::Expr<'tcx>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this still feels kind of wrong, although I don't have any obviously better suggestions.

@teor2345 teor2345 Jul 27, 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.

To make the states clearer, it could be:

enum SplattedFunc {
    FnDefReceiver(Expr),
    /* NoFnDefReceiver, edit: can't be known at call site */
    FnPtrExpression(Expr),
}

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

rustbot commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

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

@teor2345 teor2345 left a comment

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.

This PR got quite big and seemingly fixes a bunch of different things, is there any chance you can split it up? At least the refactoring into using the custom enums seems like it could be split out, just stubbing out the branches for FnPtr and errors. If possible a separate PR would be nice (just assign me as a reviewer to preserve context), otherwise separate commits would also work.

That's good feedback. I ended up squashing the whole thing to make it easier to rebase and rewrite, but I didn't split it into commits again.

I think it can be split into something like:

  • inline the splatted_callee function
  • refactor using custom enums (with stubs)
  • support for splatted FnPtrs

In a separate PR:

  • update tests to use assert_eq! (and a single rustfmt::skip)
    • edit: for FnPtr specific tests, I made this change in the commit where I added FnPtr support
  • remove resolved FIXMEs in splat UI tests

If you'd like some of those commits in different PRs, or in a different order, please let me know.

View changes since this review

receiver: Option<&'tcx hir::Expr<'tcx>>,
has_receiver: bool,
// This is the method receiver, or the function path
receiver_or_func: &'tcx hir::Expr<'tcx>,

@teor2345 teor2345 Jul 27, 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.

To make the states clearer, it could be:

enum SplattedFunc {
    FnDefReceiver(Expr),
    /* NoFnDefReceiver, edit: can't be known at call site */
    FnPtrExpression(Expr),
}

Comment thread tests/ui/splat/splat-fn-ptr-generic.rs Outdated

fn main() {
// FIXME(rustfmt): the attribute gets deleted by rustfmt
#[rustfmt::skip]

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.

Happy to do that. It's better for readability, but slightly worse for maintenance because now every line is unformatted, not just the splatted type lines.

Ultimately we'll fix this in rustfmt if function argument attributes become a thing.

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.

No particular reason, it will be much more readable to use assert_eq! across all the splat UI tests. The generic case can just return the argument.

A codegen bug could swap the tupled argument and return value, or overwrite the return value, but the multi-argument case will catch that. Or it might be worth having a test that swaps the order of the arguments in the return value. I'll have a think about it.

@rustbot

This comment has been minimized.

@teor2345

Copy link
Copy Markdown
Contributor Author

I split into these commits:

  • inline the splatted_callee function
  • refactor using custom enums (with stubs)
  • support for splatted FnPtrs
    • including test cleanups for FnPtr tests

And I opened PR #160049 with the unrelated test cleanups.

@rustbot ready

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

This comment has been minimized.

teor2345 added 3 commits July 29, 2026 10:07
Add tests for generic function pointers
Change FnPtr tests to use assert_eq!() rather than println!()
@rustbot

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

jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 29, 2026
…olkertdev

Use assert_eq! in splat codegen tests

Tracking issue: rust-lang#153629

This PR does some cleanups in the splat UI tests from folkert's PR rust-lang#159643 review:
- move rustfmt::skip to main() and deduplicate it: rust-lang#159643 (comment)
- use assert_eq!() to test codegen rather than println!(): rust-lang#159643 (comment)
- delete resolved FIXME comments about supporting tupled and de-tupled calls to splatted functions

r? @folkertdev

@rustbot label +F-splat +C-cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bug Category: This is a bug. F-splat `#![feature(splat)]` https://github.com/rust-lang/rust/issues/153629 I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE] due to #[splat] in a function pointer type

5 participants