Skip to content

Commit b79b7d8

Browse files
committed
Auto merge of #101846 - chenyukang:fix-101793, r=davidtwco
Fix the wording of help msg for bitwise not Fixes #101793
2 parents cba4a38 + 484b612 commit b79b7d8

9 files changed

+109
-29
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,17 @@ parser_invalid_comparison_operator = invalid comparison operator `{$invalid}`
4747
.spaceship_operator_invalid = `<=>` is not a valid comparison operator, use `std::cmp::Ordering`
4848
4949
parser_invalid_logical_operator = `{$incorrect}` is not a logical operator
50-
.note = unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
50+
.note = unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators
5151
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
5252
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
5353
5454
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 logical negation
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-2
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,32 @@ pub(crate) struct NotAsNegationOperator {
430430
#[primary_span]
431431
pub negated: Span,
432432
pub negated_desc: String,
433-
#[suggestion_short(applicability = "machine-applicable", code = "!")]
434-
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),
435459
}
436460

437461
#[derive(SessionDiagnostic)]

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

+15-3
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,12 +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());
664+
665+
let sub_diag = if negated_token.is_numeric_lit() {
666+
NotAsNegationOperatorSub::SuggestNotBitwise
667+
} else if negated_token.is_bool_lit() {
668+
NotAsNegationOperatorSub::SuggestNotLogical
669+
} else {
670+
NotAsNegationOperatorSub::SuggestNotDefault
671+
};
672+
663673
self.sess.emit_err(NotAsNegationOperator {
664674
negated: negated_token.span,
665675
negated_desc: super::token_descr(&negated_token),
666676
// Span the `not` plus trailing whitespace to avoid
667677
// trailing whitespace after the `!` in our suggestion
668-
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+
),
669681
});
670682

671683
// ...and recover!

Diff for: src/test/ui/did_you_mean/issue-41679-tilde-bitwise-negation-attempt.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22

33
fn main() {
44
let _x = !1; //~ ERROR cannot be used as a unary operator
5+
let _y = !1; //~ ERROR unexpected `1` after identifier
6+
let _z = !false; //~ ERROR unexpected keyword `false` after identifier
7+
let _a = !true; //~ ERROR unexpected keyword `true` after identifier
8+
let v = 1 + 2;
9+
let _v = !v; //~ ERROR unexpected `v` after identifier
510
}

Diff for: src/test/ui/did_you_mean/issue-41679-tilde-bitwise-negation-attempt.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22

33
fn main() {
44
let _x = ~1; //~ ERROR cannot be used as a unary operator
5+
let _y = not 1; //~ ERROR unexpected `1` after identifier
6+
let _z = not false; //~ ERROR unexpected keyword `false` after identifier
7+
let _a = not true; //~ ERROR unexpected keyword `true` after identifier
8+
let v = 1 + 2;
9+
let _v = not v; //~ ERROR unexpected `v` after identifier
510
}

Diff for: src/test/ui/did_you_mean/issue-41679-tilde-bitwise-negation-attempt.stderr

+33-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,37 @@ error: `~` cannot be used as a unary operator
44
LL | let _x = ~1;
55
| ^ help: use `!` to perform bitwise not
66

7-
error: aborting due to previous error
7+
error: unexpected `1` after identifier
8+
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:5:18
9+
|
10+
LL | let _y = not 1;
11+
| ----^
12+
| |
13+
| help: use `!` to perform bitwise not
14+
15+
error: unexpected keyword `false` after identifier
16+
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:6:18
17+
|
18+
LL | let _z = not false;
19+
| ----^^^^^
20+
| |
21+
| help: use `!` to perform logical negation
22+
23+
error: unexpected keyword `true` after identifier
24+
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:7:18
25+
|
26+
LL | let _a = not true;
27+
| ----^^^^
28+
| |
29+
| help: use `!` to perform logical negation
30+
31+
error: unexpected `v` after identifier
32+
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:9:18
33+
|
34+
LL | let _v = not v;
35+
| ----^
36+
| |
37+
| help: use `!` to perform logical negation or bitwise not
38+
39+
error: aborting due to 5 previous errors
840

Diff for: src/test/ui/did_you_mean/issue-46836-identifier-not-instead-of-negation.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ error: unexpected `for_you` after identifier
44
LL | if not for_you {
55
| ----^^^^^^^
66
| |
7-
| help: use `!` to perform logical negation
7+
| help: use `!` to perform logical negation or bitwise not
88

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

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

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

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

5555
error: aborting due to 6 previous errors
5656

Diff for: src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr

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

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

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

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

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

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

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

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

6565
error[E0308]: mismatched types
6666
--> $DIR/issue-54109-and_instead_of_ampersands.rs:13:33

Diff for: src/test/ui/did_you_mean/issue-54109-without-witness.stderr

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

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

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

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

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

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

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

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

6565
error: aborting due to 8 previous errors
6666

0 commit comments

Comments
 (0)