Skip to content

Commit 9146879

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 <david.wood@huawei.com>
1 parent 8e0db2d commit 9146879

File tree

23 files changed

+86
-48
lines changed

23 files changed

+86
-48
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 12 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)