Shorten some error invocations.

- `struct_foo` + `emit` -> `foo`
- `create_foo` + `emit` -> `emit_foo`

I have made recent commits in other PRs that have removed some of these
shortcuts for combinations with few uses, e.g.
`struct_span_err_with_code`. But for the remaining combinations that
have high levels of use, we might as well use them wherever possible.
This commit is contained in:
Nicholas Nethercote 2024-01-04 10:38:10 +11:00
parent 4864cb8aef
commit ff40ad4107
13 changed files with 30 additions and 34 deletions

View File

@ -155,7 +155,7 @@ pub fn expand_include<'cx>(
if self.p.token != token::Eof {
let token = pprust::token_to_string(&self.p.token);
let msg = format!("expected item, found `{token}`");
self.p.dcx().struct_span_err(self.p.token.span, msg).emit();
self.p.dcx().span_err(self.p.token.span, msg);
}
break;

View File

@ -180,7 +180,7 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx,
}
Error(err_sp, msg) => {
let span = err_sp.substitute_dummy(self.root_span);
self.cx.dcx().struct_span_err(span, msg.clone()).emit();
self.cx.dcx().span_err(span, msg.clone());
self.result = Some(DummyResult::any(span));
}
ErrorReported(_) => self.result = Some(DummyResult::any(self.root_span)),

View File

@ -458,7 +458,7 @@ pub fn compile_declarative_macro(
return dummy_syn_ext();
}
Error(sp, msg) => {
sess.dcx().struct_span_err(sp.substitute_dummy(def.span), msg).emit();
sess.dcx().span_err(sp.substitute_dummy(def.span), msg);
return dummy_syn_ext();
}
ErrorReported(_) => {

View File

@ -552,7 +552,7 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
sym::simd_shuffle_generic => (2, 1, vec![param(0), param(0)], param(1)),
_ => {
let msg = format!("unrecognized platform-specific intrinsic function: `{name}`");
tcx.dcx().struct_span_err(it.span, msg).emit();
tcx.dcx().span_err(it.span, msg);
return;
}
};

View File

@ -325,7 +325,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
op.is_clobber(),
) {
let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
self.tcx.dcx().span_err(*op_sp, msg);
continue;
}
}
@ -364,7 +364,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
reg_class.name(),
feature
);
self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
self.tcx.dcx().span_err(*op_sp, msg);
// register isn't enabled, don't do more checks
continue;
}
@ -378,7 +378,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
.intersperse(", ")
.collect::<String>(),
);
self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
self.tcx.dcx().span_err(*op_sp, msg);
// register isn't enabled, don't do more checks
continue;
}

View File

@ -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;

View File

@ -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 });
}
};

View File

@ -1447,7 +1447,7 @@ impl EarlyDiagCtxt {
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_note(&self, msg: impl Into<DiagnosticMessage>) {
self.dcx.struct_note(msg).emit()
self.dcx.note(msg)
}
#[allow(rustc::untranslatable_diagnostic)]
@ -1460,13 +1460,13 @@ impl EarlyDiagCtxt {
#[allow(rustc::diagnostic_outside_of_impl)]
#[must_use = "ErrorGuaranteed must be returned from `run_compiler` in order to exit with a non-zero status code"]
pub fn early_err(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
self.dcx.struct_err(msg).emit()
self.dcx.err(msg)
}
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_fatal(&self, msg: impl Into<DiagnosticMessage>) -> ! {
self.dcx.struct_fatal(msg).emit()
self.dcx.fatal(msg)
}
#[allow(rustc::untranslatable_diagnostic)]
@ -1481,7 +1481,7 @@ impl EarlyDiagCtxt {
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_warn(&self, msg: impl Into<DiagnosticMessage>) {
self.dcx.struct_warn(msg).emit()
self.dcx.warn(msg)
}
pub fn initialize_checked_jobserver(&self) {

View File

@ -421,7 +421,7 @@ impl Options {
let paths = match theme::load_css_paths(content) {
Ok(p) => p,
Err(e) => {
dcx.struct_err(e).emit();
dcx.err(e);
return Err(1);
}
};
@ -452,10 +452,10 @@ impl Options {
let input = PathBuf::from(if describe_lints {
"" // dummy, this won't be used
} else if matches.free.is_empty() {
dcx.struct_err("missing file operand").emit();
dcx.err("missing file operand");
return Err(1);
} else if matches.free.len() > 1 {
dcx.struct_err("too many file operands").emit();
dcx.err("too many file operands");
return Err(1);
} else {
&matches.free[0]
@ -467,7 +467,7 @@ impl Options {
let extern_html_root_urls = match parse_extern_html_roots(matches) {
Ok(ex) => ex,
Err(err) => {
dcx.struct_err(err).emit();
dcx.err(err);
return Err(1);
}
};
@ -534,7 +534,7 @@ impl Options {
let output = matches.opt_str("output").map(|s| PathBuf::from(&s));
let output = match (out_dir, output) {
(Some(_), Some(_)) => {
dcx.struct_err("cannot use both 'out-dir' and 'output' at once").emit();
dcx.err("cannot use both 'out-dir' and 'output' at once");
return Err(1);
}
(Some(out_dir), None) => out_dir,
@ -549,7 +549,7 @@ impl Options {
if let Some(ref p) = extension_css {
if !p.is_file() {
dcx.struct_err("option --extend-css argument must be a file").emit();
dcx.err("option --extend-css argument must be a file");
return Err(1);
}
}
@ -567,7 +567,7 @@ impl Options {
let paths = match theme::load_css_paths(content) {
Ok(p) => p,
Err(e) => {
dcx.struct_err(e).emit();
dcx.err(e);
return Err(1);
}
};
@ -589,7 +589,7 @@ impl Options {
}
let (success, ret) = theme::test_theme_against(&theme_file, &paths, &dcx);
if !success {
dcx.struct_err(format!("error loading theme file: \"{theme_s}\"")).emit();
dcx.err(format!("error loading theme file: \"{theme_s}\""));
return Err(1);
} else if !ret.is_empty() {
dcx.struct_warn(format!(
@ -626,7 +626,7 @@ impl Options {
match matches.opt_str("r").as_deref() {
Some("rust") | None => {}
Some(s) => {
dcx.struct_err(format!("unknown input format: {s}")).emit();
dcx.err(format!("unknown input format: {s}"));
return Err(1);
}
}
@ -634,7 +634,7 @@ impl Options {
let index_page = matches.opt_str("index-page").map(|s| PathBuf::from(&s));
if let Some(ref index_page) = index_page {
if !index_page.is_file() {
dcx.struct_err("option `--index-page` argument must be a file").emit();
dcx.err("option `--index-page` argument must be a file");
return Err(1);
}
}
@ -646,7 +646,7 @@ impl Options {
let crate_types = match parse_crate_types_from_list(matches.opt_strs("crate-type")) {
Ok(types) => types,
Err(e) => {
dcx.struct_err(format!("unknown crate type: {e}")).emit();
dcx.err(format!("unknown crate type: {e}"));
return Err(1);
}
};
@ -664,7 +664,7 @@ impl Options {
out_fmt
}
Err(e) => {
dcx.struct_err(e).emit();
dcx.err(e);
return Err(1);
}
},

View File

@ -96,7 +96,7 @@ pub(crate) fn load_string<P: AsRef<Path>>(
match str::from_utf8(&contents) {
Ok(s) => Ok(s.to_string()),
Err(_) => {
dcx.struct_err(format!("error reading `{}`: not UTF-8", file_path.display())).emit();
dcx.err(format!("error reading `{}`: not UTF-8", file_path.display()));
Err(LoadStringError::BadUtf8)
}
}

View File

@ -757,8 +757,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
// Flush pending errors.
Rc::get_mut(&mut self.shared).unwrap().fs.close();
let nb_errors =
self.shared.errors.iter().map(|err| self.tcx().dcx().struct_err(err).emit()).count();
let nb_errors = self.shared.errors.iter().map(|err| self.tcx().dcx().err(err)).count();
if nb_errors > 0 {
Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
} else {

View File

@ -676,10 +676,7 @@ type MainResult = Result<(), ErrorGuaranteed>;
fn wrap_return(dcx: &rustc_errors::DiagCtxt, res: Result<(), String>) -> MainResult {
match res {
Ok(()) => dcx.has_errors().map_or(Ok(()), Err),
Err(err) => {
let reported = dcx.struct_err(err).emit();
Err(reported)
}
Err(err) => Err(dcx.err(err)),
}
}

View File

@ -244,7 +244,7 @@ pub(crate) fn test_theme_against<P: AsRef<Path>>(
{
Ok(c) => c,
Err(e) => {
dcx.struct_err(e).emit();
dcx.err(e);
return (false, vec![]);
}
};