Skip to content

Commit f43562b

Browse files
committed
more tweak on diagnostic messages
1 parent 2194fc9 commit f43562b

9 files changed

+65
-23
lines changed

compiler/rustc_error_messages/locales/en-US/parser.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ 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 bitwise not
58+
.suggestion = use `!` to perform {$negated_msg}
5959
6060
parser_malformed_loop_label = malformed loop label
6161
.suggestion = use the correct loop label format

compiler/rustc_parse/src/parser/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ pub(crate) struct NotAsNegationOperator {
430430
#[primary_span]
431431
pub negated: Span,
432432
pub negated_desc: String,
433+
pub negated_msg: String,
433434
#[suggestion_short(applicability = "machine-applicable", code = "!")]
434435
pub not: Span,
435436
}

compiler/rustc_parse/src/parser/expr.rs

+9
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,18 @@ impl<'a> Parser<'a> {
660660
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
661661
// Emit the error...
662662
let negated_token = self.look_ahead(1, |t| t.clone());
663+
let negtated_msg = if negated_token.is_numeric_lit() {
664+
"bitwise not"
665+
} else if negated_token.is_bool_lit() {
666+
"logical negation"
667+
} else {
668+
"logical negation or bitwise not"
669+
};
670+
663671
self.sess.emit_err(NotAsNegationOperator {
664672
negated: negated_token.span,
665673
negated_desc: super::token_descr(&negated_token),
674+
negated_msg: negtated_msg.to_string(),
666675
// Span the `not` plus trailing whitespace to avoid
667676
// trailing whitespace after the `!` in our suggestion
668677
not: self.sess.source_map().span_until_non_whitespace(lo.to(negated_token.span)),

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

+4
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
fn main() {
44
let _x = !1; //~ ERROR cannot be used as a unary operator
55
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
610
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
fn main() {
44
let _x = ~1; //~ ERROR cannot be used as a unary operator
55
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
610
}

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,29 @@ LL | let _y = not 1;
1212
| |
1313
| help: use `!` to perform bitwise not
1414

15-
error: aborting due to 2 previous errors
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
1640

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

+4-4
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 bitwise not
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 bitwise not
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 bitwise not
21+
| ----- help: use `!` to perform logical negation or bitwise not
2222
LL | println!("Then when?");
2323
| ^^^^^^^
2424

@@ -50,7 +50,7 @@ error: unexpected `be_smothered_out_before` after identifier
5050
LL | let young_souls = not be_smothered_out_before;
5151
| ----^^^^^^^^^^^^^^^^^^^^^^^
5252
| |
53-
| help: use `!` to perform bitwise not
53+
| help: use `!` to perform logical negation or bitwise not
5454

5555
error: aborting due to 6 previous errors
5656

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

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)