Skip to content

Commit 7c52e14

Browse files
authored
Rollup merge of rust-lang#101266 - LuisCardosoOliveira:translation-rustcsession-pt3, r=davidtwco
translations(rustc_session): migrates rustc_session to use SessionDiagnostic - Final # Description This is the final part of the rustc_session rust-lang#100717 (comment). Please only review this [commit](rust-lang@a545347). The other ones are from the PR rust-lang#101041 that is not yet merged. In this PR, we migrate the file `output.rs`
2 parents 5338f5f + 329d501 commit 7c52e14

File tree

6 files changed

+118
-26
lines changed

6 files changed

+118
-26
lines changed

compiler/rustc_error_messages/locales/en-US/session.ftl

+10
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ session_target_invalid_bits_size = {$err}
5656
session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
5757
5858
session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform
59+
60+
session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions
61+
62+
session_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$s}` != `{$name}`
63+
64+
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
65+
66+
session_crate_name_empty = crate name must not be empty
67+
68+
session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`

compiler/rustc_errors/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ rustc_serialize = { path = "../rustc_serialize" }
1313
rustc_span = { path = "../rustc_span" }
1414
rustc_macros = { path = "../rustc_macros" }
1515
rustc_data_structures = { path = "../rustc_data_structures" }
16+
rustc_target = { path = "../rustc_target" }
1617
rustc_hir = { path = "../rustc_hir" }
1718
rustc_lint_defs = { path = "../rustc_lint_defs" }
18-
rustc_target = { path = "../rustc_target" }
1919
unicode-width = "0.1.4"
2020
atty = "0.2"
2121
termcolor = "1.0"

compiler/rustc_session/src/errors.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::num::NonZeroU32;
22

33
use crate::cgu_reuse_tracker::CguReuse;
44
use crate::{self as rustc_session, SessionDiagnostic};
5-
use rustc_errors::{fluent, DiagnosticBuilder, Handler, MultiSpan};
5+
use rustc_errors::{fluent, DiagnosticBuilder, ErrorGuaranteed, Handler, MultiSpan};
66
use rustc_macros::SessionDiagnostic;
77
use rustc_span::{Span, Symbol};
88
use rustc_target::abi::TargetDataLayoutErrors;
@@ -170,3 +170,52 @@ pub struct StackProtectorNotSupportedForTarget<'a> {
170170
pub struct SplitDebugInfoUnstablePlatform {
171171
pub debuginfo: SplitDebuginfo,
172172
}
173+
174+
#[derive(SessionDiagnostic)]
175+
#[diag(session::file_is_not_writeable)]
176+
pub struct FileIsNotWriteable<'a> {
177+
pub file: &'a std::path::Path,
178+
}
179+
180+
#[derive(SessionDiagnostic)]
181+
#[diag(session::crate_name_does_not_match)]
182+
pub struct CrateNameDoesNotMatch<'a> {
183+
#[primary_span]
184+
pub span: Span,
185+
pub s: &'a str,
186+
pub name: Symbol,
187+
}
188+
189+
#[derive(SessionDiagnostic)]
190+
#[diag(session::crate_name_invalid)]
191+
pub struct CrateNameInvalid<'a> {
192+
pub s: &'a str,
193+
}
194+
195+
#[derive(SessionDiagnostic)]
196+
#[diag(session::crate_name_empty)]
197+
pub struct CrateNameEmpty {
198+
#[primary_span]
199+
pub span: Option<Span>,
200+
}
201+
202+
pub struct InvalidCharacterInCrateName<'a> {
203+
pub span: Option<Span>,
204+
pub character: char,
205+
pub crate_name: &'a str,
206+
}
207+
208+
impl crate::SessionDiagnostic<'_> for InvalidCharacterInCrateName<'_> {
209+
fn into_diagnostic(
210+
self,
211+
sess: &Handler,
212+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
213+
let mut diag = sess.struct_err(fluent::session::invalid_character_in_create_name);
214+
if let Some(sp) = self.span {
215+
diag.set_span(sp);
216+
}
217+
diag.set_arg("character", self.character);
218+
diag.set_arg("crate_name", self.crate_name);
219+
diag
220+
}
221+
}

compiler/rustc_session/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#![feature(map_many_mut)]
1010
#![recursion_limit = "256"]
1111
#![allow(rustc::potential_query_instability)]
12+
#![deny(rustc::untranslatable_diagnostic)]
13+
#![deny(rustc::diagnostic_outside_of_impl)]
1214

1315
#[macro_use]
1416
extern crate rustc_macros;

compiler/rustc_session/src/output.rs

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! Related to out filenames of compilation (e.g. save analysis, binaries).
22
use crate::config::{CrateType, Input, OutputFilenames, OutputType};
3+
use crate::errors::{
4+
CrateNameDoesNotMatch, CrateNameEmpty, CrateNameInvalid, FileIsNotWriteable,
5+
InvalidCharacterInCrateName,
6+
};
37
use crate::Session;
48
use rustc_ast as ast;
59
use rustc_span::symbol::sym;
@@ -30,11 +34,7 @@ pub fn out_filename(
3034
/// read-only file. We should be consistent.
3135
pub fn check_file_is_writeable(file: &Path, sess: &Session) {
3236
if !is_writeable(file) {
33-
sess.fatal(&format!(
34-
"output file {} is not writeable -- check its \
35-
permissions",
36-
file.display()
37-
));
37+
sess.emit_fatal(FileIsNotWriteable { file });
3838
}
3939
}
4040

@@ -61,11 +61,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
6161
if let Some(ref s) = sess.opts.crate_name {
6262
if let Some((attr, name)) = attr_crate_name {
6363
if name.as_str() != s {
64-
let msg = format!(
65-
"`--crate-name` and `#[crate_name]` are \
66-
required to match, but `{s}` != `{name}`"
67-
);
68-
sess.span_err(attr.span, &msg);
64+
sess.emit_err(CrateNameDoesNotMatch { span: attr.span, s, name });
6965
}
7066
}
7167
return validate(s.clone(), None);
@@ -77,11 +73,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
7773
if let Input::File(ref path) = *input {
7874
if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
7975
if s.starts_with('-') {
80-
let msg = format!(
81-
"crate names cannot start with a `-`, but \
82-
`{s}` has a leading hyphen"
83-
);
84-
sess.err(&msg);
76+
sess.emit_err(CrateNameInvalid { s });
8577
} else {
8678
return validate(s.replace('-', "_"), None);
8779
}
@@ -94,15 +86,9 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
9486
pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
9587
let mut err_count = 0;
9688
{
97-
let mut say = |s: &str| {
98-
match sp {
99-
Some(sp) => sess.span_err(sp, s),
100-
None => sess.err(s),
101-
};
102-
err_count += 1;
103-
};
10489
if s.is_empty() {
105-
say("crate name must not be empty");
90+
err_count += 1;
91+
sess.emit_err(CrateNameEmpty { span: sp });
10692
}
10793
for c in s.chars() {
10894
if c.is_alphanumeric() {
@@ -111,7 +97,8 @@ pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
11197
if c == '_' {
11298
continue;
11399
}
114-
say(&format!("invalid character `{c}` in crate name: `{s}`"));
100+
err_count += 1;
101+
sess.emit_err(InvalidCharacterInCrateName { span: sp, character: c, crate_name: s });
115102
}
116103
}
117104

compiler/rustc_session/src/session.rs

+44
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ impl Session {
297297
}
298298

299299
#[rustc_lint_diagnostics]
300+
#[allow(rustc::untranslatable_diagnostic)]
301+
#[allow(rustc::diagnostic_outside_of_impl)]
300302
pub fn struct_span_warn<S: Into<MultiSpan>>(
301303
&self,
302304
sp: S,
@@ -305,6 +307,8 @@ impl Session {
305307
self.diagnostic().struct_span_warn(sp, msg)
306308
}
307309
#[rustc_lint_diagnostics]
310+
#[allow(rustc::untranslatable_diagnostic)]
311+
#[allow(rustc::diagnostic_outside_of_impl)]
308312
pub fn struct_span_warn_with_expectation<S: Into<MultiSpan>>(
309313
&self,
310314
sp: S,
@@ -314,6 +318,8 @@ impl Session {
314318
self.diagnostic().struct_span_warn_with_expectation(sp, msg, id)
315319
}
316320
#[rustc_lint_diagnostics]
321+
#[allow(rustc::untranslatable_diagnostic)]
322+
#[allow(rustc::diagnostic_outside_of_impl)]
317323
pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
318324
&self,
319325
sp: S,
@@ -323,10 +329,14 @@ impl Session {
323329
self.diagnostic().struct_span_warn_with_code(sp, msg, code)
324330
}
325331
#[rustc_lint_diagnostics]
332+
#[allow(rustc::untranslatable_diagnostic)]
333+
#[allow(rustc::diagnostic_outside_of_impl)]
326334
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
327335
self.diagnostic().struct_warn(msg)
328336
}
329337
#[rustc_lint_diagnostics]
338+
#[allow(rustc::untranslatable_diagnostic)]
339+
#[allow(rustc::diagnostic_outside_of_impl)]
330340
pub fn struct_warn_with_expectation(
331341
&self,
332342
msg: impl Into<DiagnosticMessage>,
@@ -335,6 +345,8 @@ impl Session {
335345
self.diagnostic().struct_warn_with_expectation(msg, id)
336346
}
337347
#[rustc_lint_diagnostics]
348+
#[allow(rustc::untranslatable_diagnostic)]
349+
#[allow(rustc::diagnostic_outside_of_impl)]
338350
pub fn struct_span_allow<S: Into<MultiSpan>>(
339351
&self,
340352
sp: S,
@@ -343,10 +355,14 @@ impl Session {
343355
self.diagnostic().struct_span_allow(sp, msg)
344356
}
345357
#[rustc_lint_diagnostics]
358+
#[allow(rustc::untranslatable_diagnostic)]
359+
#[allow(rustc::diagnostic_outside_of_impl)]
346360
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
347361
self.diagnostic().struct_allow(msg)
348362
}
349363
#[rustc_lint_diagnostics]
364+
#[allow(rustc::untranslatable_diagnostic)]
365+
#[allow(rustc::diagnostic_outside_of_impl)]
350366
pub fn struct_expect(
351367
&self,
352368
msg: impl Into<DiagnosticMessage>,
@@ -355,6 +371,8 @@ impl Session {
355371
self.diagnostic().struct_expect(msg, id)
356372
}
357373
#[rustc_lint_diagnostics]
374+
#[allow(rustc::untranslatable_diagnostic)]
375+
#[allow(rustc::diagnostic_outside_of_impl)]
358376
pub fn struct_span_err<S: Into<MultiSpan>>(
359377
&self,
360378
sp: S,
@@ -363,6 +381,8 @@ impl Session {
363381
self.diagnostic().struct_span_err(sp, msg)
364382
}
365383
#[rustc_lint_diagnostics]
384+
#[allow(rustc::untranslatable_diagnostic)]
385+
#[allow(rustc::diagnostic_outside_of_impl)]
366386
pub fn struct_span_err_with_code<S: Into<MultiSpan>>(
367387
&self,
368388
sp: S,
@@ -373,13 +393,17 @@ impl Session {
373393
}
374394
// FIXME: This method should be removed (every error should have an associated error code).
375395
#[rustc_lint_diagnostics]
396+
#[allow(rustc::untranslatable_diagnostic)]
397+
#[allow(rustc::diagnostic_outside_of_impl)]
376398
pub fn struct_err(
377399
&self,
378400
msg: impl Into<DiagnosticMessage>,
379401
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
380402
self.parse_sess.struct_err(msg)
381403
}
382404
#[rustc_lint_diagnostics]
405+
#[allow(rustc::untranslatable_diagnostic)]
406+
#[allow(rustc::diagnostic_outside_of_impl)]
383407
pub fn struct_err_with_code(
384408
&self,
385409
msg: impl Into<DiagnosticMessage>,
@@ -388,6 +412,8 @@ impl Session {
388412
self.diagnostic().struct_err_with_code(msg, code)
389413
}
390414
#[rustc_lint_diagnostics]
415+
#[allow(rustc::untranslatable_diagnostic)]
416+
#[allow(rustc::diagnostic_outside_of_impl)]
391417
pub fn struct_warn_with_code(
392418
&self,
393419
msg: impl Into<DiagnosticMessage>,
@@ -396,6 +422,8 @@ impl Session {
396422
self.diagnostic().struct_warn_with_code(msg, code)
397423
}
398424
#[rustc_lint_diagnostics]
425+
#[allow(rustc::untranslatable_diagnostic)]
426+
#[allow(rustc::diagnostic_outside_of_impl)]
399427
pub fn struct_span_fatal<S: Into<MultiSpan>>(
400428
&self,
401429
sp: S,
@@ -404,6 +432,8 @@ impl Session {
404432
self.diagnostic().struct_span_fatal(sp, msg)
405433
}
406434
#[rustc_lint_diagnostics]
435+
#[allow(rustc::untranslatable_diagnostic)]
436+
#[allow(rustc::diagnostic_outside_of_impl)]
407437
pub fn struct_span_fatal_with_code<S: Into<MultiSpan>>(
408438
&self,
409439
sp: S,
@@ -413,15 +443,21 @@ impl Session {
413443
self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
414444
}
415445
#[rustc_lint_diagnostics]
446+
#[allow(rustc::untranslatable_diagnostic)]
447+
#[allow(rustc::diagnostic_outside_of_impl)]
416448
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
417449
self.diagnostic().struct_fatal(msg)
418450
}
419451

420452
#[rustc_lint_diagnostics]
453+
#[allow(rustc::untranslatable_diagnostic)]
454+
#[allow(rustc::diagnostic_outside_of_impl)]
421455
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: impl Into<DiagnosticMessage>) -> ! {
422456
self.diagnostic().span_fatal(sp, msg)
423457
}
424458
#[rustc_lint_diagnostics]
459+
#[allow(rustc::untranslatable_diagnostic)]
460+
#[allow(rustc::diagnostic_outside_of_impl)]
425461
pub fn span_fatal_with_code<S: Into<MultiSpan>>(
426462
&self,
427463
sp: S,
@@ -431,10 +467,14 @@ impl Session {
431467
self.diagnostic().span_fatal_with_code(sp, msg, code)
432468
}
433469
#[rustc_lint_diagnostics]
470+
#[allow(rustc::untranslatable_diagnostic)]
471+
#[allow(rustc::diagnostic_outside_of_impl)]
434472
pub fn fatal(&self, msg: impl Into<DiagnosticMessage>) -> ! {
435473
self.diagnostic().fatal(msg).raise()
436474
}
437475
#[rustc_lint_diagnostics]
476+
#[allow(rustc::untranslatable_diagnostic)]
477+
#[allow(rustc::diagnostic_outside_of_impl)]
438478
pub fn span_err_or_warn<S: Into<MultiSpan>>(
439479
&self,
440480
is_warning: bool,
@@ -448,6 +488,8 @@ impl Session {
448488
}
449489
}
450490
#[rustc_lint_diagnostics]
491+
#[allow(rustc::untranslatable_diagnostic)]
492+
#[allow(rustc::diagnostic_outside_of_impl)]
451493
pub fn span_err<S: Into<MultiSpan>>(
452494
&self,
453495
sp: S,
@@ -456,6 +498,8 @@ impl Session {
456498
self.diagnostic().span_err(sp, msg)
457499
}
458500
#[rustc_lint_diagnostics]
501+
#[allow(rustc::untranslatable_diagnostic)]
502+
#[allow(rustc::diagnostic_outside_of_impl)]
459503
pub fn span_err_with_code<S: Into<MultiSpan>>(
460504
&self,
461505
sp: S,

0 commit comments

Comments
 (0)