-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Tweak output of missing lifetime on associated type #135602
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
estebank
wants to merge
8
commits into
rust-lang:master
Choose a base branch
from
estebank:issue-135589
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+759
−148
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eb6d6f9
Add tests for some cases mentioned in #135589
estebank b4507bf
Detect case of missing lifetime in assoc type
estebank a6cb0b5
On unconstrained lifetime on `impl` block, suggest using it if there'…
estebank 2c7160b
Do not suggest introducing lifetime in impl assoc type
estebank 119ea42
Always point at trait assoc item when generics don't match
estebank 4756c5d
Look at the current `impl` before suggesting adding a lifetime
estebank a46faeb
Tweak wording in associated type with anon lifetime error
estebank b4682f4
Make "add lifetime" suggestion verbose
estebank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ use std::borrow::Cow; | |||||||||||||||||||||||||||
use std::collections::BTreeSet; | ||||||||||||||||||||||||||||
use std::collections::hash_map::Entry; | ||||||||||||||||||||||||||||
use std::mem::{replace, swap, take}; | ||||||||||||||||||||||||||||
use std::ops::ControlFlow; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
use rustc_ast::ptr::P; | ||||||||||||||||||||||||||||
use rustc_ast::visit::{ | ||||||||||||||||||||||||||||
|
@@ -20,20 +21,21 @@ use rustc_ast::*; | |||||||||||||||||||||||||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; | ||||||||||||||||||||||||||||
use rustc_errors::codes::*; | ||||||||||||||||||||||||||||
use rustc_errors::{ | ||||||||||||||||||||||||||||
Applicability, DiagArgValue, ErrorGuaranteed, IntoDiagArg, StashKey, Suggestions, | ||||||||||||||||||||||||||||
Applicability, Diag, DiagArgValue, ErrorGuaranteed, IntoDiagArg, MultiSpan, StashKey, | ||||||||||||||||||||||||||||
Suggestions, pluralize, | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
use rustc_hir::def::Namespace::{self, *}; | ||||||||||||||||||||||||||||
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS}; | ||||||||||||||||||||||||||||
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId}; | ||||||||||||||||||||||||||||
use rustc_hir::{MissingLifetimeKind, PrimTy, TraitCandidate}; | ||||||||||||||||||||||||||||
use rustc_middle::middle::resolve_bound_vars::Set1; | ||||||||||||||||||||||||||||
use rustc_middle::ty::DelegationFnSig; | ||||||||||||||||||||||||||||
use rustc_middle::ty::{AssocKind, DelegationFnSig}; | ||||||||||||||||||||||||||||
use rustc_middle::{bug, span_bug}; | ||||||||||||||||||||||||||||
use rustc_session::config::{CrateType, ResolveDocLinks}; | ||||||||||||||||||||||||||||
use rustc_session::lint::{self, BuiltinLintDiag}; | ||||||||||||||||||||||||||||
use rustc_session::parse::feature_err; | ||||||||||||||||||||||||||||
use rustc_span::source_map::{Spanned, respan}; | ||||||||||||||||||||||||||||
use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym}; | ||||||||||||||||||||||||||||
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol, SyntaxContext, kw, sym}; | ||||||||||||||||||||||||||||
use smallvec::{SmallVec, smallvec}; | ||||||||||||||||||||||||||||
use tracing::{debug, instrument, trace}; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -359,6 +361,7 @@ enum LifetimeBinderKind { | |||||||||||||||||||||||||||
Function, | ||||||||||||||||||||||||||||
Closure, | ||||||||||||||||||||||||||||
ImplBlock, | ||||||||||||||||||||||||||||
ImplAssocType, | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
impl LifetimeBinderKind { | ||||||||||||||||||||||||||||
|
@@ -369,6 +372,7 @@ impl LifetimeBinderKind { | |||||||||||||||||||||||||||
PolyTrait => "bound", | ||||||||||||||||||||||||||||
WhereBound => "bound", | ||||||||||||||||||||||||||||
Item | ConstItem => "item", | ||||||||||||||||||||||||||||
ImplAssocType => "associated type", | ||||||||||||||||||||||||||||
ImplBlock => "impl block", | ||||||||||||||||||||||||||||
Function => "function", | ||||||||||||||||||||||||||||
Closure => "closure", | ||||||||||||||||||||||||||||
|
@@ -684,6 +688,9 @@ struct DiagMetadata<'ast> { | |||||||||||||||||||||||||||
/// The current impl items (used to suggest). | ||||||||||||||||||||||||||||
current_impl_items: Option<&'ast [P<AssocItem>]>, | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
/// The current impl items (used to suggest). | ||||||||||||||||||||||||||||
current_impl_item: Option<&'ast AssocItem>, | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
/// When processing impl trait | ||||||||||||||||||||||||||||
currently_processing_impl_trait: Option<(TraitRef, Ty)>, | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -1870,9 +1877,34 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { | |||||||||||||||||||||||||||
ty: ty.span, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
self.r.dcx().emit_err(errors::AnonymousLivetimeNonGatReportError { | ||||||||||||||||||||||||||||
lifetime: lifetime.ident.span, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
let decl = if !trait_id.is_local() | ||||||||||||||||||||||||||||
&& let Some(assoc) = self.diag_metadata.current_impl_item | ||||||||||||||||||||||||||||
&& let AssocItemKind::Type(_) = assoc.kind | ||||||||||||||||||||||||||||
&& let assocs = self.r.tcx.associated_items(trait_id) | ||||||||||||||||||||||||||||
&& let Some(assoc) = assocs.find_by_name_and_kind( | ||||||||||||||||||||||||||||
self.r.tcx, | ||||||||||||||||||||||||||||
assoc.ident, | ||||||||||||||||||||||||||||
AssocKind::Type, | ||||||||||||||||||||||||||||
trait_id, | ||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||
let mut decl: MultiSpan = | ||||||||||||||||||||||||||||
self.r.tcx.def_span(assoc.def_id).into(); | ||||||||||||||||||||||||||||
decl.push_span_label( | ||||||||||||||||||||||||||||
self.r.tcx.def_span(trait_id), | ||||||||||||||||||||||||||||
String::new(), | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
decl | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
DUMMY_SP.into() | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
let mut err = self.r.dcx().create_err( | ||||||||||||||||||||||||||||
errors::AnonymousLivetimeNonGatReportError { | ||||||||||||||||||||||||||||
lifetime: lifetime.ident.span, | ||||||||||||||||||||||||||||
decl, | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
self.point_at_impl_lifetimes(&mut err, i, lifetime.ident.span); | ||||||||||||||||||||||||||||
err.emit(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
self.r.dcx().emit_err(errors::ElidedAnonymousLivetimeReportError { | ||||||||||||||||||||||||||||
|
@@ -1909,6 +1941,99 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { | |||||||||||||||||||||||||||
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
fn point_at_impl_lifetimes(&mut self, err: &mut Diag<'_>, i: usize, lifetime: Span) { | ||||||||||||||||||||||||||||
let Some((rib, span)) = self.lifetime_ribs[..i] | ||||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||||
.rev() | ||||||||||||||||||||||||||||
.skip(1) | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the |
||||||||||||||||||||||||||||
.filter_map(|rib| match rib.kind { | ||||||||||||||||||||||||||||
LifetimeRibKind::Generics { span, kind: LifetimeBinderKind::ImplBlock, .. } => { | ||||||||||||||||||||||||||||
Some((rib, span)) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
_ => None, | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
.next() | ||||||||||||||||||||||||||||
Comment on lines
+1949
to
+1955
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||||||||||||||||||||||||
else { | ||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
if !rib.bindings.is_empty() { | ||||||||||||||||||||||||||||
err.span_label( | ||||||||||||||||||||||||||||
span, | ||||||||||||||||||||||||||||
format!( | ||||||||||||||||||||||||||||
"there {} named lifetime{} specified on the impl block you could use", | ||||||||||||||||||||||||||||
if rib.bindings.len() == 1 { "is a" } else { "are" }, | ||||||||||||||||||||||||||||
pluralize!(rib.bindings.len()), | ||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
if rib.bindings.len() == 1 { | ||||||||||||||||||||||||||||
err.span_suggestion_verbose( | ||||||||||||||||||||||||||||
lifetime.shrink_to_hi(), | ||||||||||||||||||||||||||||
"consider using the lifetime from the impl block", | ||||||||||||||||||||||||||||
format!("{} ", rib.bindings.keys().next().unwrap()), | ||||||||||||||||||||||||||||
Applicability::MaybeIncorrect, | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
struct AnonRefFinder; | ||||||||||||||||||||||||||||
impl<'ast> Visitor<'ast> for AnonRefFinder { | ||||||||||||||||||||||||||||
type Result = ControlFlow<Span>; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
fn visit_ty(&mut self, ty: &'ast ast::Ty) -> Self::Result { | ||||||||||||||||||||||||||||
if let ast::TyKind::Ref(None, mut_ty) = &ty.kind { | ||||||||||||||||||||||||||||
return ControlFlow::Break(mut_ty.ty.span.shrink_to_lo()); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
visit::walk_ty(self, ty) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
fn visit_lifetime( | ||||||||||||||||||||||||||||
&mut self, | ||||||||||||||||||||||||||||
lt: &'ast ast::Lifetime, | ||||||||||||||||||||||||||||
_cx: visit::LifetimeCtxt, | ||||||||||||||||||||||||||||
) -> Self::Result { | ||||||||||||||||||||||||||||
if lt.ident.name == kw::UnderscoreLifetime { | ||||||||||||||||||||||||||||
return ControlFlow::Break(lt.ident.span); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
visit::walk_lifetime(self, lt) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if let Some(ty) = &self.diag_metadata.current_self_type | ||||||||||||||||||||||||||||
&& let ControlFlow::Break(sp) = AnonRefFinder.visit_ty(ty) | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
err.multipart_suggestion_verbose( | ||||||||||||||||||||||||||||
"add a lifetime to the impl block and use it in the self type and associated \ | ||||||||||||||||||||||||||||
type", | ||||||||||||||||||||||||||||
vec![ | ||||||||||||||||||||||||||||
(span, "<'a>".to_string()), | ||||||||||||||||||||||||||||
(sp, "'a ".to_string()), | ||||||||||||||||||||||||||||
(lifetime.shrink_to_hi(), "'a ".to_string()), | ||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||
Applicability::MaybeIncorrect, | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} else if let Some(item) = &self.diag_metadata.current_item | ||||||||||||||||||||||||||||
&& let ItemKind::Impl(impl_) = &item.kind | ||||||||||||||||||||||||||||
&& let Some(of_trait) = &impl_.of_trait | ||||||||||||||||||||||||||||
&& let ControlFlow::Break(sp) = AnonRefFinder.visit_trait_ref(of_trait) | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
err.multipart_suggestion_verbose( | ||||||||||||||||||||||||||||
"add a lifetime to the impl block and use it in the trait and associated type", | ||||||||||||||||||||||||||||
vec![ | ||||||||||||||||||||||||||||
(span, "<'a>".to_string()), | ||||||||||||||||||||||||||||
(sp, "'a".to_string()), | ||||||||||||||||||||||||||||
(lifetime.shrink_to_hi(), "'a ".to_string()), | ||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||
Applicability::MaybeIncorrect, | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
err.span_label( | ||||||||||||||||||||||||||||
span, | ||||||||||||||||||||||||||||
"you could add a lifetime on the impl block, if the trait or the self type \ | ||||||||||||||||||||||||||||
could have one", | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#[instrument(level = "debug", skip(self))] | ||||||||||||||||||||||||||||
fn resolve_elided_lifetime(&mut self, anchor_id: NodeId, span: Span) { | ||||||||||||||||||||||||||||
let id = self.r.next_node_id(); | ||||||||||||||||||||||||||||
|
@@ -3265,6 +3390,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { | |||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||
use crate::ResolutionError::*; | ||||||||||||||||||||||||||||
self.resolve_doc_links(&item.attrs, MaybeExported::ImplItem(trait_id.ok_or(&item.vis))); | ||||||||||||||||||||||||||||
let prev = self.diag_metadata.current_impl_item.take(); | ||||||||||||||||||||||||||||
self.diag_metadata.current_impl_item = Some(&item); | ||||||||||||||||||||||||||||
match &item.kind { | ||||||||||||||||||||||||||||
AssocItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => { | ||||||||||||||||||||||||||||
debug!("resolve_implementation AssocItemKind::Const"); | ||||||||||||||||||||||||||||
|
@@ -3349,7 +3476,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { | |||||||||||||||||||||||||||
LifetimeRibKind::Generics { | ||||||||||||||||||||||||||||
binder: item.id, | ||||||||||||||||||||||||||||
span: generics.span, | ||||||||||||||||||||||||||||
kind: LifetimeBinderKind::Item, | ||||||||||||||||||||||||||||
kind: LifetimeBinderKind::ImplAssocType, | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
|this| { | ||||||||||||||||||||||||||||
this.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| { | ||||||||||||||||||||||||||||
|
@@ -3400,6 +3527,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { | |||||||||||||||||||||||||||
panic!("unexpanded macro in resolve!") | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
self.diag_metadata.current_impl_item = prev; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
fn check_trait_item<F>( | ||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment to the
Item
variant explaining which items it covers? Is it only trait assoc types now? If so better rename it.