Skip to content

Commit d5262a9

Browse files
committed
translations(rustc_session): migrate 80% of the file parse.rs
This commit migrates around 80% of the parse file to use SsessionDiagnostic We still have to migrate struct_err and struct_warn.
1 parent 706452e commit d5262a9

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
incorrect_cgu_reuse_type =
1+
session_incorrect_cgu_reuse_type =
22
CGU-reuse for `{$cgu_user_name}` is `{$actual_reuse}` but should be `{$at_least}``${expected_reuse}`
33

4-
cgu_not_recorded =
5-
CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded`
4+
session_cgu_not_recorded =
5+
CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded`
6+
7+
session_feature_gate_error = {$explain}
8+
9+
session_feature_diagnostic_for_issue =
10+
see issue #{$n} <https://github.com/rust-lang/rust/issues/{$n}> for more information
11+
12+
session_feature_diagnostic_help =
13+
add `#![feature({$feature})]` to the crate attributes to enable

compiler/rustc_session/src/cgu_reuse_tracker.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! output.
44
55
use crate::errors::IncorrectCguReuseType;
6-
// use crate::errors::{CguNotRecorded, IncorrectCguReuseType};
76
use rustc_data_structures::fx::FxHashMap;
87
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
98
use rustc_span::{Span, Symbol};
@@ -129,11 +128,14 @@ impl CguReuseTracker {
129128
};
130129
}
131130
} else {
131+
//FIXME: Remove this once PR #100694 that implements `[fatal(..)]` is merged
132132
let msg = format!(
133133
"CGU-reuse for `{cgu_user_name}` (mangled: `{cgu_name}`) was \
134134
not recorded"
135135
);
136136
diag.span_fatal(error_span.0, &msg)
137+
138+
//FIXME: Uncomment this once PR #100694 that implements `[fatal(..)]` is merged
137139
// CguNotRecorded { cgu_user_name, cgu_name };
138140
}
139141
}

compiler/rustc_session/src/errors.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use std::num::NonZeroU32;
2+
13
use crate as rustc_session;
24
use crate::cgu_reuse_tracker::CguReuse;
5+
use rustc_errors::MultiSpan;
36
use rustc_macros::SessionDiagnostic;
4-
use rustc_span::Span;
7+
use rustc_span::{Span, Symbol};
58

69
#[derive(SessionDiagnostic)]
7-
#[error(session::incorrect_cgu_reuse_type)]
10+
#[diag(session::incorrect_cgu_reuse_type)]
811
pub struct IncorrectCguReuseType<'a> {
912
#[primary_span]
1013
pub span: Span,
@@ -14,9 +17,30 @@ pub struct IncorrectCguReuseType<'a> {
1417
pub at_least: &'a str,
1518
}
1619

20+
//FIXME: Uncomment this once PR #100694 that implements `[fatal(..)]` is merged
1721
// #[derive(SessionDiagnostic)]
1822
// #[fatal(session::cgu_not_recorded)]
1923
// pub struct CguNotRecorded<'a> {
2024
// pub cgu_user_name: &'a str,
2125
// pub cgu_name: &'a str,
2226
// }
27+
28+
#[derive(SessionDiagnostic)]
29+
#[diag(session::feature_gate_error, code = "E0658")]
30+
pub struct FeatureGateError<'a> {
31+
#[primary_span]
32+
pub span: MultiSpan,
33+
pub explain: &'a str,
34+
}
35+
36+
#[derive(SessionSubdiagnostic)]
37+
#[note(session::feature_diagnostic_for_issue)]
38+
pub struct FeatureDiagnosticForIssue {
39+
pub n: NonZeroU32,
40+
}
41+
42+
#[derive(SessionSubdiagnostic)]
43+
#[help(session::feature_diagnostic_help)]
44+
pub struct FeatureDiagnosticHelp {
45+
pub feature: Symbol,
46+
}

compiler/rustc_session/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#![feature(map_many_mut)]
99
#![recursion_limit = "256"]
1010
#![allow(rustc::potential_query_instability)]
11-
#![deny(rustc::untranslatable_diagnostic)]
12-
#![deny(rustc::diagnostic_outside_of_impl)]
1311

1412
#[macro_use]
1513
extern crate rustc_macros;

compiler/rustc_session/src/parse.rs

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