Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the wording of help msg for bitwise not #101846

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/rustc_error_messages/locales/en-US/parser.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ parser_invalid_comparison_operator = invalid comparison operator `{$invalid}`
.spaceship_operator_invalid = `<=>` is not a valid comparison operator, use `std::cmp::Ordering`

parser_invalid_logical_operator = `{$incorrect}` is not a logical operator
.note = unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
.note = unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction

parser_tilde_is_not_unary_operator = `~` cannot be used as a unary operator
.suggestion = use `!` to perform bitwise not

parser_unexpected_token_after_not = unexpected {$negated_desc} after identifier
.suggestion = use `!` to perform logical negation
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
.suggestion = use the correct loop label format
Expand Down
28 changes: 26 additions & 2 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
18 changes: 15 additions & 3 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

fn main() {
let _x = !1; //~ ERROR cannot be used as a unary operator
let _y = !1; //~ ERROR unexpected `1` after identifier
let _z = !false; //~ ERROR unexpected keyword `false` after identifier
let _a = !true; //~ ERROR unexpected keyword `true` after identifier
let v = 1 + 2;
let _v = !v; //~ ERROR unexpected `v` after identifier
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

fn main() {
let _x = ~1; //~ ERROR cannot be used as a unary operator
let _y = not 1; //~ ERROR unexpected `1` after identifier
let _z = not false; //~ ERROR unexpected keyword `false` after identifier
let _a = not true; //~ ERROR unexpected keyword `true` after identifier
let v = 1 + 2;
let _v = not v; //~ ERROR unexpected `v` after identifier
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,37 @@ error: `~` cannot be used as a unary operator
LL | let _x = ~1;
| ^ help: use `!` to perform bitwise not

error: aborting due to previous error
error: unexpected `1` after identifier
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:5:18
|
LL | let _y = not 1;
| ----^
| |
| help: use `!` to perform bitwise not

error: unexpected keyword `false` after identifier
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:6:18
|
LL | let _z = not false;
| ----^^^^^
| |
| help: use `!` to perform logical negation

error: unexpected keyword `true` after identifier
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:7:18
|
LL | let _a = not true;
| ----^^^^
| |
| help: use `!` to perform logical negation

error: unexpected `v` after identifier
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:9:18
|
LL | let _v = not v;
| ----^
| |
| help: use `!` to perform logical negation or bitwise not

error: aborting due to 5 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ error: unexpected `for_you` after identifier
LL | if not for_you {
| ----^^^^^^^
| |
| help: use `!` to perform logical negation
| help: use `!` to perform logical negation or bitwise not

error: unexpected `the_worst` after identifier
--> $DIR/issue-46836-identifier-not-instead-of-negation.rs:11:15
|
LL | while not the_worst {
| ----^^^^^^^^^
| |
| help: use `!` to perform logical negation
| help: use `!` to perform logical negation or bitwise not

error: unexpected `println` after identifier
--> $DIR/issue-46836-identifier-not-instead-of-negation.rs:20:9
|
LL | if not // lack of braces is [sic]
| ----- help: use `!` to perform logical negation
| ----- help: use `!` to perform logical negation or bitwise not
LL | println!("Then when?");
| ^^^^^^^

Expand All @@ -42,15 +42,15 @@ error: unexpected `2` after identifier
LL | let resource = not 2;
| ----^
| |
| help: use `!` to perform logical negation
| help: use `!` to perform bitwise not

error: unexpected `be_smothered_out_before` after identifier
--> $DIR/issue-46836-identifier-not-instead-of-negation.rs:32:27
|
LL | let young_souls = not be_smothered_out_before;
| ----^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: use `!` to perform logical negation
| help: use `!` to perform logical negation or bitwise not

error: aborting due to 6 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,63 @@ error: `and` is not a logical operator
LL | let _ = a and b;
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:9:10
|
LL | if a and b {
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:20:15
|
LL | let _ = a or b;
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:22:10
|
LL | if a or b {
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:30:11
|
LL | if (a and b) {
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:38:11
|
LL | if (a or b) {
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:46:13
|
LL | while a and b {
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:54:13
|
LL | while a or b {
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error[E0308]: mismatched types
--> $DIR/issue-54109-and_instead_of_ampersands.rs:13:33
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/did_you_mean/issue-54109-without-witness.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,63 @@ error: `and` is not a logical operator
LL | let _ = a and b;
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `and` is not a logical operator
--> $DIR/issue-54109-without-witness.rs:15:10
|
LL | if a and b {
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-without-witness.rs:24:15
|
LL | let _ = a or b;
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-without-witness.rs:26:10
|
LL | if a or b {
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `and` is not a logical operator
--> $DIR/issue-54109-without-witness.rs:34:11
|
LL | if (a and b) {
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-without-witness.rs:42:11
|
LL | if (a or b) {
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `and` is not a logical operator
--> $DIR/issue-54109-without-witness.rs:50:13
|
LL | while a and b {
| ^^^ help: use `&&` to perform logical conjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: `or` is not a logical operator
--> $DIR/issue-54109-without-witness.rs:58:13
|
LL | while a or b {
| ^^ help: use `||` to perform logical disjunction
|
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
= note: unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

error: aborting due to 8 previous errors