Skip to content

Commit 7dbc04d

Browse files
committed
rustc_session: turn feature gate subdiagnostic into struct
1 parent 2e5193b commit 7dbc04d

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

compiler/rustc_session/src/errors.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for FeatureGateError {
2626
}
2727
}
2828

29+
#[derive(Subdiagnostic)]
30+
pub struct FeatureGateSubdiagnostic {
31+
#[subdiagnostic]
32+
pub(crate) issue: Option<FeatureDiagnosticForIssue>,
33+
#[subdiagnostic]
34+
pub(crate) enable_feature: Option<EnableFeatureSubdiagnostic>,
35+
#[subdiagnostic]
36+
pub(crate) upgrade_compiler: Option<SuggestUpgradeCompiler>,
37+
}
38+
2939
#[derive(Subdiagnostic)]
3040
#[note(session_feature_diagnostic_for_issue)]
3141
pub(crate) struct FeatureDiagnosticForIssue {
@@ -51,27 +61,21 @@ impl SuggestUpgradeCompiler {
5161
}
5262

5363
#[derive(Subdiagnostic)]
54-
#[help(session_feature_diagnostic_help)]
55-
pub(crate) struct FeatureDiagnosticHelp {
56-
pub(crate) feature: Symbol,
57-
}
58-
59-
#[derive(Subdiagnostic)]
60-
#[suggestion(
61-
session_feature_diagnostic_suggestion,
62-
applicability = "maybe-incorrect",
63-
code = "#![feature({feature})]\n"
64-
)]
65-
pub struct FeatureDiagnosticSuggestion {
66-
pub feature: Symbol,
67-
#[primary_span]
68-
pub span: Span,
69-
}
70-
71-
#[derive(Subdiagnostic)]
72-
#[help(session_cli_feature_diagnostic_help)]
73-
pub(crate) struct CliFeatureDiagnosticHelp {
74-
pub(crate) feature: Symbol,
64+
pub(crate) enum EnableFeatureSubdiagnostic {
65+
#[help(session_feature_diagnostic_help)]
66+
AddAttrHelp { feature: Symbol },
67+
#[suggestion(
68+
session_feature_diagnostic_suggestion,
69+
applicability = "maybe-incorrect",
70+
code = "#![feature({feature})]\n"
71+
)]
72+
AddAttrSuggestion {
73+
feature: Symbol,
74+
#[primary_span]
75+
span: Span,
76+
},
77+
#[help(session_cli_feature_diagnostic_help)]
78+
AddCliHelp { feature: Symbol },
7579
}
7680

7781
#[derive(Diagnostic)]

compiler/rustc_session/src/parse.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rustc_span::{Span, Symbol};
2020

2121
use crate::config::{Cfg, CheckCfg};
2222
use crate::errors::{
23-
CliFeatureDiagnosticHelp, FeatureDiagnosticForIssue, FeatureDiagnosticHelp,
24-
FeatureDiagnosticSuggestion, FeatureGateError, SuggestUpgradeCompiler,
23+
EnableFeatureSubdiagnostic, FeatureDiagnosticForIssue, FeatureGateError,
24+
FeatureGateSubdiagnostic, SuggestUpgradeCompiler,
2525
};
2626
use crate::lint::builtin::UNSTABLE_SYNTAX_PRE_EXPANSION;
2727
use crate::lint::{BufferedEarlyLint, BuiltinLintDiag, Lint, LintId};
@@ -177,26 +177,32 @@ pub fn add_feature_diagnostics_for_issue<G: EmissionGuarantee>(
177177
feature_from_cli: bool,
178178
inject_span: Option<Span>,
179179
) {
180-
if let Some(n) = find_feature_issue(feature, issue) {
181-
err.subdiagnostic(FeatureDiagnosticForIssue { n });
182-
}
180+
let issue = find_feature_issue(feature, issue).map(|n| FeatureDiagnosticForIssue { n });
183181

184182
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
185-
if sess.psess.unstable_features.is_nightly_build() {
186-
if feature_from_cli {
187-
err.subdiagnostic(CliFeatureDiagnosticHelp { feature });
183+
let (enable_feature, upgrade_compiler) = if sess.psess.unstable_features.is_nightly_build() {
184+
let enable_feature = if feature_from_cli {
185+
EnableFeatureSubdiagnostic::AddCliHelp { feature }
188186
} else if let Some(span) = inject_span {
189-
err.subdiagnostic(FeatureDiagnosticSuggestion { feature, span });
187+
EnableFeatureSubdiagnostic::AddAttrSuggestion { feature, span }
190188
} else {
191-
err.subdiagnostic(FeatureDiagnosticHelp { feature });
192-
}
189+
EnableFeatureSubdiagnostic::AddAttrHelp { feature }
190+
};
193191

194-
if sess.opts.unstable_opts.ui_testing {
195-
err.subdiagnostic(SuggestUpgradeCompiler::ui_testing());
192+
let upgrade_compiler = if sess.opts.unstable_opts.ui_testing {
193+
Some(SuggestUpgradeCompiler::ui_testing())
196194
} else if let Some(suggestion) = SuggestUpgradeCompiler::new() {
197-
err.subdiagnostic(suggestion);
198-
}
199-
}
195+
Some(suggestion)
196+
} else {
197+
None
198+
};
199+
200+
(Some(enable_feature), upgrade_compiler)
201+
} else {
202+
(None, None)
203+
};
204+
205+
err.subdiagnostic(FeatureGateSubdiagnostic { issue, upgrade_compiler, enable_feature });
200206
}
201207

202208
/// Info about a parsing session.

0 commit comments

Comments
 (0)