-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Migrate rustdoc diagnostics to translatable diagnostics
#112807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| use crate::fluent_generated as fluent; | ||
| use rustc_errors::IntoDiagnostic; | ||
| use rustc_macros::Diagnostic; | ||
| use rustc_span::ErrorGuaranteed; | ||
|
|
||
| #[derive(Diagnostic)] | ||
| #[diag(rustdoc_main_error)] | ||
| pub(crate) struct MainError { | ||
| pub(crate) error: String, | ||
| } | ||
|
|
||
| pub(crate) struct CouldntGenerateDocumentation { | ||
| pub(crate) error: String, | ||
| pub(crate) file: String, | ||
| } | ||
|
|
||
| impl IntoDiagnostic<'_> for CouldntGenerateDocumentation { | ||
| fn into_diagnostic( | ||
| self, | ||
| handler: &'_ rustc_errors::Handler, | ||
| ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> { | ||
| let mut diag = handler.struct_err(fluent::rustdoc_couldnt_generate_documentation); | ||
| diag.set_arg("error", self.error); | ||
| if !self.file.is_empty() { | ||
| diag.note(fluent::_subdiag::note); | ||
| } | ||
|
|
||
| diag | ||
| } | ||
| } | ||
|
|
||
| #[derive(Diagnostic)] | ||
| #[diag(rustdoc_compilation_failed)] | ||
| pub(crate) struct CompilationFailed; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| #![warn(rustc::internal)] | ||
| #![allow(clippy::collapsible_if, clippy::collapsible_else_if)] | ||
| #![allow(rustc::potential_query_instability)] | ||
| #![deny(rustc::untranslatable_diagnostic)] | ||
| #![deny(rustc::diagnostic_outside_of_impl)] | ||
|
|
||
| extern crate thin_vec; | ||
| #[macro_use] | ||
|
|
@@ -43,6 +45,7 @@ extern crate rustc_driver; | |
| extern crate rustc_errors; | ||
| extern crate rustc_expand; | ||
| extern crate rustc_feature; | ||
| extern crate rustc_fluent_macro; | ||
| extern crate rustc_hir; | ||
| extern crate rustc_hir_analysis; | ||
| extern crate rustc_hir_pretty; | ||
|
|
@@ -74,15 +77,18 @@ use std::env::{self, VarError}; | |
| use std::io::{self, IsTerminal}; | ||
| use std::process; | ||
|
|
||
| use errors::{CouldntGenerateDocumentation, MainError}; | ||
| use rustc_driver::abort_on_err; | ||
| use rustc_errors::ErrorGuaranteed; | ||
| use rustc_errors::{DiagnosticMessage, ErrorGuaranteed, SubdiagnosticMessage}; | ||
| use rustc_fluent_macro::fluent_messages; | ||
| use rustc_interface::interface; | ||
| use rustc_middle::ty::TyCtxt; | ||
| use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup}; | ||
| use rustc_session::getopts; | ||
| use rustc_session::{early_error, early_warn}; | ||
|
|
||
| use crate::clean::utils::DOC_RUST_LANG_ORG_CHANNEL; | ||
| use crate::errors::CompilationFailed; | ||
|
|
||
| /// A macro to create a FxHashMap. | ||
| /// | ||
|
|
@@ -108,6 +114,7 @@ mod core; | |
| mod docfs; | ||
| mod doctest; | ||
| mod error; | ||
| mod errors; | ||
| mod externalfiles; | ||
| mod fold; | ||
| mod formats; | ||
|
|
@@ -123,6 +130,8 @@ mod visit; | |
| mod visit_ast; | ||
| mod visit_lib; | ||
|
|
||
| fluent_messages! { "./messages.ftl" } | ||
|
|
||
| pub fn main() { | ||
| // See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs | ||
| // about jemalloc. | ||
|
|
@@ -683,10 +692,7 @@ type MainResult = Result<(), ErrorGuaranteed>; | |
| fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult { | ||
| match res { | ||
| Ok(()) => diag.has_errors().map_or(Ok(()), Err), | ||
| Err(err) => { | ||
| let reported = diag.struct_err(err).emit(); | ||
| Err(reported) | ||
| } | ||
| Err(error) => Err(diag.emit_err(MainError { error })), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -698,15 +704,10 @@ fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>( | |
| ) -> MainResult { | ||
| match formats::run_format::<T>(krate, renderopts, cache, tcx) { | ||
| Ok(_) => tcx.sess.has_errors().map_or(Ok(()), Err), | ||
| Err(e) => { | ||
| let mut msg = | ||
| tcx.sess.struct_err(format!("couldn't generate documentation: {}", e.error)); | ||
| let file = e.file.display().to_string(); | ||
| if !file.is_empty() { | ||
| msg.note(format!("failed to create or modify \"{}\"", file)); | ||
| } | ||
| Err(msg.emit()) | ||
| } | ||
| Err(e) => Err(tcx.sess.emit_err(CouldntGenerateDocumentation { | ||
| error: e.error, | ||
| file: e.file.display().to_string(), | ||
|
||
| })), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -817,7 +818,7 @@ fn main_args(at_args: &[String]) -> MainResult { | |
| compiler.enter(|queries| { | ||
| let mut gcx = abort_on_err(queries.global_ctxt(), sess); | ||
| if sess.diagnostic().has_errors_or_lint_errors().is_some() { | ||
| sess.fatal("Compilation failed, aborting rustdoc"); | ||
| sess.emit_fatal(CompilationFailed); | ||
| } | ||
|
|
||
| gcx.enter(|tcx| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,7 @@ | ||||||
| rustdoc_main_error = {$error} | ||||||
| rustdoc_couldnt_generate_documentation = | ||||||
| couldn't generate documentation: {$error} | ||||||
| .note = failed to create or modify "{$file}" | ||||||
| rustdoc_compilation_failed = Compilation failed, aborting rustdoc | ||||||
|
||||||
| rustdoc_compilation_failed = Compilation failed, aborting rustdoc | |
| rustdoc_compilation_failed = compilation failed, aborting rustdoc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the diagnostics in rustdoc are in lowercase (around < 5 that are not).
Shall we set all them to lowercase? @jsha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it makes sense to "translate" this -- it's not any better in this current state, IMO. Can you just add
diagnostic_outside_of_implfor this function and removeMainError?Ideally we'd change
Result<(), String>into something that's likeResult<(), T>whereT: IntoDiagnosticor something...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some thought, realized that the error string that we got here comes from
Result, which is in English.If we would like to make this section translatable, then we need to support translations of
Result::Err. Is this even feasible?