From 120049fab4e293c19fa1c5a09d68f89bcd940b36 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 5 May 2024 03:42:47 +0000 Subject: Always constrain the return type in lifetime suggestion ``` error: lifetime may not live long enough --> f205.rs:8:16 | 7 | fn resolve_symbolic_reference(&self, reference: Option) -> Option { | - --------- has type `Option>` | | | let's call the lifetime of this reference `'2` 8 | return reference; | ^^^^^^^^^ method was supposed to return data with lifetime `'2` but it is returning data with lifetime `'1` | help: consider introducing a named lifetime parameter | 7 | fn resolve_symbolic_reference<'a>(&'a self, reference: Option>) -> Option> { | ++++ ++ ++++ ++++ ``` The correct suggestion would be ``` help: consider introducing a named lifetime parameter | 7 | fn resolve_symbolic_reference<'a>(&self, reference: Option>) -> Option> { | ++++ ++++ ++++ ``` but we are not doing the analysis to detect that yet. If we constrain `&'a self`, then the return type with a borrow will implicitly take its lifetime from `'a`, it is better to make it explicit in the suggestion, in case that `&self` *doesn't* need to be `'a`, but the return does. --- compiler/rustc_infer/src/errors/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'compiler') diff --git a/compiler/rustc_infer/src/errors/mod.rs b/compiler/rustc_infer/src/errors/mod.rs index 03947239896..4db79af10a5 100644 --- a/compiler/rustc_infer/src/errors/mod.rs +++ b/compiler/rustc_infer/src/errors/mod.rs @@ -450,6 +450,11 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> { }; visitor.visit_ty(self.ty_sub); visitor.visit_ty(self.ty_sup); + if let Some(fn_decl) = node.fn_decl() + && let hir::FnRetTy::Return(ty) = fn_decl.output + { + visitor.visit_ty(ty); + } if visitor.suggestions.is_empty() { return false; } -- cgit 1.4.1-3-g733a5