diff options
| author | bors <bors@rust-lang.org> | 2024-01-10 18:03:53 +0000 | 
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-01-10 18:03:53 +0000 | 
| commit | a2d9d73e608f1b24eba840c4fd2d68dbe3b65e01 (patch) | |
| tree | 42523e01769059455f89af632b53b841b1401d69 /compiler/rustc_resolve | |
| parent | e9271846294c4ee5bd7706df68180320c0b5ff20 (diff) | |
| parent | 700a3965202f4f403956fcce744ce5ba6adf2ddb (diff) | |
| download | rust-a2d9d73e608f1b24eba840c4fd2d68dbe3b65e01.tar.gz rust-a2d9d73e608f1b24eba840c4fd2d68dbe3b65e01.zip | |
Auto merge of #119751 - nnethercote:error-api-fixes, r=oli-obk
Diagnostic API fixes Some improvements to diagnostic APIs: improve some naming, use shortcuts in more places, and add a couple of missing methods. r? `@compiler-errors`
Diffstat (limited to 'compiler/rustc_resolve')
| -rw-r--r-- | compiler/rustc_resolve/src/build_reduced_graph.rs | 19 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 36 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/imports.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/late.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 24 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/macros.rs | 12 | 
6 files changed, 62 insertions, 51 deletions
| diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index e176b8b4043..9ccfde5e3c6 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -19,7 +19,7 @@ use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind}; use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId}; use rustc_attr as attr; use rustc_data_structures::sync::Lrc; -use rustc_errors::{struct_span_err, Applicability}; +use rustc_errors::{struct_span_code_err, Applicability}; use rustc_expand::expand::AstFragment; use rustc_hir::def::{self, *}; use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; @@ -818,7 +818,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { self.r .dcx() .struct_span_err(item.span, "`extern crate self;` requires renaming") - .span_suggestion_mv( + .with_span_suggestion( item.span, "rename the `self` crate to be able to import it", "extern crate self as name;", @@ -999,7 +999,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { let msg = format!("`{name}` is already in scope"); let note = "macro-expanded `#[macro_use]`s may not shadow existing macros (see RFC 1560)"; - self.r.dcx().struct_span_err(span, msg).note_mv(note).emit(); + self.r.dcx().struct_span_err(span, msg).with_note(note).emit(); } } @@ -1010,7 +1010,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { for attr in &item.attrs { if attr.has_name(sym::macro_use) { if self.parent_scope.module.parent.is_some() { - struct_span_err!( + struct_span_code_err!( self.r.dcx(), item.span, E0468, @@ -1024,7 +1024,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { } } let ill_formed = |span| { - struct_span_err!(self.r.dcx(), span, E0466, "bad macro import").emit(); + struct_span_code_err!(self.r.dcx(), span, E0466, "bad macro import").emit(); }; match attr.meta() { Some(meta) => match meta.kind { @@ -1095,8 +1095,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { allow_shadowing, ); } else { - struct_span_err!(self.r.dcx(), ident.span, E0469, "imported macro not found") - .emit(); + struct_span_code_err!( + self.r.dcx(), + ident.span, + E0469, + "imported macro not found" + ) + .emit(); } } } diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 57f23200e79..0d744238eeb 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -6,7 +6,7 @@ use rustc_ast::{MetaItemKind, NestedMetaItem}; use rustc_ast_pretty::pprust; use rustc_data_structures::fx::FxHashSet; use rustc_errors::{ - pluralize, report_ambiguity_error, struct_span_err, Applicability, DiagCtxt, Diagnostic, + pluralize, report_ambiguity_error, struct_span_code_err, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, SuggestionStyle, }; use rustc_feature::BUILTIN_ATTRIBUTES; @@ -153,7 +153,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { BuiltinLintDiagnostics::AmbiguousGlobImports { diag }, ); } else { - let mut err = struct_span_err!(self.dcx(), diag.span, E0659, "{}", &diag.msg); + let mut err = struct_span_code_err!(self.dcx(), diag.span, E0659, "{}", &diag.msg); report_ambiguity_error(&mut err, diag); err.emit(); } @@ -254,15 +254,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let msg = format!("the name `{name}` is defined multiple times"); let mut err = match (old_binding.is_extern_crate(), new_binding.is_extern_crate()) { - (true, true) => struct_span_err!(self.dcx(), span, E0259, "{}", msg), + (true, true) => struct_span_code_err!(self.dcx(), span, E0259, "{}", msg), (true, _) | (_, true) => match new_binding.is_import() && old_binding.is_import() { - true => struct_span_err!(self.dcx(), span, E0254, "{}", msg), - false => struct_span_err!(self.dcx(), span, E0260, "{}", msg), + true => struct_span_code_err!(self.dcx(), span, E0254, "{}", msg), + false => struct_span_code_err!(self.dcx(), span, E0260, "{}", msg), }, _ => match (old_binding.is_import_user_facing(), new_binding.is_import_user_facing()) { - (false, false) => struct_span_err!(self.dcx(), span, E0428, "{}", msg), - (true, true) => struct_span_err!(self.dcx(), span, E0252, "{}", msg), - _ => struct_span_err!(self.dcx(), span, E0255, "{}", msg), + (false, false) => struct_span_code_err!(self.dcx(), span, E0428, "{}", msg), + (true, true) => struct_span_code_err!(self.dcx(), span, E0252, "{}", msg), + _ => struct_span_code_err!(self.dcx(), span, E0255, "{}", msg), }, }; @@ -659,7 +659,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let origin_sp = origin.iter().copied().collect::<Vec<_>>(); let msp = MultiSpan::from_spans(target_sp.clone()); - let mut err = struct_span_err!( + let mut err = struct_span_code_err!( self.dcx(), msp, E0408, @@ -788,7 +788,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } ResolutionError::FailedToResolve { last_segment, label, suggestion, module } => { let mut err = - struct_span_err!(self.dcx(), span, E0433, "failed to resolve: {}", &label); + struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {}", &label); err.span_label(span, label); if let Some((suggestions, msg, applicability)) = suggestion { @@ -950,9 +950,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { "item `{name}` is an associated {kind}, which doesn't match its trait `{trait_path}`", ), ) - .code_mv(code) - .span_label_mv(span, "does not match trait") - .span_label_mv(trait_item_span, "item in trait") + .with_code(code) + .with_span_label(span, "does not match trait") + .with_span_label(trait_item_span, "item in trait") } ResolutionError::TraitImplDuplicate { name, trait_item_span, old_span } => self .dcx() @@ -1702,8 +1702,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { // Print the primary message. let descr = get_descr(binding); - let mut err = - struct_span_err!(self.dcx(), ident.span, E0603, "{} `{}` is private", descr, ident); + let mut err = struct_span_code_err!( + self.dcx(), + ident.span, + E0603, + "{} `{}` is private", + descr, + ident + ); err.span_label(ident.span, format!("private {descr}")); let mut not_publicly_reexported = false; diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index c5bd7ffa038..2ebf4c20562 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -17,7 +17,7 @@ use crate::{NameBinding, NameBindingData, NameBindingKind, PathResult}; use rustc_ast::NodeId; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::intern::Interned; -use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan}; +use rustc_errors::{pluralize, struct_span_code_err, Applicability, MultiSpan}; use rustc_hir::def::{self, DefKind, PartialRes}; use rustc_middle::metadata::ModChild; use rustc_middle::metadata::Reexport; @@ -686,7 +686,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { .collect::<Vec<_>>(); let msg = format!("unresolved import{} {}", pluralize!(paths.len()), paths.join(", "),); - let mut diag = struct_span_err!(self.dcx(), span, E0432, "{}", &msg); + let mut diag = struct_span_code_err!(self.dcx(), span, E0432, "{}", &msg); if let Some((_, UnresolvedImportError { note: Some(note), .. })) = errors.iter().last() { diag.note(note.clone()); @@ -1371,12 +1371,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() }; let ModuleOrUniformRoot::Module(module) = import.imported_module.get().unwrap() else { - self.dcx().create_err(CannotGlobImportAllCrates { span: import.span }).emit(); + self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span }); return; }; if module.is_trait() { - self.dcx().create_err(ItemsInTraitsAreNotImportable { span: import.span }).emit(); + self.dcx().emit_err(ItemsInTraitsAreNotImportable { span: import.span }); return; } else if module == import.parent_scope.module { return; diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 059ccaebc82..4a3c8dfe3d7 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -1664,7 +1664,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { } else { ("`'_` cannot be used here", "`'_` is a reserved lifetime name") }; - let mut diag = rustc_errors::struct_span_err!( + let mut diag = rustc_errors::struct_span_code_err!( self.r.dcx(), lifetime.ident.span, E0637, @@ -1853,7 +1853,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. } | LifetimeRibKind::AnonymousWarn(_) => { let sess = self.r.tcx.sess; - let mut err = rustc_errors::struct_span_err!( + let mut err = rustc_errors::struct_span_code_err!( sess.dcx(), path_span, E0726, @@ -2301,7 +2301,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { let report_error = |this: &Self, ns| { if this.should_report_errs() { let what = if ns == TypeNS { "type parameters" } else { "local variables" }; - this.r.dcx().create_err(ImportsCannotReferTo { span: ident.span, what }).emit(); + this.r.dcx().emit_err(ImportsCannotReferTo { span: ident.span, what }); } }; @@ -2594,13 +2594,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { } if param.ident.name == kw::UnderscoreLifetime { - rustc_errors::struct_span_err!( + rustc_errors::struct_span_code_err!( self.r.dcx(), param.ident.span, E0637, "`'_` cannot be used here" ) - .span_label_mv(param.ident.span, "`'_` is a reserved lifetime name") + .with_span_label(param.ident.span, "`'_` is a reserved lifetime name") .emit(); // Record lifetime res, so lowering knows there is something fishy. self.record_lifetime_param(param.id, LifetimeRes::Error); @@ -2608,14 +2608,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { } if param.ident.name == kw::StaticLifetime { - rustc_errors::struct_span_err!( + rustc_errors::struct_span_code_err!( self.r.dcx(), param.ident.span, E0262, "invalid lifetime parameter name: `{}`", param.ident, ) - .span_label_mv(param.ident.span, "'static is a reserved lifetime name") + .with_span_label(param.ident.span, "'static is a reserved lifetime name") .emit(); // Record lifetime res, so lowering knows there is something fishy. self.record_lifetime_param(param.id, LifetimeRes::Error); diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index a36d1377ab5..2f476ae6cbc 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -16,7 +16,7 @@ use rustc_ast::{ use rustc_ast_pretty::pprust::where_bound_predicate_to_string; use rustc_data_structures::fx::FxHashSet; use rustc_errors::{ - pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, + pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, SuggestionStyle, }; use rustc_hir as hir; @@ -2603,23 +2603,23 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { ) { debug_assert_ne!(lifetime_ref.ident.name, kw::UnderscoreLifetime); let mut err = if let Some(outer) = outer_lifetime_ref { - struct_span_err!( + struct_span_code_err!( self.r.dcx(), lifetime_ref.ident.span, E0401, "can't use generic parameters from outer item", ) - .span_label_mv(lifetime_ref.ident.span, "use of generic parameter from outer item") - .span_label_mv(outer.span, "lifetime parameter from outer item") + .with_span_label(lifetime_ref.ident.span, "use of generic parameter from outer item") + .with_span_label(outer.span, "lifetime parameter from outer item") } else { - struct_span_err!( + struct_span_code_err!( self.r.dcx(), lifetime_ref.ident.span, E0261, "use of undeclared lifetime name `{}`", lifetime_ref.ident ) - .span_label_mv(lifetime_ref.ident.span, "undeclared lifetime") + .with_span_label(lifetime_ref.ident.span, "undeclared lifetime") }; self.suggest_introducing_lifetime( &mut err, @@ -2777,7 +2777,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { let num_lifetimes: usize = lifetime_refs.iter().map(|lt| lt.count).sum(); let spans: Vec<_> = lifetime_refs.iter().map(|lt| lt.span).collect(); - let mut err = struct_span_err!( + let mut err = struct_span_code_err!( self.r.dcx(), spans, E0106, @@ -3282,15 +3282,15 @@ fn mk_where_bound_predicate( /// Report lifetime/lifetime shadowing as an error. pub(super) fn signal_lifetime_shadowing(sess: &Session, orig: Ident, shadower: Ident) { - struct_span_err!( + struct_span_code_err!( sess.dcx(), shadower.span, E0496, "lifetime name `{}` shadows a lifetime name that is already in scope", orig.name, ) - .span_label_mv(orig.span, "first declared here") - .span_label_mv(shadower.span, format!("lifetime `{}` already in scope", orig.name)) + .with_span_label(orig.span, "first declared here") + .with_span_label(shadower.span, format!("lifetime `{}` already in scope", orig.name)) .emit(); } @@ -3322,7 +3322,7 @@ pub(super) fn signal_label_shadowing(sess: &Session, orig: Span, shadower: Ident shadower, format!("label name `{name}` shadows a label name that is already in scope"), ) - .span_label_mv(orig, "first declared here") - .span_label_mv(shadower, format!("label `{name}` already in scope")) + .with_span_label(orig, "first declared here") + .with_span_label(shadower, format!("label `{name}` already in scope")) .emit(); } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index fc55481cb01..66ecaeb4449 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -15,7 +15,7 @@ use rustc_ast_pretty::pprust; use rustc_attr::StabilityLevel; use rustc_data_structures::intern::Interned; use rustc_data_structures::sync::Lrc; -use rustc_errors::{struct_span_err, Applicability}; +use rustc_errors::{struct_span_code_err, Applicability}; use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand}; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::compile_declarative_macro; @@ -128,7 +128,7 @@ pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools { let msg = format!("{} `{}` was already registered", "tool", ident); tcx.dcx() .struct_span_err(ident.span, msg) - .span_label_mv(old_ident.span, "already registered here") + .with_span_label(old_ident.span, "already registered here") .emit(); } } @@ -137,7 +137,7 @@ pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools { let span = nested_meta.span(); tcx.dcx() .struct_span_err(span, msg) - .span_label_mv(span, "not an identifier") + .with_span_label(span, "not an identifier") .emit(); } } @@ -578,7 +578,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { self.dcx() .create_err(err) - .span_label_mv(path.span, format!("not {article} {expected}")) + .with_span_label(path.span, format!("not {article} {expected}")) .emit(); return Ok((self.dummy_ext(kind), Res::Err)); @@ -948,13 +948,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { rule_spans = Vec::new(); } BuiltinMacroState::AlreadySeen(span) => { - struct_span_err!( + struct_span_code_err!( self.dcx(), item.span, E0773, "attempted to define built-in macro more than once" ) - .span_note_mv(span, "previously defined here") + .with_span_note(span, "previously defined here") .emit(); } } | 
