Auto merge of #101846 - chenyukang:fix-101793, r=davidtwco

Fix the wording of help msg for bitwise not

Fixes #101793
This commit is contained in:
bors 2022-09-21 12:32:54 +00:00
commit b79b7d8b4e
9 changed files with 109 additions and 29 deletions

View file

@ -430,8 +430,32 @@ pub(crate) struct NotAsNegationOperator {
#[primary_span]
pub negated: Span,
pub negated_desc: String,
#[suggestion_short(applicability = "machine-applicable", code = "!")]
pub not: Span,
#[subdiagnostic]
pub sub: NotAsNegationOperatorSub,
}
#[derive(SessionSubdiagnostic)]
pub enum NotAsNegationOperatorSub {
#[suggestion_short(
parser::unexpected_token_after_not_default,
applicability = "machine-applicable",
code = "!"
)]
SuggestNotDefault(#[primary_span] Span),
#[suggestion_short(
parser::unexpected_token_after_not_bitwise,
applicability = "machine-applicable",
code = "!"
)]
SuggestNotBitwise(#[primary_span] Span),
#[suggestion_short(
parser::unexpected_token_after_not_logical,
applicability = "machine-applicable",
code = "!"
)]
SuggestNotLogical(#[primary_span] Span),
}
#[derive(SessionDiagnostic)]

View file

@ -6,8 +6,9 @@ use super::diagnostics::{
InvalidComparisonOperatorSub, InvalidLogicalOperator, InvalidLogicalOperatorSub,
LeftArrowOperator, LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath,
MalformedLoopLabel, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray,
NotAsNegationOperator, OuterAttributeNotAllowedOnIfElse, RequireColonAfterLabeledExpression,
SnapshotParser, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
NotAsNegationOperator, NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse,
RequireColonAfterLabeledExpression, SnapshotParser, TildeAsUnaryOperator,
UnexpectedTokenAfterLabel,
};
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
@ -660,12 +661,23 @@ impl<'a> Parser<'a> {
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
// Emit the error...
let negated_token = self.look_ahead(1, |t| t.clone());
let sub_diag = if negated_token.is_numeric_lit() {
NotAsNegationOperatorSub::SuggestNotBitwise
} else if negated_token.is_bool_lit() {
NotAsNegationOperatorSub::SuggestNotLogical
} else {
NotAsNegationOperatorSub::SuggestNotDefault
};
self.sess.emit_err(NotAsNegationOperator {
negated: negated_token.span,
negated_desc: super::token_descr(&negated_token),
// Span the `not` plus trailing whitespace to avoid
// trailing whitespace after the `!` in our suggestion
not: self.sess.source_map().span_until_non_whitespace(lo.to(negated_token.span)),
sub: sub_diag(
self.sess.source_map().span_until_non_whitespace(lo.to(negated_token.span)),
),
});
// ...and recover!