Skip to content

Fix tail calls to #[track_caller] functions #144865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

WaffleLapkin
Copy link
Member

@WaffleLapkin WaffleLapkin commented Aug 3, 2025

We want #[track_caller] to be semver independent, i.e. it should not be a breaking change to add or remove it. Since it changes ABI of a function (adding an additional argument) we have to be careful to preserve this property when adding tail calls.

The only way to achieve this that I can see is:

  • we forbid tail calls in functions which are marked with #[track_caller] (already implemented)
  • tail-calling a #[track_caller] marked function downgrades the tail-call to a normal call (or equivalently tail-calls the shim made by fn def to fn ptr cast) (this pr)

Ideally the downgrade would be performed by a MIR pass, but that requires post mono MIR opts (cc @saethlin, #131650). For now I've changed code in cg_ssa to accomodate this behaviour (+ added a hack to mono collector so that the shim is actually generated)

Additionally I added a lint, although I don't think it's strictly necessary.

Alternative to #144762 (and thus closes #144762)
Fixes #144755

@WaffleLapkin WaffleLapkin added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. F-track_caller `#![feature(track_caller)]` F-explicit_tail_calls `#![feature(explicit_tail_calls)]` labels Aug 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 3, 2025

r? @fee1-dead

rustbot has assigned @fee1-dead.
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

@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. labels Aug 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 3, 2025

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin
Copy link
Member Author

I do not get it, both tests pass for me locally ?_?

@WaffleLapkin
Copy link
Member Author

Update: @jieyouxu figured this out. It looks like the test suite that failed was running with --pass=check, which caused the tests that depend on diagnostic from codegen backend to fail. I've added //@ ignore-pass so it should be good now

@fee1-dead
Copy link
Member

not too familiar with tail calls but the diff looks good to me

r? compiler

@rustbot rustbot assigned petrochenkov and unassigned fee1-dead Aug 5, 2025
@WaffleLapkin
Copy link
Member Author

r? lqd maybe (since you were excited about tail calls ^^')

@rustbot rustbot assigned lqd and unassigned petrochenkov Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. F-explicit_tail_calls `#![feature(explicit_tail_calls)]` F-track_caller `#![feature(track_caller)]` 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.

cannot guarantee tail call due to mismatched parameter counts
6 participants