Skip to content

Commit 495e271

Browse files
committed
Migrate rustc_session::expr_parentheses_needed to Subdiagnostic struct
1 parent 57ee5cf commit 495e271

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has
6666
session_crate_name_empty = crate name must not be empty
6767
6868
session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`
69+
70+
session_expr_parentheses_needed = parentheses are required to parse this as an expression

compiler/rustc_session/src/errors.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,18 @@ impl IntoDiagnostic<'_> for InvalidCharacterInCrateName<'_> {
219219
diag
220220
}
221221
}
222+
223+
#[derive(Subdiagnostic)]
224+
#[multipart_suggestion(session::expr_parentheses_needed, applicability = "machine-applicable")]
225+
pub struct ExprParenthesesNeeded {
226+
#[suggestion_part(code = "(")]
227+
pub left: Span,
228+
#[suggestion_part(code = ")")]
229+
pub right: Span,
230+
}
231+
232+
impl ExprParenthesesNeeded {
233+
pub fn surrounding(s: Span) -> Self {
234+
ExprParenthesesNeeded { left: s.shrink_to_lo(), right: s.shrink_to_hi() }
235+
}
236+
}

compiler/rustc_session/src/parse.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
//! It also serves as an input to the parser itself.
33
44
use crate::config::CheckCfg;
5-
use crate::errors::{FeatureDiagnosticForIssue, FeatureDiagnosticHelp, FeatureGateError};
5+
use crate::errors::{
6+
ExprParenthesesNeeded, FeatureDiagnosticForIssue, FeatureDiagnosticHelp, FeatureGateError,
7+
};
68
use crate::lint::{
79
builtin::UNSTABLE_SYNTAX_PRE_EXPANSION, BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId,
810
};
@@ -11,7 +13,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
1113
use rustc_data_structures::sync::{Lock, Lrc};
1214
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
1315
use rustc_errors::{
14-
fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId,
16+
fallback_fluent_bundle, AddToDiagnostic, Diagnostic, DiagnosticBuilder, DiagnosticId,
1517
DiagnosticMessage, EmissionGuarantee, ErrorGuaranteed, IntoDiagnostic, MultiSpan, StashKey,
1618
};
1719
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
@@ -325,11 +327,7 @@ impl ParseSess {
325327
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
326328
/// parser to continue parsing the following operation as part of the same expression.
327329
pub fn expr_parentheses_needed(&self, err: &mut Diagnostic, span: Span) {
328-
err.multipart_suggestion(
329-
"parentheses are required to parse this as an expression",
330-
vec![(span.shrink_to_lo(), "(".to_string()), (span.shrink_to_hi(), ")".to_string())],
331-
Applicability::MachineApplicable,
332-
);
330+
ExprParenthesesNeeded::surrounding(span).add_to_diagnostic(err);
333331
}
334332

335333
pub fn save_proc_macro_span(&self, span: Span) -> usize {

0 commit comments

Comments
 (0)