Skip to content

Commit 9a4ead9

Browse files
committed
Migrate diagnostics on lib.rs
1 parent 6895110 commit 9a4ead9

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

src/librustdoc/errors.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::fluent_generated as fluent;
2+
use rustc_errors::IntoDiagnostic;
3+
use rustc_macros::Diagnostic;
4+
use rustc_span::ErrorGuaranteed;
5+
6+
#[derive(Diagnostic)]
7+
#[diag(rustdoc_main_error)]
8+
pub(crate) struct MainError {
9+
pub(crate) error: String,
10+
}
11+
12+
pub(crate) struct CouldntGenerateDocumentation {
13+
pub(crate) error: String,
14+
pub(crate) file: String,
15+
}
16+
17+
impl IntoDiagnostic<'_> for CouldntGenerateDocumentation {
18+
fn into_diagnostic(
19+
self,
20+
handler: &'_ rustc_errors::Handler,
21+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
22+
let mut diag = handler.struct_err(fluent::rustdoc_couldnt_generate_documentation);
23+
diag.set_arg("error", self.error);
24+
if !self.file.is_empty() {
25+
diag.note(fluent::_subdiag::note);
26+
}
27+
28+
diag
29+
}
30+
}
31+
32+
#[derive(Diagnostic)]
33+
#[diag(rustdoc_compilation_failed)]
34+
pub(crate) struct CompilationFailed;

src/librustdoc/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#![warn(rustc::internal)]
2020
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]
2121
#![allow(rustc::potential_query_instability)]
22+
#![deny(rustc::untranslatable_diagnostic)]
23+
#![deny(rustc::diagnostic_outside_of_impl)]
2224

2325
extern crate thin_vec;
2426
#[macro_use]
@@ -43,6 +45,7 @@ extern crate rustc_driver;
4345
extern crate rustc_errors;
4446
extern crate rustc_expand;
4547
extern crate rustc_feature;
48+
extern crate rustc_fluent_macro;
4649
extern crate rustc_hir;
4750
extern crate rustc_hir_analysis;
4851
extern crate rustc_hir_pretty;
@@ -74,15 +77,18 @@ use std::env::{self, VarError};
7477
use std::io::{self, IsTerminal};
7578
use std::process;
7679

80+
use errors::{CouldntGenerateDocumentation, MainError};
7781
use rustc_driver::abort_on_err;
78-
use rustc_errors::ErrorGuaranteed;
82+
use rustc_errors::{DiagnosticMessage, ErrorGuaranteed, SubdiagnosticMessage};
83+
use rustc_fluent_macro::fluent_messages;
7984
use rustc_interface::interface;
8085
use rustc_middle::ty::TyCtxt;
8186
use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
8287
use rustc_session::getopts;
8388
use rustc_session::{early_error, early_warn};
8489

8590
use crate::clean::utils::DOC_RUST_LANG_ORG_CHANNEL;
91+
use crate::errors::CompilationFailed;
8692

8793
/// A macro to create a FxHashMap.
8894
///
@@ -108,6 +114,7 @@ mod core;
108114
mod docfs;
109115
mod doctest;
110116
mod error;
117+
mod errors;
111118
mod externalfiles;
112119
mod fold;
113120
mod formats;
@@ -123,6 +130,8 @@ mod visit;
123130
mod visit_ast;
124131
mod visit_lib;
125132

133+
fluent_messages! { "./messages.ftl" }
134+
126135
pub fn main() {
127136
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
128137
// about jemalloc.
@@ -683,10 +692,7 @@ type MainResult = Result<(), ErrorGuaranteed>;
683692
fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
684693
match res {
685694
Ok(()) => diag.has_errors().map_or(Ok(()), Err),
686-
Err(err) => {
687-
let reported = diag.struct_err(err).emit();
688-
Err(reported)
689-
}
695+
Err(error) => Err(diag.emit_err(MainError { error })),
690696
}
691697
}
692698

@@ -698,15 +704,10 @@ fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
698704
) -> MainResult {
699705
match formats::run_format::<T>(krate, renderopts, cache, tcx) {
700706
Ok(_) => tcx.sess.has_errors().map_or(Ok(()), Err),
701-
Err(e) => {
702-
let mut msg =
703-
tcx.sess.struct_err(format!("couldn't generate documentation: {}", e.error));
704-
let file = e.file.display().to_string();
705-
if !file.is_empty() {
706-
msg.note(format!("failed to create or modify \"{}\"", file));
707-
}
708-
Err(msg.emit())
709-
}
707+
Err(e) => Err(tcx.sess.emit_err(CouldntGenerateDocumentation {
708+
error: e.error,
709+
file: e.file.display().to_string(),
710+
})),
710711
}
711712
}
712713

@@ -817,7 +818,7 @@ fn main_args(at_args: &[String]) -> MainResult {
817818
compiler.enter(|queries| {
818819
let mut gcx = abort_on_err(queries.global_ctxt(), sess);
819820
if sess.diagnostic().has_errors_or_lint_errors().is_some() {
820-
sess.fatal("Compilation failed, aborting rustdoc");
821+
sess.emit_fatal(CompilationFailed);
821822
}
822823

823824
gcx.enter(|tcx| {

src/librustdoc/messages.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
rustdoc_main_error = {$error}
2+
3+
rustdoc_couldnt_generate_documentation =
4+
couldn't generate documentation: {$error}
5+
.note = failed to create or modify "{$file}"
6+
7+
rustdoc_compilation_failed = Compilation failed, aborting rustdoc

0 commit comments

Comments
 (0)