Skip to content

Tail call diagnostics to include lifetime info #145012

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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_mir_build/src/check_tail_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {

if caller_sig.inputs_and_output != callee_sig.inputs_and_output {
if caller_sig.inputs() != callee_sig.inputs() {
self.report_arguments_mismatch(expr.span, caller_sig, callee_sig);
self.report_arguments_mismatch(
expr.span,
self.caller_ty.fn_sig(self.tcx).skip_binder(),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.caller_ty.fn_sig(self.tcx).skip_binder(),
self.caller_ty.fn_sig(self.tcx).instantiate_identity(),

Copy link
Member Author

Choose a reason for hiding this comment

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

It does not work for me

error[E0599]: no method named `instantiate_identity` found for struct `Binder` in the current scope
   --> compiler/rustc_mir_build/src/check_tail_calls.rs:137:53
    |
137 |                     self.caller_ty.fn_sig(self.tcx).instantiate_identity(),
    |                                                     ^^^^^^^^^^^^^^^^^^^^ method not found in `Binder<TyCtxt<'_>, FnSig<TyCtxt<'_>>>`

Copy link
Member

Choose a reason for hiding this comment

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

oh, it's a binder, not an early binder. that should be liberated then probably, use tcx.liberate_late_bound_regions instead.

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'd love to hear why current approach is not best just to learn things

Also about 'static yes it shows, should I include this in test?

4 |     become bar(dummy);
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: -Ztrack-diagnostics: created at compiler/rustc_mir_build/src/check_tail_calls.rs:368:14
  = note: `become` requires caller and callee to have matching signatures
  = note: caller signature: `fn(for<'a> fn(&'a ()))`
  = note: callee signature: `fn(fn(&'static ()))`

Copy link
Member Author

@Kivooeo Kivooeo Aug 6, 2025

Choose a reason for hiding this comment

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

I did tcx.liberate_late_bound_regions it works but I did not found a better way to obtain DefId rather than do this

Update TailCallCkVisitor with adding a field that will contain def id

struct TailCallCkVisitor<'a, 'tcx> {
    ... // existed fields
    def: DefId,
}

And create it like this

    let mut visitor = TailCallCkVisitor {
        ... // eixsted fields
        def: def.to_def_id(),
    };

This might be a bad solution to pass DefId like this?

    self.report_arguments_mismatch(
        expr.span,
        self.tcx.liberate_late_bound_regions(self.def, self.caller_ty.fn_sig(self.tcx)),
        self.tcx.liberate_late_bound_regions(self.def, ty.fn_sig(self.tcx)),
    );

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey @compiler-errors, when you have a moment, could you take another look and let me know which approach you think works better?

ty.fn_sig(self.tcx).skip_binder(),
);
}

// FIXME(explicit_tail_calls): this currently fails for cases where opaques are used.
Expand Down
39 changes: 39 additions & 0 deletions tests/ui/explicit-tail-calls/caller-lifetime-presence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Regression test for: https://github.com/rust-lang/rust/issues/144957
//!
//! This test ensures that lifetime information is included in diagnostics.
//!
//! Specifically, it checks that the `become` call produces an error with lifetimes shown
//! in both caller and callee signatures.
//!
//! If the test fails:
//! - Lifetimes may be missing (fix the diagnostic), or
//! - The message format changed (update the test).

#![feature(explicit_tail_calls)]
#![allow(incomplete_features)]

fn foo<'a>(_: fn(&'a ())) {
become bar(dummy);
//~^ ERROR mismatched signatures
//~| NOTE `become` requires caller and callee to have matching signatures
//~| NOTE caller signature: `fn(fn(&'a ()))`
//~| NOTE callee signature: `fn(for<'a> fn(&'a ()))`
}

fn bar(_: fn(&())) {}

fn dummy(_: &()) {}

fn foo_(_: fn(&())) {
become bar1(dummy2);
//~^ ERROR mismatched signatures
//~| NOTE `become` requires caller and callee to have matching signatures
//~| NOTE caller signature: `fn(for<'a> fn(&'a ()))`
//~| NOTE callee signature: `fn(fn(&'a ()))`
}

fn bar1<'a>(_: fn(&'a ())) {}

fn dummy2(_: &()) {}

fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/explicit-tail-calls/caller-lifetime-presence.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: mismatched signatures
--> $DIR/caller-lifetime-presence.rs:16:5
|
LL | become bar(dummy);
| ^^^^^^^^^^^^^^^^^
|
= note: `become` requires caller and callee to have matching signatures
= note: caller signature: `fn(fn(&'a ()))`
= note: callee signature: `fn(for<'a> fn(&'a ()))`

error: mismatched signatures
--> $DIR/caller-lifetime-presence.rs:28:5
|
LL | become bar1(dummy2);
| ^^^^^^^^^^^^^^^^^^^
|
= note: `become` requires caller and callee to have matching signatures
= note: caller signature: `fn(for<'a> fn(&'a ()))`
= note: callee signature: `fn(fn(&'a ()))`

error: aborting due to 2 previous errors

Loading