Rename DiagnosticMetadata as DiagMetadata.

This commit is contained in:
Nicholas Nethercote 2024-02-29 17:24:21 +11:00
parent f8429390ec
commit 5cce28725f
2 changed files with 86 additions and 92 deletions

View File

@ -581,7 +581,7 @@ impl MaybeExported<'_> {
} }
#[derive(Default)] #[derive(Default)]
struct DiagnosticMetadata<'ast> { struct DiagMetadata<'ast> {
/// The current trait's associated items' ident, used for diagnostic suggestions. /// The current trait's associated items' ident, used for diagnostic suggestions.
current_trait_assoc_items: Option<&'ast [P<AssocItem>]>, current_trait_assoc_items: Option<&'ast [P<AssocItem>]>,
@ -674,7 +674,7 @@ struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
current_trait_ref: Option<(Module<'a>, TraitRef)>, current_trait_ref: Option<(Module<'a>, TraitRef)>,
/// Fields used to add information to diagnostic errors. /// Fields used to add information to diagnostic errors.
diagnostic_metadata: Box<DiagnosticMetadata<'ast>>, diag_metadata: Box<DiagMetadata<'ast>>,
/// State used to know whether to ignore resolution errors for function bodies. /// State used to know whether to ignore resolution errors for function bodies.
/// ///
@ -694,12 +694,12 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
// as they do not correspond to actual code. // as they do not correspond to actual code.
} }
fn visit_item(&mut self, item: &'ast Item) { fn visit_item(&mut self, item: &'ast Item) {
let prev = replace(&mut self.diagnostic_metadata.current_item, Some(item)); let prev = replace(&mut self.diag_metadata.current_item, Some(item));
// Always report errors in items we just entered. // Always report errors in items we just entered.
let old_ignore = replace(&mut self.in_func_body, false); let old_ignore = replace(&mut self.in_func_body, false);
self.with_lifetime_rib(LifetimeRibKind::Item, |this| this.resolve_item(item)); self.with_lifetime_rib(LifetimeRibKind::Item, |this| this.resolve_item(item));
self.in_func_body = old_ignore; self.in_func_body = old_ignore;
self.diagnostic_metadata.current_item = prev; self.diag_metadata.current_item = prev;
} }
fn visit_arm(&mut self, arm: &'ast Arm) { fn visit_arm(&mut self, arm: &'ast Arm) {
self.resolve_arm(arm); self.resolve_arm(arm);
@ -716,10 +716,10 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
self.resolve_expr(expr, None); self.resolve_expr(expr, None);
} }
fn visit_pat(&mut self, p: &'ast Pat) { fn visit_pat(&mut self, p: &'ast Pat) {
let prev = self.diagnostic_metadata.current_pat; let prev = self.diag_metadata.current_pat;
self.diagnostic_metadata.current_pat = Some(p); self.diag_metadata.current_pat = Some(p);
visit::walk_pat(self, p); visit::walk_pat(self, p);
self.diagnostic_metadata.current_pat = prev; self.diag_metadata.current_pat = prev;
} }
fn visit_local(&mut self, local: &'ast Local) { fn visit_local(&mut self, local: &'ast Local) {
let local_spans = match local.pat.kind { let local_spans = match local.pat.kind {
@ -731,13 +731,13 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
local.kind.init().map(|init| init.span), local.kind.init().map(|init| init.span),
)), )),
}; };
let original = replace(&mut self.diagnostic_metadata.current_let_binding, local_spans); let original = replace(&mut self.diag_metadata.current_let_binding, local_spans);
self.resolve_local(local); self.resolve_local(local);
self.diagnostic_metadata.current_let_binding = original; self.diag_metadata.current_let_binding = original;
} }
fn visit_ty(&mut self, ty: &'ast Ty) { fn visit_ty(&mut self, ty: &'ast Ty) {
let prev = self.diagnostic_metadata.current_trait_object; let prev = self.diag_metadata.current_trait_object;
let prev_ty = self.diagnostic_metadata.current_type_path; let prev_ty = self.diag_metadata.current_type_path;
match &ty.kind { match &ty.kind {
TyKind::Ref(None, _) => { TyKind::Ref(None, _) => {
// Elided lifetime in reference: we resolve as if there was some lifetime `'_` with // Elided lifetime in reference: we resolve as if there was some lifetime `'_` with
@ -748,7 +748,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
visit::walk_ty(self, ty); visit::walk_ty(self, ty);
} }
TyKind::Path(qself, path) => { TyKind::Path(qself, path) => {
self.diagnostic_metadata.current_type_path = Some(ty); self.diag_metadata.current_type_path = Some(ty);
self.smart_resolve_path(ty.id, qself, path, PathSource::Type); self.smart_resolve_path(ty.id, qself, path, PathSource::Type);
// Check whether we should interpret this as a bare trait object. // Check whether we should interpret this as a bare trait object.
@ -795,7 +795,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
self.lifetime_elision_candidates = candidates; self.lifetime_elision_candidates = candidates;
} }
TyKind::TraitObject(bounds, ..) => { TyKind::TraitObject(bounds, ..) => {
self.diagnostic_metadata.current_trait_object = Some(&bounds[..]); self.diag_metadata.current_trait_object = Some(&bounds[..]);
visit::walk_ty(self, ty) visit::walk_ty(self, ty)
} }
TyKind::BareFn(bare_fn) => { TyKind::BareFn(bare_fn) => {
@ -842,8 +842,8 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
} }
_ => visit::walk_ty(self, ty), _ => visit::walk_ty(self, ty),
} }
self.diagnostic_metadata.current_trait_object = prev; self.diag_metadata.current_trait_object = prev;
self.diagnostic_metadata.current_type_path = prev_ty; self.diag_metadata.current_type_path = prev_ty;
} }
fn visit_poly_trait_ref(&mut self, tref: &'ast PolyTraitRef) { fn visit_poly_trait_ref(&mut self, tref: &'ast PolyTraitRef) {
let span = tref.span.shrink_to_lo().to(tref.trait_ref.path.span.shrink_to_lo()); let span = tref.span.shrink_to_lo().to(tref.trait_ref.path.span.shrink_to_lo());
@ -906,7 +906,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
} }
} }
fn visit_fn(&mut self, fn_kind: FnKind<'ast>, sp: Span, fn_id: NodeId) { fn visit_fn(&mut self, fn_kind: FnKind<'ast>, sp: Span, fn_id: NodeId) {
let previous_value = self.diagnostic_metadata.current_function; let previous_value = self.diag_metadata.current_function;
match fn_kind { match fn_kind {
// Bail if the function is foreign, and thus cannot validly have // Bail if the function is foreign, and thus cannot validly have
// a body, or if there's no body for some other reason. // a body, or if there's no body for some other reason.
@ -939,7 +939,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
return; return;
} }
FnKind::Fn(..) => { FnKind::Fn(..) => {
self.diagnostic_metadata.current_function = Some((fn_kind, sp)); self.diag_metadata.current_function = Some((fn_kind, sp));
} }
// Do not update `current_function` for closures: it suggests `self` parameters. // Do not update `current_function` for closures: it suggests `self` parameters.
FnKind::Closure(..) => {} FnKind::Closure(..) => {}
@ -1040,17 +1040,14 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
} }
}) })
}); });
self.diagnostic_metadata.current_function = previous_value; self.diag_metadata.current_function = previous_value;
} }
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::LifetimeCtxt) { fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::LifetimeCtxt) {
self.resolve_lifetime(lifetime, use_ctxt) self.resolve_lifetime(lifetime, use_ctxt)
} }
fn visit_generics(&mut self, generics: &'ast Generics) { fn visit_generics(&mut self, generics: &'ast Generics) {
self.visit_generic_params( self.visit_generic_params(&generics.params, self.diag_metadata.current_self_item.is_some());
&generics.params,
self.diagnostic_metadata.current_self_item.is_some(),
);
for p in &generics.where_clause.predicates { for p in &generics.where_clause.predicates {
self.visit_where_predicate(p); self.visit_where_predicate(p);
} }
@ -1062,7 +1059,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
ClosureBinder::For { generic_params, .. } => { ClosureBinder::For { generic_params, .. } => {
self.visit_generic_params( self.visit_generic_params(
generic_params, generic_params,
self.diagnostic_metadata.current_self_item.is_some(), self.diag_metadata.current_self_item.is_some(),
); );
} }
} }
@ -1070,7 +1067,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
fn visit_generic_arg(&mut self, arg: &'ast GenericArg) { fn visit_generic_arg(&mut self, arg: &'ast GenericArg) {
debug!("visit_generic_arg({:?})", arg); debug!("visit_generic_arg({:?})", arg);
let prev = replace(&mut self.diagnostic_metadata.currently_processing_generic_args, true); let prev = replace(&mut self.diag_metadata.currently_processing_generic_args, true);
match arg { match arg {
GenericArg::Type(ref ty) => { GenericArg::Type(ref ty) => {
// We parse const arguments as path types as we cannot distinguish them during // We parse const arguments as path types as we cannot distinguish them during
@ -1101,7 +1098,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
}, },
); );
self.diagnostic_metadata.currently_processing_generic_args = prev; self.diag_metadata.currently_processing_generic_args = prev;
return; return;
} }
} }
@ -1114,7 +1111,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
self.resolve_anon_const(ct, AnonConstKind::ConstArg(IsRepeatExpr::No)) self.resolve_anon_const(ct, AnonConstKind::ConstArg(IsRepeatExpr::No))
} }
} }
self.diagnostic_metadata.currently_processing_generic_args = prev; self.diag_metadata.currently_processing_generic_args = prev;
} }
fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) { fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {
@ -1192,8 +1189,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
fn visit_where_predicate(&mut self, p: &'ast WherePredicate) { fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
debug!("visit_where_predicate {:?}", p); debug!("visit_where_predicate {:?}", p);
let previous_value = let previous_value = replace(&mut self.diag_metadata.current_where_predicate, Some(p));
replace(&mut self.diagnostic_metadata.current_where_predicate, Some(p));
self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| { self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
if let WherePredicate::BoundPredicate(WhereBoundPredicate { if let WherePredicate::BoundPredicate(WhereBoundPredicate {
ref bounded_ty, ref bounded_ty,
@ -1224,7 +1220,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
visit::walk_where_predicate(this, p); visit::walk_where_predicate(this, p);
} }
}); });
self.diagnostic_metadata.current_where_predicate = previous_value; self.diag_metadata.current_where_predicate = previous_value;
} }
fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) { fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) {
@ -1297,7 +1293,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
lifetime_ribs: Vec::new(), lifetime_ribs: Vec::new(),
lifetime_elision_candidates: None, lifetime_elision_candidates: None,
current_trait_ref: None, current_trait_ref: None,
diagnostic_metadata: Default::default(), diag_metadata: Default::default(),
// errors at module scope should always be reported // errors at module scope should always be reported
in_func_body: false, in_func_body: false,
lifetime_uses: Default::default(), lifetime_uses: Default::default(),
@ -1707,7 +1703,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
return; return;
} }
LifetimeRibKind::ElisionFailure => { LifetimeRibKind::ElisionFailure => {
self.diagnostic_metadata.current_elision_failures.push(missing_lifetime); self.diag_metadata.current_elision_failures.push(missing_lifetime);
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate); self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
return; return;
} }
@ -1911,7 +1907,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
break; break;
} }
LifetimeRibKind::ElisionFailure => { LifetimeRibKind::ElisionFailure => {
self.diagnostic_metadata.current_elision_failures.push(missing_lifetime); self.diag_metadata.current_elision_failures.push(missing_lifetime);
for id in node_ids { for id in node_ids {
self.record_lifetime_res( self.record_lifetime_res(
id, id,
@ -2003,7 +1999,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
let elision_lifetime = self.resolve_fn_params(has_self, inputs); let elision_lifetime = self.resolve_fn_params(has_self, inputs);
debug!(?elision_lifetime); debug!(?elision_lifetime);
let outer_failures = take(&mut self.diagnostic_metadata.current_elision_failures); let outer_failures = take(&mut self.diag_metadata.current_elision_failures);
let output_rib = if let Ok(res) = elision_lifetime.as_ref() { let output_rib = if let Ok(res) = elision_lifetime.as_ref() {
self.r.lifetime_elision_allowed.insert(fn_id); self.r.lifetime_elision_allowed.insert(fn_id);
LifetimeRibKind::Elided(*res) LifetimeRibKind::Elided(*res)
@ -2012,7 +2008,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
}; };
self.with_lifetime_rib(output_rib, |this| visit::walk_fn_ret_ty(this, output_ty)); self.with_lifetime_rib(output_rib, |this| visit::walk_fn_ret_ty(this, output_ty));
let elision_failures = let elision_failures =
replace(&mut self.diagnostic_metadata.current_elision_failures, outer_failures); replace(&mut self.diag_metadata.current_elision_failures, outer_failures);
if !elision_failures.is_empty() { if !elision_failures.is_empty() {
let Err(failure_info) = elision_lifetime else { bug!() }; let Err(failure_info) = elision_lifetime else { bug!() };
self.report_missing_lifetime_specifiers(elision_failures, Some(failure_info)); self.report_missing_lifetime_specifiers(elision_failures, Some(failure_info));
@ -2187,7 +2183,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
} }
let impl_self = self let impl_self = self
.diagnostic_metadata .diag_metadata
.current_self_type .current_self_type
.as_ref() .as_ref()
.and_then(|ty| { .and_then(|ty| {
@ -2385,7 +2381,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
items: ref impl_items, items: ref impl_items,
.. ..
}) => { }) => {
self.diagnostic_metadata.current_impl_items = Some(impl_items); self.diag_metadata.current_impl_items = Some(impl_items);
self.resolve_implementation( self.resolve_implementation(
&item.attrs, &item.attrs,
generics, generics,
@ -2394,7 +2390,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
item.id, item.id,
impl_items, impl_items,
); );
self.diagnostic_metadata.current_impl_items = None; self.diag_metadata.current_impl_items = None;
} }
ItemKind::Trait(box Trait { ref generics, ref bounds, ref items, .. }) => { ItemKind::Trait(box Trait { ref generics, ref bounds, ref items, .. }) => {
@ -2744,24 +2740,23 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
fn with_current_self_type<T>(&mut self, self_type: &Ty, f: impl FnOnce(&mut Self) -> T) -> T { fn with_current_self_type<T>(&mut self, self_type: &Ty, f: impl FnOnce(&mut Self) -> T) -> T {
// Handle nested impls (inside fn bodies) // Handle nested impls (inside fn bodies)
let previous_value = let previous_value =
replace(&mut self.diagnostic_metadata.current_self_type, Some(self_type.clone())); replace(&mut self.diag_metadata.current_self_type, Some(self_type.clone()));
let result = f(self); let result = f(self);
self.diagnostic_metadata.current_self_type = previous_value; self.diag_metadata.current_self_type = previous_value;
result result
} }
fn with_current_self_item<T>(&mut self, self_item: &Item, f: impl FnOnce(&mut Self) -> T) -> T { fn with_current_self_item<T>(&mut self, self_item: &Item, f: impl FnOnce(&mut Self) -> T) -> T {
let previous_value = let previous_value = replace(&mut self.diag_metadata.current_self_item, Some(self_item.id));
replace(&mut self.diagnostic_metadata.current_self_item, Some(self_item.id));
let result = f(self); let result = f(self);
self.diagnostic_metadata.current_self_item = previous_value; self.diag_metadata.current_self_item = previous_value;
result result
} }
/// When evaluating a `trait` use its associated types' idents for suggestions in E0412. /// When evaluating a `trait` use its associated types' idents for suggestions in E0412.
fn resolve_trait_items(&mut self, trait_items: &'ast [P<AssocItem>]) { fn resolve_trait_items(&mut self, trait_items: &'ast [P<AssocItem>]) {
let trait_assoc_items = let trait_assoc_items =
replace(&mut self.diagnostic_metadata.current_trait_assoc_items, Some(trait_items)); replace(&mut self.diag_metadata.current_trait_assoc_items, Some(trait_items));
let walk_assoc_item = let walk_assoc_item =
|this: &mut Self, generics: &Generics, kind, item: &'ast AssocItem| { |this: &mut Self, generics: &Generics, kind, item: &'ast AssocItem| {
@ -2818,7 +2813,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
}; };
} }
self.diagnostic_metadata.current_trait_assoc_items = trait_assoc_items; self.diag_metadata.current_trait_assoc_items = trait_assoc_items;
} }
/// This is called to resolve a trait reference from an `impl` (i.e., `impl Trait for Foo`). /// This is called to resolve a trait reference from an `impl` (i.e., `impl Trait for Foo`).
@ -2832,7 +2827,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
let mut new_id = None; let mut new_id = None;
if let Some(trait_ref) = opt_trait_ref { if let Some(trait_ref) = opt_trait_ref {
let path: Vec<_> = Segment::from_path(&trait_ref.path); let path: Vec<_> = Segment::from_path(&trait_ref.path);
self.diagnostic_metadata.currently_processing_impl_trait = self.diag_metadata.currently_processing_impl_trait =
Some((trait_ref.clone(), self_type.clone())); Some((trait_ref.clone(), self_type.clone()));
let res = self.smart_resolve_path_fragment( let res = self.smart_resolve_path_fragment(
&None, &None,
@ -2841,7 +2836,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
Finalize::new(trait_ref.ref_id, trait_ref.path.span), Finalize::new(trait_ref.ref_id, trait_ref.path.span),
RecordPartialRes::Yes, RecordPartialRes::Yes,
); );
self.diagnostic_metadata.currently_processing_impl_trait = None; self.diag_metadata.currently_processing_impl_trait = None;
if let Some(def_id) = res.expect_full_res().opt_def_id() { if let Some(def_id) = res.expect_full_res().opt_def_id() {
new_id = Some(def_id); new_id = Some(def_id);
new_val = Some((self.r.expect_module(def_id), trait_ref.clone())); new_val = Some((self.r.expect_module(def_id), trait_ref.clone()));
@ -3737,7 +3732,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
let def_id = this.parent_scope.module.nearest_parent_mod(); let def_id = this.parent_scope.module.nearest_parent_mod();
let instead = res.is_some(); let instead = res.is_some();
let suggestion = if let Some((start, end)) = this.diagnostic_metadata.in_range let suggestion = if let Some((start, end)) = this.diag_metadata.in_range
&& path[0].ident.span.lo() == end.span.lo() && path[0].ident.span.lo() == end.span.lo()
{ {
let mut sugg = "."; let mut sugg = ".";
@ -3890,7 +3885,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
) { ) {
Ok(Some(partial_res)) if let Some(res) = partial_res.full_res() => { Ok(Some(partial_res)) if let Some(res) = partial_res.full_res() => {
// if we also have an associated type that matches the ident, stash a suggestion // if we also have an associated type that matches the ident, stash a suggestion
if let Some(items) = self.diagnostic_metadata.current_trait_assoc_items if let Some(items) = self.diag_metadata.current_trait_assoc_items
&& let [Segment { ident, .. }] = path && let [Segment { ident, .. }] = path
&& items.iter().any(|item| { && items.iter().any(|item| {
item.ident == *ident && matches!(item.kind, AssocItemKind::Type(_)) item.ident == *ident && matches!(item.kind, AssocItemKind::Type(_))
@ -4189,7 +4184,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
fn with_resolved_label(&mut self, label: Option<Label>, id: NodeId, f: impl FnOnce(&mut Self)) { fn with_resolved_label(&mut self, label: Option<Label>, id: NodeId, f: impl FnOnce(&mut Self)) {
if let Some(label) = label { if let Some(label) = label {
if label.ident.as_str().as_bytes()[1] != b'_' { if label.ident.as_str().as_bytes()[1] != b'_' {
self.diagnostic_metadata.unused_labels.insert(id, label.ident.span); self.diag_metadata.unused_labels.insert(id, label.ident.span);
} }
if let Ok((_, orig_span)) = self.resolve_label(label.ident) { if let Ok((_, orig_span)) = self.resolve_label(label.ident) {
@ -4226,12 +4221,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
self.ribs[ValueNS].push(Rib::new(RibKind::Normal)); self.ribs[ValueNS].push(Rib::new(RibKind::Normal));
} }
let prev = self.diagnostic_metadata.current_block_could_be_bare_struct_literal.take(); let prev = self.diag_metadata.current_block_could_be_bare_struct_literal.take();
if let (true, [Stmt { kind: StmtKind::Expr(expr), .. }]) = if let (true, [Stmt { kind: StmtKind::Expr(expr), .. }]) =
(block.could_be_bare_literal, &block.stmts[..]) (block.could_be_bare_literal, &block.stmts[..])
&& let ExprKind::Type(..) = expr.kind && let ExprKind::Type(..) = expr.kind
{ {
self.diagnostic_metadata.current_block_could_be_bare_struct_literal = Some(block.span); self.diag_metadata.current_block_could_be_bare_struct_literal = Some(block.span);
} }
// Descend into the block. // Descend into the block.
for stmt in &block.stmts { for stmt in &block.stmts {
@ -4246,7 +4241,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
self.visit_stmt(stmt); self.visit_stmt(stmt);
} }
self.diagnostic_metadata.current_block_could_be_bare_struct_literal = prev; self.diag_metadata.current_block_could_be_bare_struct_literal = prev;
// Move back up. // Move back up.
self.parent_scope.module = orig_module; self.parent_scope.module = orig_module;
@ -4353,7 +4348,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
Ok((node_id, _)) => { Ok((node_id, _)) => {
// Since this res is a label, it is never read. // Since this res is a label, it is never read.
self.r.label_res_map.insert(expr.id, node_id); self.r.label_res_map.insert(expr.id, node_id);
self.diagnostic_metadata.unused_labels.remove(&node_id); self.diag_metadata.unused_labels.remove(&node_id);
} }
Err(error) => { Err(error) => {
self.report_error(label.ident.span, error); self.report_error(label.ident.span, error);
@ -4377,9 +4372,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
ExprKind::If(ref cond, ref then, ref opt_else) => { ExprKind::If(ref cond, ref then, ref opt_else) => {
self.with_rib(ValueNS, RibKind::Normal, |this| { self.with_rib(ValueNS, RibKind::Normal, |this| {
let old = this.diagnostic_metadata.in_if_condition.replace(cond); let old = this.diag_metadata.in_if_condition.replace(cond);
this.visit_expr(cond); this.visit_expr(cond);
this.diagnostic_metadata.in_if_condition = old; this.diag_metadata.in_if_condition = old;
this.visit_block(then); this.visit_block(then);
}); });
if let Some(expr) = opt_else { if let Some(expr) = opt_else {
@ -4394,9 +4389,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
ExprKind::While(ref cond, ref block, label) => { ExprKind::While(ref cond, ref block, label) => {
self.with_resolved_label(label, expr.id, |this| { self.with_resolved_label(label, expr.id, |this| {
this.with_rib(ValueNS, RibKind::Normal, |this| { this.with_rib(ValueNS, RibKind::Normal, |this| {
let old = this.diagnostic_metadata.in_if_condition.replace(cond); let old = this.diag_metadata.in_if_condition.replace(cond);
this.visit_expr(cond); this.visit_expr(cond);
this.diagnostic_metadata.in_if_condition = old; this.diag_metadata.in_if_condition = old;
this.visit_block(block); this.visit_block(block);
}) })
}); });
@ -4476,20 +4471,20 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
self.visit_expr(idx); self.visit_expr(idx);
} }
ExprKind::Assign(ref lhs, ref rhs, _) => { ExprKind::Assign(ref lhs, ref rhs, _) => {
if !self.diagnostic_metadata.is_assign_rhs { if !self.diag_metadata.is_assign_rhs {
self.diagnostic_metadata.in_assignment = Some(expr); self.diag_metadata.in_assignment = Some(expr);
} }
self.visit_expr(lhs); self.visit_expr(lhs);
self.diagnostic_metadata.is_assign_rhs = true; self.diag_metadata.is_assign_rhs = true;
self.diagnostic_metadata.in_assignment = None; self.diag_metadata.in_assignment = None;
self.visit_expr(rhs); self.visit_expr(rhs);
self.diagnostic_metadata.is_assign_rhs = false; self.diag_metadata.is_assign_rhs = false;
} }
ExprKind::Range(Some(ref start), Some(ref end), RangeLimits::HalfOpen) => { ExprKind::Range(Some(ref start), Some(ref end), RangeLimits::HalfOpen) => {
self.diagnostic_metadata.in_range = Some((start, end)); self.diag_metadata.in_range = Some((start, end));
self.resolve_expr(start, Some(expr)); self.resolve_expr(start, Some(expr));
self.resolve_expr(end, Some(expr)); self.resolve_expr(end, Some(expr));
self.diagnostic_metadata.in_range = None; self.diag_metadata.in_range = None;
} }
_ => { _ => {
visit::walk_expr(self, expr); visit::walk_expr(self, expr);
@ -4737,7 +4732,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let mut late_resolution_visitor = LateResolutionVisitor::new(self); let mut late_resolution_visitor = LateResolutionVisitor::new(self);
late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID)); late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID));
visit::walk_crate(&mut late_resolution_visitor, krate); visit::walk_crate(&mut late_resolution_visitor, krate);
for (id, span) in late_resolution_visitor.diagnostic_metadata.unused_labels.iter() { for (id, span) in late_resolution_visitor.diag_metadata.unused_labels.iter() {
self.lint_buffer.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label"); self.lint_buffer.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label");
} }
} }

View File

@ -224,13 +224,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
let item_ident = path.last().unwrap().ident; let item_ident = path.last().unwrap().ident;
let item_span = item_ident.span; let item_span = item_ident.span;
let (mod_prefix, mod_str, module, suggestion) = if path.len() == 1 { let (mod_prefix, mod_str, module, suggestion) = if path.len() == 1 {
debug!(?self.diagnostic_metadata.current_impl_items); debug!(?self.diag_metadata.current_impl_items);
debug!(?self.diagnostic_metadata.current_function); debug!(?self.diag_metadata.current_function);
let suggestion = if self.current_trait_ref.is_none() let suggestion = if self.current_trait_ref.is_none()
&& let Some((fn_kind, _)) = self.diagnostic_metadata.current_function && let Some((fn_kind, _)) = self.diag_metadata.current_function
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt() && let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
&& let FnKind::Fn(_, _, sig, ..) = fn_kind && let FnKind::Fn(_, _, sig, ..) = fn_kind
&& let Some(items) = self.diagnostic_metadata.current_impl_items && let Some(items) = self.diag_metadata.current_impl_items
&& let Some(item) = items.iter().find(|i| { && let Some(item) = items.iter().find(|i| {
i.ident.name == item_str.name i.ident.name == item_str.name
// Don't suggest if the item is in Fn signature arguments (#112590). // Don't suggest if the item is in Fn signature arguments (#112590).
@ -501,7 +501,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
err: &mut Diag<'_>, err: &mut Diag<'_>,
base_error: &BaseError, base_error: &BaseError,
) { ) {
let Some(ty) = self.diagnostic_metadata.current_type_path else { let Some(ty) = self.diag_metadata.current_type_path else {
return; return;
}; };
let TyKind::Path(_, path) = &ty.kind else { let TyKind::Path(_, path) = &ty.kind else {
@ -555,7 +555,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
); );
if !self.self_value_is_available(path[0].ident.span) { if !self.self_value_is_available(path[0].ident.span) {
if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) = if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) =
&self.diagnostic_metadata.current_function &self.diag_metadata.current_function
{ {
let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) { let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) {
(param.span.shrink_to_lo(), "&self, ") (param.span.shrink_to_lo(), "&self, ")
@ -805,7 +805,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
) = (source, res, is_macro) ) = (source, res, is_macro)
{ {
if let Some(bounds @ [first_bound, .., last_bound]) = if let Some(bounds @ [first_bound, .., last_bound]) =
self.diagnostic_metadata.current_trait_object self.diag_metadata.current_trait_object
{ {
fallback = true; fallback = true;
let spans: Vec<Span> = bounds let spans: Vec<Span> = bounds
@ -880,7 +880,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
let typo_sugg = typo_sugg.to_opt_suggestion(); let typo_sugg = typo_sugg.to_opt_suggestion();
if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) { if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
fallback = true; fallback = true;
match self.diagnostic_metadata.current_let_binding { match self.diag_metadata.current_let_binding {
Some((pat_sp, Some(ty_sp), None)) Some((pat_sp, Some(ty_sp), None))
if ty_sp.contains(base_error.span) && base_error.could_be_expr => if ty_sp.contains(base_error.span) && base_error.could_be_expr =>
{ {
@ -962,7 +962,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
// Do not lint against unused label when we suggest them. // Do not lint against unused label when we suggest them.
self.diagnostic_metadata.unused_labels.remove(node_id); self.diag_metadata.unused_labels.remove(node_id);
} }
} }
} }
@ -993,7 +993,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
} }
err.code(E0411); err.code(E0411);
err.span_label(span, "`Self` is only available in impls, traits, and type definitions"); err.span_label(span, "`Self` is only available in impls, traits, and type definitions");
if let Some(item_kind) = self.diagnostic_metadata.current_item { if let Some(item_kind) = self.diag_metadata.current_item {
if !item_kind.ident.span.is_dummy() { if !item_kind.ident.span.is_dummy() {
err.span_label( err.span_label(
item_kind.ident.span, item_kind.ident.span,
@ -1031,7 +1031,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
}, },
); );
let is_assoc_fn = self.self_type_is_available(); let is_assoc_fn = self.self_type_is_available();
if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function { if let Some((fn_kind, span)) = &self.diag_metadata.current_function {
// The current function has a `self` parameter, but we were unable to resolve // The current function has a `self` parameter, but we were unable to resolve
// a reference to `self`. This can only happen if the `self` identifier we // a reference to `self`. This can only happen if the `self` identifier we
// are resolving came from a different hygiene context. // are resolving came from a different hygiene context.
@ -1077,7 +1077,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
); );
} }
} }
} else if let Some(item_kind) = self.diagnostic_metadata.current_item { } else if let Some(item_kind) = self.diag_metadata.current_item {
err.span_label( err.span_label(
item_kind.ident.span, item_kind.ident.span,
format!( format!(
@ -1095,7 +1095,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
err: &mut Diag<'_>, err: &mut Diag<'_>,
path: &[Segment], path: &[Segment],
) { ) {
let Some(pat) = self.diagnostic_metadata.current_pat else { return }; let Some(pat) = self.diag_metadata.current_pat else { return };
let (bound, side, range) = match &pat.kind { let (bound, side, range) = match &pat.kind {
ast::PatKind::Range(Some(bound), None, range) => (bound, Side::Start, range), ast::PatKind::Range(Some(bound), None, range) => (bound, Side::Start, range),
ast::PatKind::Range(None, Some(bound), range) => (bound, Side::End, range), ast::PatKind::Range(None, Some(bound), range) => (bound, Side::End, range),
@ -1137,7 +1137,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
span: Span, span: Span,
) { ) {
if let Some((trait_ref, self_ty)) = if let Some((trait_ref, self_ty)) =
self.diagnostic_metadata.currently_processing_impl_trait.clone() self.diag_metadata.currently_processing_impl_trait.clone()
&& let TyKind::Path(_, self_ty_path) = &self_ty.kind && let TyKind::Path(_, self_ty_path) = &self_ty.kind
&& let PathResult::Module(ModuleOrUniformRoot::Module(module)) = && let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None) self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None)
@ -1158,7 +1158,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
} }
fn suggest_bare_struct_literal(&mut self, err: &mut Diag<'_>) { fn suggest_bare_struct_literal(&mut self, err: &mut Diag<'_>) {
if let Some(span) = self.diagnostic_metadata.current_block_could_be_bare_struct_literal { if let Some(span) = self.diag_metadata.current_block_could_be_bare_struct_literal {
err.multipart_suggestion( err.multipart_suggestion(
"you might have meant to write a `struct` literal", "you might have meant to write a `struct` literal",
vec![ vec![
@ -1195,7 +1195,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
_ => return, _ => return,
}; };
let Some(item) = self.diagnostic_metadata.current_item else { return }; let Some(item) = self.diag_metadata.current_item else { return };
let Some(generics) = item.kind.generics() else { return }; let Some(generics) = item.kind.generics() else { return };
let param = generics.params.iter().find_map(|param| { let param = generics.params.iter().find_map(|param| {
@ -1230,7 +1230,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
) -> bool { ) -> bool {
if let PathSource::Expr(_) = source if let PathSource::Expr(_) = source
&& let Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. }) = && let Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. }) =
self.diagnostic_metadata.in_if_condition self.diag_metadata.in_if_condition
{ {
// Icky heuristic so we don't suggest: // Icky heuristic so we don't suggest:
// `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern) // `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern)
@ -1287,7 +1287,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
bound_generic_params, bound_generic_params,
bounds, bounds,
span, span,
})) = self.diagnostic_metadata.current_where_predicate })) = self.diag_metadata.current_where_predicate
{ {
if !bound_generic_params.is_empty() { if !bound_generic_params.is_empty() {
return false; return false;
@ -2015,7 +2015,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// Fields are generally expected in the same contexts as locals. // Fields are generally expected in the same contexts as locals.
if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) { if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) {
if let Some(node_id) = if let Some(node_id) =
self.diagnostic_metadata.current_self_type.as_ref().and_then(extract_node_id) self.diag_metadata.current_self_type.as_ref().and_then(extract_node_id)
{ {
// Look for a field with the same name in the current self_type. // Look for a field with the same name in the current self_type.
if let Some(resolution) = self.r.partial_res_map.get(&node_id) { if let Some(resolution) = self.r.partial_res_map.get(&node_id) {
@ -2035,7 +2035,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
} }
} }
if let Some(items) = self.diagnostic_metadata.current_trait_assoc_items { if let Some(items) = self.diag_metadata.current_trait_assoc_items {
for assoc_item in items { for assoc_item in items {
if assoc_item.ident == ident { if assoc_item.ident == ident {
return Some(match &assoc_item.kind { return Some(match &assoc_item.kind {
@ -2250,8 +2250,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// try to give a suggestion for this pattern: `name = blah`, which is common in other languages // try to give a suggestion for this pattern: `name = blah`, which is common in other languages
// suggest `let name = blah` to introduce a new binding // suggest `let name = blah` to introduce a new binding
fn let_binding_suggestion(&mut self, err: &mut Diag<'_>, ident_span: Span) -> bool { fn let_binding_suggestion(&mut self, err: &mut Diag<'_>, ident_span: Span) -> bool {
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) = if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) = self.diag_metadata.in_assignment
self.diagnostic_metadata.in_assignment
&& let ast::ExprKind::Path(None, ref path) = lhs.kind && let ast::ExprKind::Path(None, ref path) = lhs.kind
{ {
if !ident_span.from_expansion() { if !ident_span.from_expansion() {
@ -2502,10 +2501,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
let mut iter = ident.chars().map(|c| c.is_uppercase()); let mut iter = ident.chars().map(|c| c.is_uppercase());
let single_uppercase_char = let single_uppercase_char =
matches!(iter.next(), Some(true)) && matches!(iter.next(), None); matches!(iter.next(), Some(true)) && matches!(iter.next(), None);
if !self.diagnostic_metadata.currently_processing_generic_args && !single_uppercase_char { if !self.diag_metadata.currently_processing_generic_args && !single_uppercase_char {
return None; return None;
} }
match (self.diagnostic_metadata.current_item, single_uppercase_char, self.diagnostic_metadata.currently_processing_generic_args) { match (self.diag_metadata.current_item, single_uppercase_char, self.diag_metadata.currently_processing_generic_args) {
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), _, _) if ident.name == sym::main => { (Some(Item { kind: ItemKind::Fn(..), ident, .. }), _, _) if ident.name == sym::main => {
// Ignore `fn main()` as we don't want to suggest `fn main<T>()` // Ignore `fn main()` as we don't want to suggest `fn main<T>()`
} }
@ -3079,7 +3078,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|| lt.kind == MissingLifetimeKind::Underscore) || lt.kind == MissingLifetimeKind::Underscore)
{ {
let pre = if lt.kind == MissingLifetimeKind::Ampersand let pre = if lt.kind == MissingLifetimeKind::Ampersand
&& let Some((kind, _span)) = self.diagnostic_metadata.current_function && let Some((kind, _span)) = self.diag_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind && let FnKind::Fn(_, _, sig, _, _, _) = kind
&& !sig.decl.inputs.is_empty() && !sig.decl.inputs.is_empty()
&& let sugg = sig && let sugg = sig
@ -3120,7 +3119,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
"...or alternatively, you might want" "...or alternatively, you might want"
} else if (lt.kind == MissingLifetimeKind::Ampersand } else if (lt.kind == MissingLifetimeKind::Ampersand
|| lt.kind == MissingLifetimeKind::Underscore) || lt.kind == MissingLifetimeKind::Underscore)
&& let Some((kind, _span)) = self.diagnostic_metadata.current_function && let Some((kind, _span)) = self.diag_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind && let FnKind::Fn(_, _, sig, _, _, _) = kind
&& let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output && let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
&& !sig.decl.inputs.is_empty() && !sig.decl.inputs.is_empty()
@ -3180,7 +3179,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
}; };
let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand; let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand;
let mut sugg = vec![(lt.span, String::new())]; let mut sugg = vec![(lt.span, String::new())];
if let Some((kind, _span)) = self.diagnostic_metadata.current_function if let Some((kind, _span)) = self.diag_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind && let FnKind::Fn(_, _, sig, _, _, _) = kind
&& let ast::FnRetTy::Ty(ty) = &sig.decl.output && let ast::FnRetTy::Ty(ty) = &sig.decl.output
{ {