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 2 commits 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"

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

/// is no different than a normal call (except for changes in drop order).
pub TAIL_CALL_TRACK_CALLER,
Warn,
"detects tail calls of functions 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.

Could you gate this behind the tail call feature?

I can't remember the syntax but I think it's something like @feature = ....

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not know this is something that can be done!

The syntax is apparently @feature_gate = ident;. I've gated the lint.

@lqd
Copy link
Member

lqd commented Aug 14, 2025

🚀

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 14, 2025

📌 Commit fa18b3e has been approved by lqd

It is now in the queue for this repository.

@bors bors 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 Aug 14, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 15, 2025
Fix tail calls to `#[track_caller]` functions

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,` rust-lang#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 rust-lang#144762 (and thus closes rust-lang#144762)
Fixes rust-lang#144755
bors added a commit that referenced this pull request Aug 15, 2025
Rollup of 22 pull requests

Successful merges:

 - #118087 (Add Ref/RefMut try_map method)
 - #122661 (Change the desugaring of `assert!` for better error output)
 - #140740 (Add `-Zindirect-branch-cs-prefix`)
 - #142640 (Implement autodiff using intrinsics)
 - #143075 (compiler: Allow `extern "interrupt" fn() -> !`)
 - #144865 (Fix tail calls to `#[track_caller]` functions)
 - #144944 (E0793: Clarify that it applies to unions as well)
 - #144947 (Fix description of unsigned `checked_exact_div`)
 - #145004 (Couple of minor cleanups)
 - #145005 (strip prefix of temporary file names when it exceeds filesystem name length limit)
 - #145012 (Tail call diagnostics to include lifetime info)
 - #145065 (resolve: Introduce `RibKind::Block`)
 - #145120 (llvm: Accept new LLVM lifetime format)
 - #145189 (Weekly `cargo update`)
 - #145235 (Minor `[const]` tweaks)
 - #145275 (fix(compiler/rustc_codegen_llvm): apply `target-cpu` attribute)
 - #145322 (Resolve the prelude import in `build_reduced_graph`)
 - #145331 (Make std use the edition 2024 prelude)
 - #145369 (Do not ICE on private type in field of unresolved struct)
 - #145378 (Add `FnContext` in parser for diagnostic)
 - #145389 ([rustdoc] Revert "rustdoc search: prefer stable items in search results")
 - #145392 (coverage: Remove intermediate data structures from mapping creation)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 15, 2025
Fix tail calls to `#[track_caller]` functions

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,`` rust-lang#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 rust-lang#144762 (and thus closes rust-lang#144762)
Fixes rust-lang#144755
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 15, 2025
Fix tail calls to `#[track_caller]` functions

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,``` rust-lang#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 rust-lang#144762 (and thus closes rust-lang#144762)
Fixes rust-lang#144755
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-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.

cannot guarantee tail call due to mismatched parameter counts
9 participants