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`
This commit is contained in:
bors 2024-01-10 18:03:53 +00:00
commit a2d9d73e60
135 changed files with 766 additions and 756 deletions

View file

@ -2995,13 +2995,13 @@ fn clean_use_statement_inner<'tcx>(
visibility.is_accessible_from(parent_mod, cx.tcx) && !current_mod.is_top_level_module();
if pub_underscore && let Some(ref inline) = inline_attr {
rustc_errors::struct_span_err!(
rustc_errors::struct_span_code_err!(
cx.tcx.dcx(),
inline.span(),
E0780,
"anonymous imports cannot be inlined"
)
.span_label_mv(import.span, "anonymous import")
.with_span_label(import.span, "anonymous import")
.emit();
}

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);
}
};
@ -577,26 +577,26 @@ impl Options {
{
if !theme_file.is_file() {
dcx.struct_err(format!("invalid argument: \"{theme_s}\""))
.help_mv("arguments to --theme must be files")
.with_help("arguments to --theme must be files")
.emit();
return Err(1);
}
if theme_file.extension() != Some(OsStr::new("css")) {
dcx.struct_err(format!("invalid argument: \"{theme_s}\""))
.help_mv("arguments to --theme must have a .css extension")
.with_help("arguments to --theme must have a .css extension")
.emit();
return Err(1);
}
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!(
"theme file \"{theme_s}\" is missing CSS rules from the default theme",
))
.warn_mv("the theme may appear incorrect when loaded")
.help_mv(format!(
.with_warn("the theme may appear incorrect when loaded")
.with_help(format!(
"to see what rules are missing, call `rustdoc --check-theme \"{theme_s}\"`",
))
.emit();
@ -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);
}
},
@ -809,7 +809,7 @@ fn check_deprecated_options(matches: &getopts::Matches, dcx: &rustc_errors::Diag
for &flag in deprecated_flags.iter() {
if matches.opt_present(flag) {
dcx.struct_warn(format!("the `{flag}` flag is deprecated"))
.note_mv(
.with_note(
"see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
for more information",
)

View file

@ -495,15 +495,15 @@ impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> {
.intersperse("::")
.collect::<String>()
);
rustc_errors::struct_span_err!(
rustc_errors::struct_span_code_err!(
self.tcx.dcx(),
path.span,
E0433,
"failed to resolve: {label}",
)
.span_label_mv(path.span, label)
.note_mv("this error was originally ignored because you are running `rustdoc`")
.note_mv("try running again with `rustc` or `cargo check` and you may get a more detailed error")
.with_span_label(path.span, label)
.with_note("this error was originally ignored because you are running `rustdoc`")
.with_note("try running again with `rustc` or `cargo check` and you may get a more detailed error")
.emit();
}
// We could have an outer resolution that succeeded,

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

@ -1228,7 +1228,7 @@ impl LinkCollector<'_, '_> {
span,
"linking to associated items of raw pointers is experimental",
)
.note_mv("rustdoc does not allow disambiguating between `*const` and `*mut`, and pointers are unstable until it does")
.with_note("rustdoc does not allow disambiguating between `*const` and `*mut`, and pointers are unstable until it does")
.emit();
}

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![]);
}
};