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
@lqd
Copy link
Member

lqd commented Aug 6, 2025

It also LGTM -- though to be clear, I'm not familiar with the #[track_caller] infra. I wonder if people will have questions about the wording "has no special effect", as in "will not be TCEd yet".

Also, do we have to involve t-lang for new lints?

@VorpalBlade
Copy link

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)

Would this be a silent downgrade? I think that would be a terrible idea, since for me the main usage of become is guaranteed tail calls for threaded interpreters. The tail call turning into a normal call means the code won't work and will eventually overflow the stack. This sort of implicit behaviour is the antithesis of what rust stands for.

@WaffleLapkin
Copy link
Member Author

@lqd the wording is certainly not ideal, although I'm not entirely sure what would be the proper way to describe this situation. I don't think T-lang needs to be involved, since this lint is triggered only by an unstable/experimental feature (but they'll certainly have to look into the lint before stabilizing the feature).

@VorpalBlade it will not be silent, as this PR also adds a lint. However, note that functions marked with #[track_caller] can't tail call other functions, and as such can't participate in any tail call cycles, so you don't have any guarantees about stack exhaustion regardless.

@lqd
Copy link
Member

lqd commented Aug 7, 2025

Ok I wasn't aware of the procedure for new lints.

Copy link
Member

@lqd lqd left a comment

Choose a reason for hiding this comment

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

👍


declare_lint! {
/// The `tail_call_track_caller` lint detects usage of `become` attempting to tail call
/// function marked with `#[track_caller]`.
Copy link
Member

Choose a reason for hiding this comment

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

nit, "a function" or "functions"

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
7 participants