Skip to content

Commit 484b612

Browse files
committed
use subdiagnostic for logical negation, bitwise not
1 parent f43562b commit 484b612

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

Diff for: compiler/rustc_error_messages/locales/en-US/parser.ftl

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ parser_tilde_is_not_unary_operator = `~` cannot be used as a unary operator
5555
.suggestion = use `!` to perform bitwise not
5656
5757
parser_unexpected_token_after_not = unexpected {$negated_desc} after identifier
58-
.suggestion = use `!` to perform {$negated_msg}
58+
parser_unexpected_token_after_not_bitwise = use `!` to perform bitwise not
59+
parser_unexpected_token_after_not_logical = use `!` to perform logical negation
60+
parser_unexpected_token_after_not_default = use `!` to perform logical negation or bitwise not
5961
6062
parser_malformed_loop_label = malformed loop label
6163
.suggestion = use the correct loop label format

Diff for: compiler/rustc_parse/src/parser/diagnostics.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,32 @@ pub(crate) struct NotAsNegationOperator {
430430
#[primary_span]
431431
pub negated: Span,
432432
pub negated_desc: String,
433-
pub negated_msg: String,
434-
#[suggestion_short(applicability = "machine-applicable", code = "!")]
435-
pub not: Span,
433+
#[subdiagnostic]
434+
pub sub: NotAsNegationOperatorSub,
435+
}
436+
437+
#[derive(SessionSubdiagnostic)]
438+
pub enum NotAsNegationOperatorSub {
439+
#[suggestion_short(
440+
parser::unexpected_token_after_not_default,
441+
applicability = "machine-applicable",
442+
code = "!"
443+
)]
444+
SuggestNotDefault(#[primary_span] Span),
445+
446+
#[suggestion_short(
447+
parser::unexpected_token_after_not_bitwise,
448+
applicability = "machine-applicable",
449+
code = "!"
450+
)]
451+
SuggestNotBitwise(#[primary_span] Span),
452+
453+
#[suggestion_short(
454+
parser::unexpected_token_after_not_logical,
455+
applicability = "machine-applicable",
456+
code = "!"
457+
)]
458+
SuggestNotLogical(#[primary_span] Span),
436459
}
437460

438461
#[derive(SessionDiagnostic)]

Diff for: compiler/rustc_parse/src/parser/expr.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use super::diagnostics::{
66
InvalidComparisonOperatorSub, InvalidLogicalOperator, InvalidLogicalOperatorSub,
77
LeftArrowOperator, LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath,
88
MalformedLoopLabel, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray,
9-
NotAsNegationOperator, OuterAttributeNotAllowedOnIfElse, RequireColonAfterLabeledExpression,
10-
SnapshotParser, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
9+
NotAsNegationOperator, NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse,
10+
RequireColonAfterLabeledExpression, SnapshotParser, TildeAsUnaryOperator,
11+
UnexpectedTokenAfterLabel,
1112
};
1213
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
1314
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
@@ -660,21 +661,23 @@ impl<'a> Parser<'a> {
660661
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
661662
// Emit the error...
662663
let negated_token = self.look_ahead(1, |t| t.clone());
663-
let negtated_msg = if negated_token.is_numeric_lit() {
664-
"bitwise not"
664+
665+
let sub_diag = if negated_token.is_numeric_lit() {
666+
NotAsNegationOperatorSub::SuggestNotBitwise
665667
} else if negated_token.is_bool_lit() {
666-
"logical negation"
668+
NotAsNegationOperatorSub::SuggestNotLogical
667669
} else {
668-
"logical negation or bitwise not"
670+
NotAsNegationOperatorSub::SuggestNotDefault
669671
};
670672

671673
self.sess.emit_err(NotAsNegationOperator {
672674
negated: negated_token.span,
673675
negated_desc: super::token_descr(&negated_token),
674-
negated_msg: negtated_msg.to_string(),
675676
// Span the `not` plus trailing whitespace to avoid
676677
// trailing whitespace after the `!` in our suggestion
677-
not: self.sess.source_map().span_until_non_whitespace(lo.to(negated_token.span)),
678+
sub: sub_diag(
679+
self.sess.source_map().span_until_non_whitespace(lo.to(negated_token.span)),
680+
),
678681
});
679682

680683
// ...and recover!

0 commit comments

Comments
 (0)