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)]
struct DiagnosticMetadata<'ast> {
struct DiagMetadata<'ast> {
/// The current trait's associated items' ident, used for diagnostic suggestions.
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)>,
/// 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.
///
@@ -694,12 +694,12 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
// as they do not correspond to actual code.
}
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.
let old_ignore = replace(&mut self.in_func_body, false);
self.with_lifetime_rib(LifetimeRibKind::Item, |this| this.resolve_item(item));
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) {
self.resolve_arm(arm);
@@ -716,10 +716,10 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
self.resolve_expr(expr, None);
}
fn visit_pat(&mut self, p: &'ast Pat) {
let prev = self.diagnostic_metadata.current_pat;
self.diagnostic_metadata.current_pat = Some(p);
let prev = self.diag_metadata.current_pat;
self.diag_metadata.current_pat = Some(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) {
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),
)),
};
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.diagnostic_metadata.current_let_binding = original;
self.diag_metadata.current_let_binding = original;
}
fn visit_ty(&mut self, ty: &'ast Ty) {
let prev = self.diagnostic_metadata.current_trait_object;
let prev_ty = self.diagnostic_metadata.current_type_path;
let prev = self.diag_metadata.current_trait_object;
let prev_ty = self.diag_metadata.current_type_path;
match &ty.kind {
TyKind::Ref(None, _) => {
// 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);
}
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);
// 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;
}
TyKind::TraitObject(bounds, ..) => {
self.diagnostic_metadata.current_trait_object = Some(&bounds[..]);
self.diag_metadata.current_trait_object = Some(&bounds[..]);
visit::walk_ty(self, ty)
}
TyKind::BareFn(bare_fn) => {
@@ -842,8 +842,8 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
}
_ => visit::walk_ty(self, ty),
}
self.diagnostic_metadata.current_trait_object = prev;
self.diagnostic_metadata.current_type_path = prev_ty;
self.diag_metadata.current_trait_object = prev;
self.diag_metadata.current_type_path = prev_ty;
}
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());
@@ -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) {
let previous_value = self.diagnostic_metadata.current_function;
let previous_value = self.diag_metadata.current_function;
match fn_kind {
// Bail if the function is foreign, and thus cannot validly have
// 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;
}
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.
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) {
self.resolve_lifetime(lifetime, use_ctxt)
}
fn visit_generics(&mut self, generics: &'ast Generics) {
self.visit_generic_params(
&generics.params,
self.diagnostic_metadata.current_self_item.is_some(),
);
self.visit_generic_params(&generics.params, self.diag_metadata.current_self_item.is_some());
for p in &generics.where_clause.predicates {
self.visit_where_predicate(p);
}
@@ -1062,7 +1059,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
ClosureBinder::For { generic_params, .. } => {
self.visit_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) {
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 {
GenericArg::Type(ref ty) => {
// 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;
}
}
@@ -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.diagnostic_metadata.currently_processing_generic_args = prev;
self.diag_metadata.currently_processing_generic_args = prev;
}
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) {
debug!("visit_where_predicate {:?}", p);
let previous_value =
replace(&mut self.diagnostic_metadata.current_where_predicate, Some(p));
let previous_value = replace(&mut self.diag_metadata.current_where_predicate, Some(p));
self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
ref bounded_ty,
@@ -1224,7 +1220,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
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) {
@@ -1297,7 +1293,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
lifetime_ribs: Vec::new(),
lifetime_elision_candidates: None,
current_trait_ref: None,
diagnostic_metadata: Default::default(),
diag_metadata: Default::default(),
// errors at module scope should always be reported
in_func_body: false,
lifetime_uses: Default::default(),
@@ -1707,7 +1703,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
return;
}
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);
return;
}
@@ -1911,7 +1907,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
break;
}
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 {
self.record_lifetime_res(
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);
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() {
self.r.lifetime_elision_allowed.insert(fn_id);
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));
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() {
let Err(failure_info) = elision_lifetime else { bug!() };
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
.diagnostic_metadata
.diag_metadata
.current_self_type
.as_ref()
.and_then(|ty| {
@@ -2385,7 +2381,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
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(
&item.attrs,
generics,
@@ -2394,7 +2390,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
item.id,
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, .. }) => {
@@ -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 {
// Handle nested impls (inside fn bodies)
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);
self.diagnostic_metadata.current_self_type = previous_value;
self.diag_metadata.current_self_type = previous_value;
result
}
fn with_current_self_item<T>(&mut self, self_item: &Item, f: impl FnOnce(&mut Self) -> T) -> T {
let previous_value =
replace(&mut self.diagnostic_metadata.current_self_item, Some(self_item.id));
let previous_value = replace(&mut self.diag_metadata.current_self_item, Some(self_item.id));
let result = f(self);
self.diagnostic_metadata.current_self_item = previous_value;
self.diag_metadata.current_self_item = previous_value;
result
}
/// When evaluating a `trait` use its associated types' idents for suggestions in E0412.
fn resolve_trait_items(&mut self, trait_items: &'ast [P<AssocItem>]) {
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 =
|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`).
@@ -2832,7 +2827,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
let mut new_id = None;
if let Some(trait_ref) = opt_trait_ref {
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()));
let res = self.smart_resolve_path_fragment(
&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),
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() {
new_id = Some(def_id);
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 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()
{
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() => {
// 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
&& items.iter().any(|item| {
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)) {
if let Some(label) = label {
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) {
@@ -4226,12 +4221,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
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), .. }]) =
(block.could_be_bare_literal, &block.stmts[..])
&& 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.
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.diagnostic_metadata.current_block_could_be_bare_struct_literal = prev;
self.diag_metadata.current_block_could_be_bare_struct_literal = prev;
// Move back up.
self.parent_scope.module = orig_module;
@@ -4353,7 +4348,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
Ok((node_id, _)) => {
// Since this res is a label, it is never read.
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) => {
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) => {
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.diagnostic_metadata.in_if_condition = old;
this.diag_metadata.in_if_condition = old;
this.visit_block(then);
});
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) => {
self.with_resolved_label(label, expr.id, |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.diagnostic_metadata.in_if_condition = old;
this.diag_metadata.in_if_condition = old;
this.visit_block(block);
})
});
@@ -4476,20 +4471,20 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
self.visit_expr(idx);
}
ExprKind::Assign(ref lhs, ref rhs, _) => {
if !self.diagnostic_metadata.is_assign_rhs {
self.diagnostic_metadata.in_assignment = Some(expr);
if !self.diag_metadata.is_assign_rhs {
self.diag_metadata.in_assignment = Some(expr);
}
self.visit_expr(lhs);
self.diagnostic_metadata.is_assign_rhs = true;
self.diagnostic_metadata.in_assignment = None;
self.diag_metadata.is_assign_rhs = true;
self.diag_metadata.in_assignment = None;
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) => {
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(end, Some(expr));
self.diagnostic_metadata.in_range = None;
self.diag_metadata.in_range = None;
}
_ => {
visit::walk_expr(self, expr);
@@ -4737,7 +4732,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID));
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");
}
}