Skip to content

Commit b411ade

Browse files
committed
Move rustc_interface diagnostics to struct SessionDiagnostic derives
1 parent e4403ae commit b411ade

File tree

4 files changed

+137
-40
lines changed

4 files changed

+137
-40
lines changed

compiler/rustc_error_messages/locales/en-US/interface.ftl

+37
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,40 @@ interface_ferris_identifier =
44
55
interface_emoji_identifier =
66
identifiers cannot contain emoji: `{$ident}`
7+
8+
mixed_bin_crate =
9+
cannot mix `bin` crate type with others
10+
11+
mixed_proc_macro_crate =
12+
cannot mix `proc-macro` crate type with others
13+
14+
proc_macro_doc_without_arg =
15+
Trying to document proc macro crate without passing '--crate-type proc-macro to rustdoc
16+
.warn = The generated documentation may be incorrect
17+
18+
error_writing_dependencies =
19+
error writing dependencies to `{$path}`: {$error}
20+
21+
input_file_would_be_overwritten =
22+
the input file "{$path}" would be overwritten by the generated executable
23+
24+
generated_file_conflicts_with_directory =
25+
the generated executable for the input file "{$input_path}" conflicts with the existing directory "{$dir_path}"
26+
27+
temps_dir_error =
28+
failed to find or create the directory specified by `--temps-dir`
29+
30+
out_dir_error =
31+
failed to find or create the directory specified by `--out-dir`
32+
33+
cant_emit_mir =
34+
could not emit MIR: {$error}
35+
36+
rustc_error_fatal =
37+
fatal error triggered by #[rustc_error]
38+
39+
rustc_error_unexpected_annotation =
40+
unexpected annotation used with `#[rustc_error(...)]!
41+
42+
failed_writing_file =
43+
failed to write file {$path}: {$error}"

compiler/rustc_interface/src/errors.rs

+68
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::DiagnosticArgFromDisplay;
12
use rustc_macros::SessionDiagnostic;
23
use rustc_span::{Span, Symbol};
34

@@ -17,3 +18,70 @@ pub struct EmojiIdentifier {
1718
pub spans: Vec<Span>,
1819
pub ident: Symbol,
1920
}
21+
22+
#[derive(SessionDiagnostic)]
23+
#[diag(interface::mixed_bin_crate)]
24+
pub struct MixedBinCrate;
25+
26+
#[derive(SessionDiagnostic)]
27+
#[diag(interface::mixed_proc_macro_crate)]
28+
pub struct MixedProcMacroCrate;
29+
30+
#[derive(SessionDiagnostic)]
31+
#[diag(interface::proc_macro_doc_without_arg)]
32+
pub struct ProcMacroDocWithoutArg;
33+
34+
#[derive(SessionDiagnostic)]
35+
#[diag(interface::error_writing_dependencies)]
36+
pub struct ErrorWritingDependencies<'a> {
37+
pub path: DiagnosticArgFromDisplay<'a>,
38+
pub error: DiagnosticArgFromDisplay<'a>,
39+
}
40+
41+
#[derive(SessionDiagnostic)]
42+
#[diag(interface::input_file_would_be_overwritten)]
43+
pub struct InputFileWouldBeOverWritten<'a> {
44+
pub path: DiagnosticArgFromDisplay<'a>,
45+
}
46+
47+
#[derive(SessionDiagnostic)]
48+
#[diag(interface::generated_file_conflicts_with_directory)]
49+
pub struct GeneratedFileConflictsWithDirectory<'a> {
50+
pub input_path: DiagnosticArgFromDisplay<'a>,
51+
pub dir_path: DiagnosticArgFromDisplay<'a>,
52+
}
53+
54+
#[derive(SessionDiagnostic)]
55+
#[diag(interface::temps_dir_error)]
56+
pub struct TempsDirError;
57+
58+
#[derive(SessionDiagnostic)]
59+
#[diag(interface::out_dir_error)]
60+
pub struct OutDirError;
61+
62+
#[derive(SessionDiagnostic)]
63+
#[diag(interface::cant_emit_mir)]
64+
pub struct CantEmitMIR<'a> {
65+
pub error: DiagnosticArgFromDisplay<'a>,
66+
}
67+
68+
#[derive(SessionDiagnostic)]
69+
#[diag(interface::rustc_error_fatal)]
70+
pub struct RustcErrorFatal {
71+
#[primary_span]
72+
pub span: Span,
73+
}
74+
75+
#[derive(SessionDiagnostic)]
76+
#[diag(interface::rustc_error_unexpected_annotation)]
77+
pub struct RustcErrorUnexpectedAnnotation {
78+
#[primary_span]
79+
pub span: Span,
80+
}
81+
82+
#[derive(SessionDiagnostic)]
83+
#[diag(interface::failed_writing_file)]
84+
pub struct FailedWritingFile<'a> {
85+
pub path: DiagnosticArgFromDisplay<'a>,
86+
pub error: DiagnosticArgFromDisplay<'a>,
87+
}

compiler/rustc_interface/src/passes.rs

+23-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::errors::{EmojiIdentifier, FerrisIdentifier};
1+
use crate::errors::{
2+
CantEmitMIR, EmojiIdentifier, ErrorWritingDependencies, FerrisIdentifier,
3+
GeneratedFileConflictsWithDirectory, InputFileWouldBeOverWritten, MixedBinCrate,
4+
MixedProcMacroCrate, OutDirError, ProcMacroDocWithoutArg, TempsDirError,
5+
};
26
use crate::interface::{Compiler, Result};
37
use crate::proc_macro_decls;
48
use crate::util;
@@ -375,10 +379,10 @@ pub fn configure_and_expand(
375379

376380
if crate_types.len() > 1 {
377381
if is_executable_crate {
378-
sess.err("cannot mix `bin` crate type with others");
382+
sess.emit_err(MixedBinCrate);
379383
}
380384
if is_proc_macro_crate {
381-
sess.err("cannot mix `proc-macro` crate type with others");
385+
sess.emit_err(MixedProcMacroCrate);
382386
}
383387
}
384388

@@ -389,13 +393,7 @@ pub fn configure_and_expand(
389393
// However, we do emit a warning, to let such users know that they should
390394
// start passing '--crate-type proc-macro'
391395
if has_proc_macro_decls && sess.opts.actually_rustdoc && !is_proc_macro_crate {
392-
let mut msg = sess.diagnostic().struct_warn(
393-
"Trying to document proc macro crate \
394-
without passing '--crate-type proc-macro to rustdoc",
395-
);
396-
397-
msg.warn("The generated documentation may be incorrect");
398-
msg.emit();
396+
sess.emit_warning(ProcMacroDocWithoutArg);
399397
} else {
400398
krate = sess.time("maybe_create_a_macro_crate", || {
401399
let is_test_crate = sess.opts.test;
@@ -649,11 +647,12 @@ fn write_out_deps(
649647
.emit_artifact_notification(&deps_filename, "dep-info");
650648
}
651649
}
652-
Err(e) => sess.fatal(&format!(
653-
"error writing dependencies to `{}`: {}",
654-
deps_filename.display(),
655-
e
656-
)),
650+
Err(e) => {
651+
sess.emit_fatal(ErrorWritingDependencies {
652+
path: (&deps_filename.display()).into(),
653+
error: (&e).into(),
654+
});
655+
}
657656
}
658657
}
659658

@@ -683,29 +682,23 @@ pub fn prepare_outputs(
683682
if let Some(ref input_path) = compiler.input_path {
684683
if sess.opts.will_create_output_file() {
685684
if output_contains_path(&output_paths, input_path) {
686-
let reported = sess.err(&format!(
687-
"the input file \"{}\" would be overwritten by the generated \
688-
executable",
689-
input_path.display()
690-
));
685+
let reported = sess
686+
.emit_err(InputFileWouldBeOverWritten { path: (&input_path.display()).into() });
691687
return Err(reported);
692688
}
693689
if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
694-
let reported = sess.err(&format!(
695-
"the generated executable for the input file \"{}\" conflicts with the \
696-
existing directory \"{}\"",
697-
input_path.display(),
698-
dir_path.display()
699-
));
690+
let reported = sess.emit_err(GeneratedFileConflictsWithDirectory {
691+
input_path: (&input_path.display()).into(),
692+
dir_path: (&dir_path.display()).into(),
693+
});
700694
return Err(reported);
701695
}
702696
}
703697
}
704698

705699
if let Some(ref dir) = compiler.temps_dir {
706700
if fs::create_dir_all(dir).is_err() {
707-
let reported =
708-
sess.err("failed to find or create the directory specified by `--temps-dir`");
701+
let reported = sess.emit_err(TempsDirError);
709702
return Err(reported);
710703
}
711704
}
@@ -718,8 +711,7 @@ pub fn prepare_outputs(
718711
if !only_dep_info {
719712
if let Some(ref dir) = compiler.output_dir {
720713
if fs::create_dir_all(dir).is_err() {
721-
let reported =
722-
sess.err("failed to find or create the directory specified by `--out-dir`");
714+
let reported = sess.emit_err(OutDirError);
723715
return Err(reported);
724716
}
725717
}
@@ -1003,7 +995,7 @@ pub fn start_codegen<'tcx>(
1003995

1004996
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
1005997
if let Err(e) = rustc_mir_transform::dump_mir::emit_mir(tcx, outputs) {
1006-
tcx.sess.err(&format!("could not emit MIR: {}", e));
998+
tcx.sess.emit_err(CantEmitMIR { error: (&e).into() });
1007999
tcx.sess.abort_if_errors();
10081000
}
10091001
}

compiler/rustc_interface/src/queries.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
12
use crate::interface::{Compiler, Result};
23
use crate::passes::{self, BoxedResolver, QueryContext};
34

@@ -274,18 +275,14 @@ impl<'tcx> Queries<'tcx> {
274275

275276
// Bare `#[rustc_error]`.
276277
None => {
277-
tcx.sess.span_fatal(
278-
tcx.def_span(def_id),
279-
"fatal error triggered by #[rustc_error]",
280-
);
278+
tcx.sess.emit_fatal(RustcErrorFatal { span: tcx.def_span(def_id) });
281279
}
282280

283281
// Some other attribute.
284282
Some(_) => {
285-
tcx.sess.span_warn(
286-
tcx.def_span(def_id),
287-
"unexpected annotation used with `#[rustc_error(...)]!",
288-
);
283+
tcx.sess.emit_warning(RustcErrorUnexpectedAnnotation {
284+
span: tcx.def_span(def_id),
285+
});
289286
}
290287
}
291288
}
@@ -361,7 +358,10 @@ impl Linker {
361358
let encoded = CodegenResults::serialize_rlink(&codegen_results);
362359
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
363360
std::fs::write(&rlink_file, encoded).map_err(|err| {
364-
sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
361+
sess.emit_fatal(FailedWritingFile {
362+
path: (&rlink_file.display()).into(),
363+
error: (&err).into(),
364+
})
365365
})?;
366366
return Ok(());
367367
}

0 commit comments

Comments
 (0)