Skip to content

Commit 2625518

Browse files
committed
various: translation resources from cg backend
Extend `CodegenBackend` trait with a function returning the translation resources from the codegen backend, which can be added to the complete list of resources provided to the emitter. Signed-off-by: David Wood <[email protected]>
1 parent a8e3750 commit 2625518

File tree

23 files changed

+86
-48
lines changed

23 files changed

+86
-48
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ pub struct CraneliftCodegenBackend {
172172
}
173173

174174
impl CodegenBackend for CraneliftCodegenBackend {
175+
fn locale_resource(&self) -> &'static str {
176+
// FIXME(rust-lang/rust#100717) - cranelift codegen backend is not yet translated
177+
""
178+
}
179+
175180
fn init(&self, sess: &Session) {
176181
use rustc_session::config::Lto;
177182
match sess.lto() {

compiler/rustc_codegen_gcc/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ pub struct GccCodegenBackend {
103103
}
104104

105105
impl CodegenBackend for GccCodegenBackend {
106+
fn locale_resource(&self) -> &'static str {
107+
crate::DEFAULT_LOCALE_RESOURCE
108+
}
109+
106110
fn init(&self, sess: &Session) {
107111
if sess.lto() != Lto::No {
108112
sess.emit_warning(LTONotSupported {});

compiler/rustc_codegen_llvm/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ impl LlvmCodegenBackend {
249249
}
250250

251251
impl CodegenBackend for LlvmCodegenBackend {
252+
fn locale_resource(&self) -> &'static str {
253+
crate::DEFAULT_LOCALE_RESOURCE
254+
}
255+
252256
fn init(&self, sess: &Session) {
253257
llvm_util::init(sess); // Make sure llvm is inited
254258
}

compiler/rustc_codegen_ssa/src/traits/backend.rs

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ impl<'tcx, T> Backend<'tcx> for T where
5757
}
5858

5959
pub trait CodegenBackend {
60+
/// Locale resources for diagnostic messages - a string the content of the Fluent resource.
61+
/// Called before `init` so that all other functions are able to emit translatable diagnostics.
62+
fn locale_resource(&self) -> &'static str;
63+
6064
fn init(&self, _sess: &Session) {}
6165
fn print(&self, _req: PrintRequest, _sess: &Session) {}
6266
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> {

compiler/rustc_driver_impl/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ fn run_compiler(
362362
}
363363

364364
// Make sure name resolution and macro expansion is run.
365-
queries.global_ctxt()?;
365+
queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering(()));
366366

367367
if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
368368
return early_exit();
@@ -1204,7 +1204,7 @@ static DEFAULT_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send +
12041204
/// hook.
12051205
pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12061206
let fallback_bundle =
1207-
rustc_errors::fallback_fluent_bundle(crate::DEFAULT_LOCALE_RESOURCES, false);
1207+
rustc_errors::fallback_fluent_bundle(crate::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
12081208
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
12091209
rustc_errors::ColorConfig::Auto,
12101210
None,

compiler/rustc_error_messages/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ pub type LazyFallbackBundle = Lrc<Lazy<FluentBundle, impl FnOnce() -> FluentBund
223223
/// Return the default `FluentBundle` with standard "en-US" diagnostic messages.
224224
#[instrument(level = "trace")]
225225
pub fn fallback_fluent_bundle(
226-
resources: &'static [&'static str],
226+
resources: Vec<&'static str>,
227227
with_directionality_markers: bool,
228228
) -> LazyFallbackBundle {
229229
Lrc::new(Lazy::new(move || {

compiler/rustc_errors/src/json/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ impl<T: Write> Write for Shared<T> {
4242

4343
/// Test the span yields correct positions in JSON.
4444
fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
45-
static TEST_LOCALE_RESOURCES: &[&str] = &[crate::DEFAULT_LOCALE_RESOURCE];
4645
rustc_span::create_default_session_globals_then(|| {
4746
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
4847
sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned());
49-
let fallback_bundle = crate::fallback_fluent_bundle(TEST_LOCALE_RESOURCES, false);
48+
let fallback_bundle =
49+
crate::fallback_fluent_bundle(vec![crate::DEFAULT_LOCALE_RESOURCE], false);
5050

5151
let output = Arc::new(Mutex::new(Vec::new()));
5252
let je = JsonEmitter::new(

compiler/rustc_expand/src/parse/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use rustc_span::{BytePos, FileName, Pos, Span};
1717

1818
use std::path::PathBuf;
1919

20-
static TEST_LOCALE_RESOURCES: &[&str] =
21-
&[crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE];
22-
2320
fn sess() -> ParseSess {
24-
ParseSess::new(TEST_LOCALE_RESOURCES, FilePathMapping::empty())
21+
ParseSess::new(
22+
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
23+
FilePathMapping::empty(),
24+
)
2525
}
2626

2727
/// Parses an item.

compiler/rustc_expand/src/tests.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ where
3434

3535
/// Maps a string to tts, using a made-up filename.
3636
pub(crate) fn string_to_stream(source_str: String) -> TokenStream {
37-
let ps = ParseSess::new(TEST_LOCALE_RESOURCES, FilePathMapping::empty());
37+
let ps = ParseSess::new(
38+
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
39+
FilePathMapping::empty(),
40+
);
3841
source_file_to_stream(
3942
&ps,
4043
ps.source_map().new_source_file(PathBuf::from("bogofile").into(), source_str),
@@ -45,7 +48,10 @@ pub(crate) fn string_to_stream(source_str: String) -> TokenStream {
4548

4649
/// Parses a string, returns a crate.
4750
pub(crate) fn string_to_crate(source_str: String) -> ast::Crate {
48-
let ps = ParseSess::new(TEST_LOCALE_RESOURCES, FilePathMapping::empty());
51+
let ps = ParseSess::new(
52+
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
53+
FilePathMapping::empty(),
54+
);
4955
with_error_checking_parse(source_str, &ps, |p| p.parse_crate_mod())
5056
}
5157

@@ -123,14 +129,14 @@ impl<T: Write> Write for Shared<T> {
123129
}
124130
}
125131

126-
static TEST_LOCALE_RESOURCES: &[&str] =
127-
&[crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE];
128-
129132
fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
130133
create_default_session_if_not_set_then(|_| {
131134
let output = Arc::new(Mutex::new(Vec::new()));
132135

133-
let fallback_bundle = rustc_errors::fallback_fluent_bundle(TEST_LOCALE_RESOURCES, false);
136+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
137+
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
138+
false,
139+
);
134140
let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
135141
source_map.new_source_file(Path::new("test.rs").to_owned().into(), file_text.to_owned());
136142

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
5050
output_file: None,
5151
temps_dir,
5252
};
53-
let sess = build_session(sessopts, io, None, registry, &[], Default::default(), None, None);
53+
let sess = build_session(sessopts, io, None, registry, vec![], Default::default(), None, None);
5454
(sess, cfg)
5555
}
5656

compiler/rustc_interface/src/util.rs

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ pub fn create_session(
9090
}
9191
};
9292

93+
let mut locale_resources = Vec::from(locale_resources);
94+
locale_resources.push(codegen_backend.locale_resource());
95+
9396
let mut sess = session::build_session(
9497
sopts,
9598
io,

compiler/rustc_session/src/parse.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,7 @@ pub struct ParseSess {
226226

227227
impl ParseSess {
228228
/// Used for testing.
229-
pub fn new(
230-
locale_resources: &'static [&'static str],
231-
file_path_mapping: FilePathMapping,
232-
) -> Self {
229+
pub fn new(locale_resources: Vec<&'static str>, file_path_mapping: FilePathMapping) -> Self {
233230
let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
234231
let sm = Lrc::new(SourceMap::new(file_path_mapping));
235232
let handler = Handler::with_tty_emitter(
@@ -268,7 +265,7 @@ impl ParseSess {
268265
}
269266

270267
pub fn with_silent_emitter(fatal_note: Option<String>) -> Self {
271-
let fallback_bundle = fallback_fluent_bundle(&[], false);
268+
let fallback_bundle = fallback_fluent_bundle(Vec::new(), false);
272269
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
273270
let fatal_handler =
274271
Handler::with_tty_emitter(ColorConfig::Auto, false, None, None, None, fallback_bundle);

compiler/rustc_session/src/session.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ pub fn build_session(
13411341
io: CompilerIO,
13421342
bundle: Option<Lrc<rustc_errors::FluentBundle>>,
13431343
registry: rustc_errors::registry::Registry,
1344-
fluent_resources: &'static [&'static str],
1344+
fluent_resources: Vec<&'static str>,
13451345
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
13461346
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
13471347
target_override: Option<Target>,
@@ -1630,13 +1630,11 @@ pub enum IncrCompSession {
16301630
InvalidBecauseOfErrors { session_directory: PathBuf },
16311631
}
16321632

1633-
// FIXME(#100717): early errors aren't translated at the moment, so this is fine, but it will need
1634-
// to reference every crate that might emit an early error for translation to work.
1635-
static EARLY_ERROR_LOCALE_RESOURCE: &'static [&'static str] =
1636-
&[rustc_errors::DEFAULT_LOCALE_RESOURCE];
1637-
16381633
fn early_error_handler(output: config::ErrorOutputType) -> rustc_errors::Handler {
1639-
let fallback_bundle = fallback_fluent_bundle(EARLY_ERROR_LOCALE_RESOURCE, false);
1634+
// FIXME(#100717): early errors aren't translated at the moment, so this is fine, but it will
1635+
// need to reference every crate that might emit an early error for translation to work.
1636+
let fallback_bundle =
1637+
fallback_fluent_bundle(vec![rustc_errors::DEFAULT_LOCALE_RESOURCE], false);
16401638
let emitter: Box<dyn Emitter + sync::Send> = match output {
16411639
config::ErrorOutputType::HumanReadable(kind) => {
16421640
let (short, color_config) = kind.unzip();

src/librustdoc/clean/render_macro_matchers.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ fn snippet_equal_to_token(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Option<String
6363
let snippet = source_map.span_to_snippet(span).ok()?;
6464

6565
// Create a Parser.
66-
let sess = ParseSess::new(rustc_driver::DEFAULT_LOCALE_RESOURCES, FilePathMapping::empty());
66+
let sess =
67+
ParseSess::new(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), FilePathMapping::empty());
6768
let file_name = source_map.span_to_filename(span);
6869
let mut parser =
6970
match rustc_parse::maybe_new_parser_from_source_str(&sess, file_name, snippet.clone()) {

src/librustdoc/core.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ pub(crate) fn new_handler(
115115
diagnostic_width: Option<usize>,
116116
unstable_opts: &UnstableOptions,
117117
) -> rustc_errors::Handler {
118-
let fallback_bundle =
119-
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES, false);
118+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
119+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
120+
false,
121+
);
120122
let emitter: Box<dyn Emitter + sync::Send> = match error_format {
121123
ErrorOutputType::HumanReadable(kind) => {
122124
let (short, color_config) = kind.unzip();

src/librustdoc/doctest.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,10 @@ pub(crate) fn make_test(
546546
// Any errors in parsing should also appear when the doctest is compiled for real, so just
547547
// send all the errors that librustc_ast emits directly into a `Sink` instead of stderr.
548548
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
549-
let fallback_bundle =
550-
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES, false);
549+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
550+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
551+
false,
552+
);
551553
supports_color = EmitterWriter::stderr(
552554
ColorConfig::Auto,
553555
None,
@@ -742,8 +744,10 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
742744
// Any errors in parsing should also appear when the doctest is compiled for real, so just
743745
// send all the errors that librustc_ast emits directly into a `Sink` instead of stderr.
744746
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
745-
let fallback_bundle =
746-
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES, false);
747+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
748+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
749+
false,
750+
);
747751

748752
let emitter = EmitterWriter::new(
749753
Box::new(io::sink()),

src/librustdoc/passes/lint/check_code_block_syntax.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ fn check_rust_syntax(
3333
code_block: RustCodeBlock,
3434
) {
3535
let buffer = Lrc::new(Lock::new(Buffer::default()));
36-
let fallback_bundle =
37-
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES, false);
36+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
37+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
38+
false,
39+
);
3840
let emitter = BufferEmitter { buffer: Lrc::clone(&buffer), fallback_bundle };
3941

4042
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));

src/tools/clippy/clippy_lints/src/doc.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,10 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
704704
let filename = FileName::anon_source_code(&code);
705705

706706
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
707-
let fallback_bundle =
708-
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES, false);
707+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
708+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
709+
false
710+
);
709711
let emitter = EmitterWriter::new(
710712
Box::new(io::sink()),
711713
None,

src/tools/clippy/src/driver.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
209209
// Separate the output with an empty line
210210
eprintln!();
211211

212-
let fallback_bundle = rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES, false);
212+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
213+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
214+
false
215+
);
213216
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
214217
rustc_errors::ColorConfig::Auto,
215218
None,

src/tools/rustfmt/src/parse/session.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ fn default_handler(
123123
let emitter = if hide_parse_errors {
124124
silent_emitter()
125125
} else {
126-
let fallback_bundle =
127-
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES, false);
126+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
127+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
128+
false,
129+
);
128130
Box::new(EmitterWriter::stderr(
129131
color_cfg,
130132
Some(source_map.clone()),

tests/run-make-fulldeps/hotplug_codegen_backend/the_backend.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use std::any::Any;
2727
struct TheBackend;
2828

2929
impl CodegenBackend for TheBackend {
30+
fn locale_resource(&self) -> &'static str { "" }
31+
3032
fn codegen_crate<'a, 'tcx>(
3133
&self,
3234
tcx: TyCtxt<'tcx>,

tests/ui-fulldeps/mod_dir_path_canonicalized.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ pub fn main() {
3030
assert_eq!(gravy::foo(), 10);
3131
}
3232

33-
static TEST_LOCALE_RESOURCES: &[&str] = &[rustc_parse::DEFAULT_LOCALE_RESOURCE];
34-
3533
fn parse() {
36-
let parse_session = ParseSess::new(TEST_LOCALE_RESOURCES, FilePathMapping::empty());
34+
let parse_session = ParseSess::new(
35+
vec![rustc_parse::DEFAULT_LOCALE_RESOURCE],
36+
FilePathMapping::empty()
37+
);
3738

3839
let path = Path::new(file!());
3940
let path = path.canonicalize().unwrap();

tests/ui-fulldeps/pprust-expr-roundtrip.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,8 @@ fn main() {
219219
rustc_span::create_default_session_globals_then(|| run());
220220
}
221221

222-
static TEST_LOCALE_RESOURCES: &[&str] = &[rustc_parse::DEFAULT_LOCALE_RESOURCE];
223-
224222
fn run() {
225-
let ps = ParseSess::new(TEST_LOCALE_RESOURCES, FilePathMapping::empty());
223+
let ps = ParseSess::new(vec![rustc_parse::DEFAULT_LOCALE_RESOURCE], FilePathMapping::empty());
226224

227225
iter_exprs(2, &mut |mut e| {
228226
// If the pretty printer is correct, then `parse(print(e))` should be identical to `e`,

0 commit comments

Comments
 (0)