Skip to content

Commit 0fd4a74

Browse files
authored
Rollup merge of #100808 - SkiFire13:migrate_diagnostics_rustc_interface, r=davidtwco
Migrate `rustc_interface` diagnostics ``@rustbot`` label +A-translation r? rust-lang/diagnostics cc #100717
2 parents 2e4760c + 645de5b commit 0fd4a74

File tree

5 files changed

+156
-62
lines changed

5 files changed

+156
-62
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+
interface_mixed_bin_crate =
9+
cannot mix `bin` crate type with others
10+
11+
interface_mixed_proc_macro_crate =
12+
cannot mix `proc-macro` crate type with others
13+
14+
interface_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+
interface_error_writing_dependencies =
19+
error writing dependencies to `{$path}`: {$error}
20+
21+
interface_input_file_would_be_overwritten =
22+
the input file "{$path}" would be overwritten by the generated executable
23+
24+
interface_generated_file_conflicts_with_directory =
25+
the generated executable for the input file "{$input_path}" conflicts with the existing directory "{$dir_path}"
26+
27+
interface_temps_dir_error =
28+
failed to find or create the directory specified by `--temps-dir`
29+
30+
interface_out_dir_error =
31+
failed to find or create the directory specified by `--out-dir`
32+
33+
interface_cant_emit_mir =
34+
could not emit MIR: {$error}
35+
36+
interface_rustc_error_fatal =
37+
fatal error triggered by #[rustc_error]
38+
39+
interface_rustc_error_unexpected_annotation =
40+
unexpected annotation used with `#[rustc_error(...)]!
41+
42+
interface_failed_writing_file =
43+
failed to write file {$path}: {$error}"
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use rustc_macros::SessionDiagnostic;
2+
use rustc_span::{Span, Symbol};
3+
4+
use std::io;
5+
use std::path::Path;
6+
7+
#[derive(SessionDiagnostic)]
8+
#[diag(interface::ferris_identifier)]
9+
pub struct FerrisIdentifier {
10+
#[primary_span]
11+
pub spans: Vec<Span>,
12+
#[suggestion(code = "ferris", applicability = "maybe-incorrect")]
13+
pub first_span: Span,
14+
}
15+
16+
#[derive(SessionDiagnostic)]
17+
#[diag(interface::emoji_identifier)]
18+
pub struct EmojiIdentifier {
19+
#[primary_span]
20+
pub spans: Vec<Span>,
21+
pub ident: Symbol,
22+
}
23+
24+
#[derive(SessionDiagnostic)]
25+
#[diag(interface::mixed_bin_crate)]
26+
pub struct MixedBinCrate;
27+
28+
#[derive(SessionDiagnostic)]
29+
#[diag(interface::mixed_proc_macro_crate)]
30+
pub struct MixedProcMacroCrate;
31+
32+
#[derive(SessionDiagnostic)]
33+
#[diag(interface::proc_macro_doc_without_arg)]
34+
pub struct ProcMacroDocWithoutArg;
35+
36+
#[derive(SessionDiagnostic)]
37+
#[diag(interface::error_writing_dependencies)]
38+
pub struct ErrorWritingDependencies<'a> {
39+
pub path: &'a Path,
40+
pub error: io::Error,
41+
}
42+
43+
#[derive(SessionDiagnostic)]
44+
#[diag(interface::input_file_would_be_overwritten)]
45+
pub struct InputFileWouldBeOverWritten<'a> {
46+
pub path: &'a Path,
47+
}
48+
49+
#[derive(SessionDiagnostic)]
50+
#[diag(interface::generated_file_conflicts_with_directory)]
51+
pub struct GeneratedFileConflictsWithDirectory<'a> {
52+
pub input_path: &'a Path,
53+
pub dir_path: &'a Path,
54+
}
55+
56+
#[derive(SessionDiagnostic)]
57+
#[diag(interface::temps_dir_error)]
58+
pub struct TempsDirError;
59+
60+
#[derive(SessionDiagnostic)]
61+
#[diag(interface::out_dir_error)]
62+
pub struct OutDirError;
63+
64+
#[derive(SessionDiagnostic)]
65+
#[diag(interface::cant_emit_mir)]
66+
pub struct CantEmitMIR {
67+
pub error: io::Error,
68+
}
69+
70+
#[derive(SessionDiagnostic)]
71+
#[diag(interface::rustc_error_fatal)]
72+
pub struct RustcErrorFatal {
73+
#[primary_span]
74+
pub span: Span,
75+
}
76+
77+
#[derive(SessionDiagnostic)]
78+
#[diag(interface::rustc_error_unexpected_annotation)]
79+
pub struct RustcErrorUnexpectedAnnotation {
80+
#[primary_span]
81+
pub span: Span,
82+
}
83+
84+
#[derive(SessionDiagnostic)]
85+
#[diag(interface::failed_writing_file)]
86+
pub struct FailedWritingFile<'a> {
87+
pub path: &'a Path,
88+
pub error: io::Error,
89+
}

compiler/rustc_interface/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
#![feature(once_cell)]
66
#![recursion_limit = "256"]
77
#![allow(rustc::potential_query_instability)]
8+
#![deny(rustc::untranslatable_diagnostic)]
9+
#![deny(rustc::diagnostic_outside_of_impl)]
810

911
mod callbacks;
12+
mod errors;
1013
pub mod interface;
1114
mod passes;
1215
mod proc_macro_decls;

compiler/rustc_interface/src/passes.rs

+20-51
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
use crate::errors::{
2+
CantEmitMIR, EmojiIdentifier, ErrorWritingDependencies, FerrisIdentifier,
3+
GeneratedFileConflictsWithDirectory, InputFileWouldBeOverWritten, MixedBinCrate,
4+
MixedProcMacroCrate, OutDirError, ProcMacroDocWithoutArg, TempsDirError,
5+
};
16
use crate::interface::{Compiler, Result};
27
use crate::proc_macro_decls;
38
use crate::util;
@@ -13,7 +18,6 @@ use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
1318
use rustc_hir::def_id::StableCrateId;
1419
use rustc_hir::definitions::Definitions;
1520
use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore};
16-
use rustc_macros::SessionDiagnostic;
1721
use rustc_metadata::creader::CStore;
1822
use rustc_middle::arena::Arena;
1923
use rustc_middle::dep_graph::DepGraph;
@@ -31,7 +35,7 @@ use rustc_session::output::filename_for_input;
3135
use rustc_session::search_paths::PathKind;
3236
use rustc_session::{Limit, Session};
3337
use rustc_span::symbol::{sym, Symbol};
34-
use rustc_span::{FileName, Span};
38+
use rustc_span::FileName;
3539
use rustc_trait_selection::traits;
3640
use rustc_typeck as typeck;
3741
use tracing::{info, warn};
@@ -264,23 +268,6 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> {
264268
}
265269
}
266270

267-
#[derive(SessionDiagnostic)]
268-
#[diag(interface::ferris_identifier)]
269-
struct FerrisIdentifier {
270-
#[primary_span]
271-
spans: Vec<Span>,
272-
#[suggestion(code = "ferris", applicability = "maybe-incorrect")]
273-
first_span: Span,
274-
}
275-
276-
#[derive(SessionDiagnostic)]
277-
#[diag(interface::emoji_identifier)]
278-
struct EmojiIdentifier {
279-
#[primary_span]
280-
spans: Vec<Span>,
281-
ident: Symbol,
282-
}
283-
284271
/// Runs the "early phases" of the compiler: initial `cfg` processing, loading compiler plugins,
285272
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
286273
/// harness if one is to be provided, injection of a dependency on the
@@ -392,10 +379,10 @@ pub fn configure_and_expand(
392379

393380
if crate_types.len() > 1 {
394381
if is_executable_crate {
395-
sess.err("cannot mix `bin` crate type with others");
382+
sess.emit_err(MixedBinCrate);
396383
}
397384
if is_proc_macro_crate {
398-
sess.err("cannot mix `proc-macro` crate type with others");
385+
sess.emit_err(MixedProcMacroCrate);
399386
}
400387
}
401388

@@ -406,13 +393,7 @@ pub fn configure_and_expand(
406393
// However, we do emit a warning, to let such users know that they should
407394
// start passing '--crate-type proc-macro'
408395
if has_proc_macro_decls && sess.opts.actually_rustdoc && !is_proc_macro_crate {
409-
let mut msg = sess.diagnostic().struct_warn(
410-
"Trying to document proc macro crate \
411-
without passing '--crate-type proc-macro to rustdoc",
412-
);
413-
414-
msg.warn("The generated documentation may be incorrect");
415-
msg.emit();
396+
sess.emit_warning(ProcMacroDocWithoutArg);
416397
} else {
417398
krate = sess.time("maybe_create_a_macro_crate", || {
418399
let is_test_crate = sess.opts.test;
@@ -666,11 +647,9 @@ fn write_out_deps(
666647
.emit_artifact_notification(&deps_filename, "dep-info");
667648
}
668649
}
669-
Err(e) => sess.fatal(&format!(
670-
"error writing dependencies to `{}`: {}",
671-
deps_filename.display(),
672-
e
673-
)),
650+
Err(error) => {
651+
sess.emit_fatal(ErrorWritingDependencies { path: &deps_filename, error });
652+
}
674653
}
675654
}
676655

@@ -700,29 +679,20 @@ pub fn prepare_outputs(
700679
if let Some(ref input_path) = compiler.input_path {
701680
if sess.opts.will_create_output_file() {
702681
if output_contains_path(&output_paths, input_path) {
703-
let reported = sess.err(&format!(
704-
"the input file \"{}\" would be overwritten by the generated \
705-
executable",
706-
input_path.display()
707-
));
682+
let reported = sess.emit_err(InputFileWouldBeOverWritten { path: input_path });
708683
return Err(reported);
709684
}
710-
if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
711-
let reported = sess.err(&format!(
712-
"the generated executable for the input file \"{}\" conflicts with the \
713-
existing directory \"{}\"",
714-
input_path.display(),
715-
dir_path.display()
716-
));
685+
if let Some(ref dir_path) = output_conflicts_with_dir(&output_paths) {
686+
let reported =
687+
sess.emit_err(GeneratedFileConflictsWithDirectory { input_path, dir_path });
717688
return Err(reported);
718689
}
719690
}
720691
}
721692

722693
if let Some(ref dir) = compiler.temps_dir {
723694
if fs::create_dir_all(dir).is_err() {
724-
let reported =
725-
sess.err("failed to find or create the directory specified by `--temps-dir`");
695+
let reported = sess.emit_err(TempsDirError);
726696
return Err(reported);
727697
}
728698
}
@@ -735,8 +705,7 @@ pub fn prepare_outputs(
735705
if !only_dep_info {
736706
if let Some(ref dir) = compiler.output_dir {
737707
if fs::create_dir_all(dir).is_err() {
738-
let reported =
739-
sess.err("failed to find or create the directory specified by `--out-dir`");
708+
let reported = sess.emit_err(OutDirError);
740709
return Err(reported);
741710
}
742711
}
@@ -1019,8 +988,8 @@ pub fn start_codegen<'tcx>(
1019988
info!("Post-codegen\n{:?}", tcx.debug_stats());
1020989

1021990
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
1022-
if let Err(e) = rustc_mir_transform::dump_mir::emit_mir(tcx, outputs) {
1023-
tcx.sess.err(&format!("could not emit MIR: {}", e));
991+
if let Err(error) = rustc_mir_transform::dump_mir::emit_mir(tcx, outputs) {
992+
tcx.sess.emit_err(CantEmitMIR { error });
1024993
tcx.sess.abort_if_errors();
1025994
}
1026995
}

compiler/rustc_interface/src/queries.rs

+7-11
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
}
@@ -360,9 +357,8 @@ impl Linker {
360357
if sess.opts.unstable_opts.no_link {
361358
let encoded = CodegenResults::serialize_rlink(&codegen_results);
362359
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
363-
std::fs::write(&rlink_file, encoded).map_err(|err| {
364-
sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
365-
})?;
360+
std::fs::write(&rlink_file, encoded)
361+
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
366362
return Ok(());
367363
}
368364

0 commit comments

Comments
 (0)