diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2023-11-15 16:56:41 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2023-11-20 23:44:37 +0000 |
| commit | 2a92d820c79e56be8191bdf64e8b2dc7c96210df (patch) | |
| tree | 0d0fe4dbdec15d9b0b0b93deedc7c2cf0121ee8a /compiler | |
| parent | dec7f00e158f04054320f63796fea1445ac18917 (diff) | |
| download | rust-2a92d820c79e56be8191bdf64e8b2dc7c96210df.tar.gz rust-2a92d820c79e56be8191bdf64e8b2dc7c96210df.zip | |
Suggest 'a when trait object assoc type has '_
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index ce981c333d8..984516baa18 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2971,9 +2971,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { // we identified that the return expression references only one argument, we // would suggest borrowing only that argument, and we'd skip the prior // "use `'static`" suggestion entirely. - if let [lt] = &lifetime_refs[..] && lt.kind == MissingLifetimeKind::Ampersand { - let pre = if let Some((kind, _span)) = - self.diagnostic_metadata.current_function + if let [lt] = &lifetime_refs[..] + && (lt.kind == MissingLifetimeKind::Ampersand + || lt.kind == MissingLifetimeKind::Underscore) + { + let pre = if lt.kind == MissingLifetimeKind::Ampersand + && let Some((kind, _span)) = + self.diagnostic_metadata.current_function && let FnKind::Fn(_, _, sig, _, _, _) = kind && !sig.decl.inputs.is_empty() && let sugg = sig @@ -3013,8 +3017,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { Applicability::MaybeIncorrect, ); "...or alternatively, you might want" - } else if let Some((kind, _span)) = - self.diagnostic_metadata.current_function + } else if (lt.kind == MissingLifetimeKind::Ampersand + || lt.kind == MissingLifetimeKind::Underscore) + && let Some((kind, _span)) = + self.diagnostic_metadata.current_function && let FnKind::Fn(_, _, sig, _, _, _) = kind && let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output && !sig.decl.inputs.is_empty() @@ -3059,7 +3065,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { err, None, |err, higher_ranked, span, message, intro_sugg| { - info!(?span, ?message, ?intro_sugg); err.multipart_suggestion_verbose( message, std::iter::once((span, intro_sugg)) @@ -3093,7 +3098,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { && segments[0].ident.name == sym::str { // Don't suggest `-> str`, suggest `-> String`. - sugg = vec![(lt.span.with_hi(ty.span.hi()), "String".to_string())]; + sugg = vec![ + (lt.span.with_hi(ty.span.hi()), "String".to_string()), + ]; } if let TyKind::Slice(inner_ty) = &ty.kind { // Don't suggest `-> [T]`, suggest `-> Vec<T>`. @@ -3104,11 +3111,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } } }; - err.multipart_suggestion_verbose( - format!("{pre} to return an owned value"), - sugg, - Applicability::MaybeIncorrect, - ); + if lt.kind == MissingLifetimeKind::Ampersand { + err.multipart_suggestion_verbose( + format!("{pre} to return an owned value"), + sugg, + Applicability::MaybeIncorrect, + ); + } } } |
