#![allow(clippy::manual_assert)]
extern crate rustc_ast;
+extern crate rustc_data_structures;
+extern crate rustc_error_messages;
extern crate rustc_errors;
extern crate rustc_expand;
extern crate rustc_parse as parse;
WhereClause,
};
use rustc_ast::mut_visit::{self, MutVisitor};
-use rustc_errors::PResult;
+use rustc_error_messages::{DiagnosticMessage, FluentArgs, LazyFallbackBundle};
+use rustc_errors::{Diagnostic, PResult};
use rustc_session::parse::ParseSess;
use rustc_span::source_map::FilePathMapping;
use rustc_span::FileName;
let before = match librustc_parse(content, &sess) {
Ok(before) => before,
Err(diagnostic) => {
- if diagnostic
- .message()
- .starts_with("file not found for module")
- {
- errorf!("=== {}: ignore\n", path.display());
- } else {
- errorf!(
- "=== {}: ignore - librustc failed to parse original content: {}\n",
- path.display(),
- diagnostic.message(),
- );
- }
+ errorf!(
+ "=== {}: ignore - librustc failed to parse original content: {}\n",
+ path.display(),
+ translate_message(&diagnostic),
+ );
diagnostic.cancel();
return Err(true);
}
parse::parse_crate_from_source_str(name, content, sess)
}
+fn translate_message(diagnostic: &Diagnostic) -> String {
+ thread_local! {
+ static FLUENT_BUNDLE: LazyFallbackBundle = {
+ let resources = rustc_error_messages::DEFAULT_LOCALE_RESOURCES;
+ let with_directionality_markers = false;
+ rustc_error_messages::fallback_fluent_bundle(resources, with_directionality_markers)
+ };
+ }
+
+ let message = &diagnostic.message[0].0;
+ let args = diagnostic.args().iter().cloned().collect::<FluentArgs>();
+
+ let (identifier, attr) = match message {
+ DiagnosticMessage::Str(msg) => return msg.clone(),
+ DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr),
+ };
+
+ FLUENT_BUNDLE.with(|fluent_bundle| {
+ let message = fluent_bundle
+ .get_message(identifier)
+ .expect("missing diagnostic in fluent bundle");
+ let value = match attr {
+ Some(attr) => message
+ .get_attribute(attr)
+ .expect("missing attribute in fluent message")
+ .value(),
+ None => message.value().expect("missing value in fluent message"),
+ };
+
+ let mut err = Vec::new();
+ let translated = fluent_bundle.format_pattern(value, Some(&args), &mut err);
+ assert!(err.is_empty());
+ translated.into_owned()
+ })
+}
+
fn normalize(krate: &mut Crate) {
struct NormalizeVisitor;