Skip to content

Commit 24922b7

Browse files
authored
Rollup merge of #100753 - LuisCardosoOliveira:translation-migrate-session, r=davidtwco
translations(rustc_session): migrates `rustc_session` to use `SessionDiagnostic` - Pt. 1 ## Description This is the first PR for the migration of the module `rustc_session`. You can follow my progress [here](#100717 (comment)). The PR migrates the files `cgu_reuse_tracker` and `parse.rs` to use `SessionDiagnostic `.
2 parents 6c4bda6 + 2c77f3e commit 24922b7

File tree

7 files changed

+110
-19
lines changed

7 files changed

+110
-19
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
18921892
}
18931893
});
18941894

1895-
sess.cgu_reuse_tracker.check_expected_reuse(sess.diagnostic());
1895+
sess.cgu_reuse_tracker.check_expected_reuse(sess);
18961896

18971897
sess.abort_if_errors();
18981898

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
session_incorrect_cgu_reuse_type =
2+
CGU-reuse for `{$cgu_user_name}` is `{$actual_reuse}` but should be {$at_least ->
3+
[one] {"at least "}
4+
*[other] {""}
5+
}`{$expected_reuse}`
6+
7+
session_cgu_not_recorded =
8+
CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded`
9+
10+
session_feature_gate_error = {$explain}
11+
12+
session_feature_diagnostic_for_issue =
13+
see issue #{$n} <https://github.com/rust-lang/rust/issues/{$n}> for more information
14+
15+
session_feature_diagnostic_help =
16+
add `#![feature({$feature})]` to the crate attributes to enable

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fluent_messages! {
4141
const_eval => "../locales/en-US/const_eval.ftl",
4242
driver => "../locales/en-US/driver.ftl",
4343
expand => "../locales/en-US/expand.ftl",
44+
session => "../locales/en-US/session.ftl",
4445
interface => "../locales/en-US/interface.ftl",
4546
infer => "../locales/en-US/infer.ftl",
4647
lint => "../locales/en-US/lint.ftl",

compiler/rustc_session/src/cgu_reuse_tracker.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
//! compilation. This is used for incremental compilation tests and debug
33
//! output.
44
5+
use crate::errors::{CguNotRecorded, IncorrectCguReuseType};
6+
use crate::Session;
57
use rustc_data_structures::fx::FxHashMap;
8+
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
69
use rustc_span::{Span, Symbol};
10+
use std::borrow::Cow;
11+
use std::fmt::{self};
712
use std::sync::{Arc, Mutex};
813
use tracing::debug;
914

@@ -14,6 +19,22 @@ pub enum CguReuse {
1419
PostLto,
1520
}
1621

22+
impl fmt::Display for CguReuse {
23+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24+
match *self {
25+
CguReuse::No => write!(f, "No"),
26+
CguReuse::PreLto => write!(f, "PreLto "),
27+
CguReuse::PostLto => write!(f, "PostLto "),
28+
}
29+
}
30+
}
31+
32+
impl IntoDiagnosticArg for CguReuse {
33+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
34+
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
35+
}
36+
}
37+
1738
#[derive(Copy, Clone, Debug, PartialEq)]
1839
pub enum ComparisonKind {
1940
Exact,
@@ -84,7 +105,7 @@ impl CguReuseTracker {
84105
}
85106
}
86107

87-
pub fn check_expected_reuse(&self, diag: &rustc_errors::Handler) {
108+
pub fn check_expected_reuse(&self, sess: &Session) {
88109
if let Some(ref data) = self.data {
89110
let data = data.lock().unwrap();
90111

@@ -98,19 +119,17 @@ impl CguReuseTracker {
98119
};
99120

100121
if error {
101-
let at_least = if at_least { "at least " } else { "" };
102-
let msg = format!(
103-
"CGU-reuse for `{cgu_user_name}` is `{actual_reuse:?}` but \
104-
should be {at_least}`{expected_reuse:?}`"
105-
);
106-
diag.span_err(error_span.0, &msg);
122+
let at_least = if at_least { 1 } else { 0 };
123+
IncorrectCguReuseType {
124+
span: error_span.0,
125+
cgu_user_name: &cgu_user_name,
126+
actual_reuse,
127+
expected_reuse,
128+
at_least,
129+
};
107130
}
108131
} else {
109-
let msg = format!(
110-
"CGU-reuse for `{cgu_user_name}` (mangled: `{cgu_name}`) was \
111-
not recorded"
112-
);
113-
diag.span_fatal(error_span.0, &msg)
132+
sess.emit_fatal(CguNotRecorded { cgu_user_name, cgu_name });
114133
}
115134
}
116135
}

compiler/rustc_session/src/errors.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::num::NonZeroU32;
2+
3+
use crate as rustc_session;
4+
use crate::cgu_reuse_tracker::CguReuse;
5+
use rustc_errors::MultiSpan;
6+
use rustc_macros::SessionDiagnostic;
7+
use rustc_span::{Span, Symbol};
8+
9+
#[derive(SessionDiagnostic)]
10+
#[diag(session::incorrect_cgu_reuse_type)]
11+
pub struct IncorrectCguReuseType<'a> {
12+
#[primary_span]
13+
pub span: Span,
14+
pub cgu_user_name: &'a str,
15+
pub actual_reuse: CguReuse,
16+
pub expected_reuse: CguReuse,
17+
pub at_least: u8,
18+
}
19+
20+
#[derive(SessionDiagnostic)]
21+
#[diag(session::cgu_not_recorded)]
22+
pub struct CguNotRecorded<'a> {
23+
pub cgu_user_name: &'a str,
24+
pub cgu_name: &'a str,
25+
}
26+
27+
#[derive(SessionDiagnostic)]
28+
#[diag(session::feature_gate_error, code = "E0658")]
29+
pub struct FeatureGateError<'a> {
30+
#[primary_span]
31+
pub span: MultiSpan,
32+
pub explain: &'a str,
33+
}
34+
35+
#[derive(SessionSubdiagnostic)]
36+
#[note(session::feature_diagnostic_for_issue)]
37+
pub struct FeatureDiagnosticForIssue {
38+
pub n: NonZeroU32,
39+
}
40+
41+
#[derive(SessionSubdiagnostic)]
42+
#[help(session::feature_diagnostic_help)]
43+
pub struct FeatureDiagnosticHelp {
44+
pub feature: Symbol,
45+
}

compiler/rustc_session/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#[macro_use]
1414
extern crate rustc_macros;
15+
pub mod errors;
1516

1617
pub mod cgu_reuse_tracker;
1718
pub mod utils;

compiler/rustc_session/src/parse.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! It also serves as an input to the parser itself.
33
44
use crate::config::CheckCfg;
5+
use crate::errors::{FeatureDiagnosticForIssue, FeatureDiagnosticHelp, FeatureGateError};
56
use crate::lint::{
67
builtin::UNSTABLE_SYNTAX_PRE_EXPANSION, BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId,
78
};
@@ -11,7 +12,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1112
use rustc_data_structures::sync::{Lock, Lrc};
1213
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
1314
use rustc_errors::{
14-
error_code, fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId,
15+
fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId,
1516
DiagnosticMessage, EmissionGuarantee, ErrorGuaranteed, MultiSpan, StashKey,
1617
};
1718
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
@@ -112,7 +113,7 @@ pub fn feature_err_issue<'a>(
112113
.map(|err| err.cancel());
113114
}
114115

115-
let mut err = sess.span_diagnostic.struct_span_err_with_code(span, explain, error_code!(E0658));
116+
let mut err = sess.create_err(FeatureGateError { span, explain });
116117
add_feature_diagnostics_for_issue(&mut err, sess, feature, issue);
117118
err
118119
}
@@ -130,6 +131,8 @@ pub fn feature_warn<'a>(sess: &'a ParseSess, feature: Symbol, span: Span, explai
130131
///
131132
/// This variant allows you to control whether it is a library or language feature.
132133
/// Almost always, you want to use this for a language feature. If so, prefer `feature_warn`.
134+
#[allow(rustc::diagnostic_outside_of_impl)]
135+
#[allow(rustc::untranslatable_diagnostic)]
133136
pub fn feature_warn_issue<'a>(
134137
sess: &'a ParseSess,
135138
feature: Symbol,
@@ -172,14 +175,12 @@ pub fn add_feature_diagnostics_for_issue<'a>(
172175
issue: GateIssue,
173176
) {
174177
if let Some(n) = find_feature_issue(feature, issue) {
175-
err.note(&format!(
176-
"see issue #{n} <https://github.com/rust-lang/rust/issues/{n}> for more information"
177-
));
178+
err.subdiagnostic(FeatureDiagnosticForIssue { n });
178179
}
179180

180181
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
181182
if sess.unstable_features.is_nightly_build() {
182-
err.help(&format!("add `#![feature({feature})]` to the crate attributes to enable"));
183+
err.subdiagnostic(FeatureDiagnosticHelp { feature });
183184
}
184185
}
185186

@@ -372,6 +373,8 @@ impl ParseSess {
372373
}
373374

374375
#[rustc_lint_diagnostics]
376+
#[allow(rustc::diagnostic_outside_of_impl)]
377+
#[allow(rustc::untranslatable_diagnostic)]
375378
pub fn struct_err(
376379
&self,
377380
msg: impl Into<DiagnosticMessage>,
@@ -380,16 +383,22 @@ impl ParseSess {
380383
}
381384

382385
#[rustc_lint_diagnostics]
386+
#[allow(rustc::diagnostic_outside_of_impl)]
387+
#[allow(rustc::untranslatable_diagnostic)]
383388
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
384389
self.span_diagnostic.struct_warn(msg)
385390
}
386391

387392
#[rustc_lint_diagnostics]
393+
#[allow(rustc::diagnostic_outside_of_impl)]
394+
#[allow(rustc::untranslatable_diagnostic)]
388395
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
389396
self.span_diagnostic.struct_fatal(msg)
390397
}
391398

392399
#[rustc_lint_diagnostics]
400+
#[allow(rustc::diagnostic_outside_of_impl)]
401+
#[allow(rustc::untranslatable_diagnostic)]
393402
pub fn struct_diagnostic<G: EmissionGuarantee>(
394403
&self,
395404
msg: impl Into<DiagnosticMessage>,

0 commit comments

Comments
 (0)