Skip to content

Enforce tail call type is related to body return type in borrowck #144917

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

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Aug 4, 2025

Like all call terminators, tail call terminators instantiate the binder of the callee signature with region variables and equate the arg operand types with that signature's args to ensure that the call is valid.

However, unlike normal call terminators, we were forgetting to also relate the return type of the call terminator to anything. In the case of tail call terminators, the correct thing is to relate it to the return type of the caller function (or in other words, the return local _0).

This meant that if the caller's return type had some lifetime constraint, then that constraint wouldn't flow through the signature and affect the args.

This is what's happening in the example test I committed:

fn link(x: &str) -> &'static str {
    become passthrough(x);
}

fn passthrough<T>(t: T) -> T { t }

fn main() {
    let x = String::from("hello, world");
    let s = link(&x);
    drop(x);
    println!("{s}");
}

Specifically, the type x is '?0 str, where '?0 is some universal arg. The type of passthrough is fn(&'?1 str) -> &'?1 str. Equating the args sets '?0 = '?1. However, we need to also equate the return type &'?1 str to &'static str so that we eventually require that '?0 = 'static, which is a borrowck error!


Look at the first commit for the functional change, and the second commit is just a refactor because we don't need to pass Option<BasicBlock> to check_call_dest, but just whether or not the terminator is expected to be diverging (i.e. if the return type is !).

Fixes #144916

@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2025

r? @lcnr

rustbot has assigned @lcnr.
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 4, 2025
@compiler-errors
Copy link
Member Author

r? @WaffleLapkin though lcnr can review it too if they want, this change should be pretty straightforward.

@rustbot rustbot assigned WaffleLapkin and unassigned lcnr Aug 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2025

WaffleLapkin is not on the review rotation at the moment.
They may take a while to respond.

} else {
ConstraintCategory::UseAsConst
}
if is_diverging {
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 don't really care about the ordering here but the true branch seemed easier to put first

@compiler-errors compiler-errors force-pushed the tail-call-linked-lifetimes branch from 4504cdb to a573fd9 Compare August 4, 2025 17:26
@compiler-errors compiler-errors changed the title Tail call linked lifetimes Enforce tail call type is related to body return type in borrowck Aug 4, 2025
@lcnr
Copy link
Contributor

lcnr commented Aug 5, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 5, 2025

📌 Commit a573fd9 has been approved by lcnr

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 5, 2025
@WaffleLapkin WaffleLapkin added the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-explicit_tail_calls `#![feature(explicit_tail_calls)]` 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.

Tail calls don't enforce lifetime constraints from return type onto args
5 participants