From b17a3e2f2dd8f9121e13bbaf0428df6be5162062 Mon Sep 17 00:00:00 2001 From: lapla Date: Wed, 15 Apr 2026 00:33:36 +0900 Subject: [PATCH] Fix misleading "borrowed data escapes outside of function" diagnostic --- .../rustc_borrowck/src/diagnostics/region_errors.rs | 2 ++ .../fn-ptr-lifetime-mismatch-with-impl-trait-arg.rs | 8 ++++++++ ...fn-ptr-lifetime-mismatch-with-impl-trait-arg.stderr | 10 ++++++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.rs create mode 100644 tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.stderr diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index f6a20e41742b..934eadd215ad 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -686,6 +686,8 @@ fn report_escaping_data_error(&self, errci: &ErrorConstraintInfo<'tcx>) -> Diag< || (*category == ConstraintCategory::Assignment && self.regioncx.universal_regions().defining_ty.is_fn_def()) || self.regioncx.universal_regions().defining_ty.is_const() + || (fr_name_and_span.is_none() + && self.regioncx.universal_regions().defining_ty.is_fn_def()) { return self.report_general_error(errci); } diff --git a/tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.rs b/tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.rs new file mode 100644 index 000000000000..730e5cdc278f --- /dev/null +++ b/tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.rs @@ -0,0 +1,8 @@ +// Regression test for https://github.com/rust-lang/rust/issues/154350 + +fn func<'a>(f: impl FnOnce(fn(&'a i32)), x: fn(&'static i32)) { + f(x); + //~^ ERROR lifetime may not live long enough +} + +fn main() {} diff --git a/tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.stderr b/tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.stderr new file mode 100644 index 000000000000..92c613acdd83 --- /dev/null +++ b/tests/ui/borrowck/fn-ptr-lifetime-mismatch-with-impl-trait-arg.stderr @@ -0,0 +1,10 @@ +error: lifetime may not live long enough + --> $DIR/fn-ptr-lifetime-mismatch-with-impl-trait-arg.rs:4:5 + | +LL | fn func<'a>(f: impl FnOnce(fn(&'a i32)), x: fn(&'static i32)) { + | -- lifetime `'a` defined here +LL | f(x); + | ^^^^ argument requires that `'a` must outlive `'static` + +error: aborting due to 1 previous error +