use subdiagnostic for logical negation, bitwise not
This commit is contained in:
parent
f43562b95b
commit
484b612909
3 changed files with 40 additions and 12 deletions
|
@ -55,7 +55,9 @@ parser_tilde_is_not_unary_operator = `~` cannot be used as a unary operator
|
||||||
.suggestion = use `!` to perform bitwise not
|
.suggestion = use `!` to perform bitwise not
|
||||||
|
|
||||||
parser_unexpected_token_after_not = unexpected {$negated_desc} after identifier
|
parser_unexpected_token_after_not = unexpected {$negated_desc} after identifier
|
||||||
.suggestion = use `!` to perform {$negated_msg}
|
parser_unexpected_token_after_not_bitwise = use `!` to perform bitwise not
|
||||||
|
parser_unexpected_token_after_not_logical = use `!` to perform logical negation
|
||||||
|
parser_unexpected_token_after_not_default = use `!` to perform logical negation or bitwise not
|
||||||
|
|
||||||
parser_malformed_loop_label = malformed loop label
|
parser_malformed_loop_label = malformed loop label
|
||||||
.suggestion = use the correct loop label format
|
.suggestion = use the correct loop label format
|
||||||
|
|
|
@ -430,9 +430,32 @@ pub(crate) struct NotAsNegationOperator {
|
||||||
#[primary_span]
|
#[primary_span]
|
||||||
pub negated: Span,
|
pub negated: Span,
|
||||||
pub negated_desc: String,
|
pub negated_desc: String,
|
||||||
pub negated_msg: String,
|
#[subdiagnostic]
|
||||||
#[suggestion_short(applicability = "machine-applicable", code = "!")]
|
pub sub: NotAsNegationOperatorSub,
|
||||||
pub not: Span,
|
}
|
||||||
|
|
||||||
|
#[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)]
|
#[derive(SessionDiagnostic)]
|
||||||
|
|
|
@ -6,8 +6,9 @@ use super::diagnostics::{
|
||||||
InvalidComparisonOperatorSub, InvalidLogicalOperator, InvalidLogicalOperatorSub,
|
InvalidComparisonOperatorSub, InvalidLogicalOperator, InvalidLogicalOperatorSub,
|
||||||
LeftArrowOperator, LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath,
|
LeftArrowOperator, LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath,
|
||||||
MalformedLoopLabel, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray,
|
MalformedLoopLabel, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray,
|
||||||
NotAsNegationOperator, OuterAttributeNotAllowedOnIfElse, RequireColonAfterLabeledExpression,
|
NotAsNegationOperator, NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse,
|
||||||
SnapshotParser, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
|
RequireColonAfterLabeledExpression, SnapshotParser, TildeAsUnaryOperator,
|
||||||
|
UnexpectedTokenAfterLabel,
|
||||||
};
|
};
|
||||||
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
|
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
|
||||||
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
|
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
|
||||||
|
@ -660,21 +661,23 @@ impl<'a> Parser<'a> {
|
||||||
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
|
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
|
||||||
// Emit the error...
|
// Emit the error...
|
||||||
let negated_token = self.look_ahead(1, |t| t.clone());
|
let negated_token = self.look_ahead(1, |t| t.clone());
|
||||||
let negtated_msg = if negated_token.is_numeric_lit() {
|
|
||||||
"bitwise not"
|
let sub_diag = if negated_token.is_numeric_lit() {
|
||||||
|
NotAsNegationOperatorSub::SuggestNotBitwise
|
||||||
} else if negated_token.is_bool_lit() {
|
} else if negated_token.is_bool_lit() {
|
||||||
"logical negation"
|
NotAsNegationOperatorSub::SuggestNotLogical
|
||||||
} else {
|
} else {
|
||||||
"logical negation or bitwise not"
|
NotAsNegationOperatorSub::SuggestNotDefault
|
||||||
};
|
};
|
||||||
|
|
||||||
self.sess.emit_err(NotAsNegationOperator {
|
self.sess.emit_err(NotAsNegationOperator {
|
||||||
negated: negated_token.span,
|
negated: negated_token.span,
|
||||||
negated_desc: super::token_descr(&negated_token),
|
negated_desc: super::token_descr(&negated_token),
|
||||||
negated_msg: negtated_msg.to_string(),
|
|
||||||
// Span the `not` plus trailing whitespace to avoid
|
// Span the `not` plus trailing whitespace to avoid
|
||||||
// trailing whitespace after the `!` in our suggestion
|
// 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!
|
// ...and recover!
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue