Skip to content

Commit e6fe523

Browse files
authored
Rollup merge of #75658 - tgnottingham:issue-75599, r=estebank
Don't emit "is not a logical operator" error outside of associative expressions Avoid showing this error where it doesn't make sense by not assuming "and" and "or" were intended to mean "&&" and "||" until after we decide to continue parsing input as an associative expression. Note that the decision of whether or not to continue parsing input as an associative expression doesn't actually depend on this assumption. Fixes #75599 --- First time contributor! Let me know if there are any conventions or policies I should be following that I missed here. Thanks :)
2 parents e6e917e + ff73a40 commit e6fe523

7 files changed

+42
-170
lines changed

src/librustc_parse/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl<'a> Parser<'a> {
308308
}
309309

310310
fn should_continue_as_assoc_expr(&mut self, lhs: &Expr) -> bool {
311-
match (self.expr_is_complete(lhs), self.check_assoc_op().map(|op| op.node)) {
311+
match (self.expr_is_complete(lhs), AssocOp::from_token(&self.token)) {
312312
// Semi-statement forms are odd:
313313
// See https://github.com/rust-lang/rust/issues/29071
314314
(true, None) => false,

src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.rs

-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ fn test_and() {
55
let b = false;
66

77
let _ = a and b; //~ ERROR `and` is not a logical operator
8-
//~| ERROR `and` is not a logical operator
98

109
if a and b { //~ ERROR `and` is not a logical operator
11-
//~| ERROR `and` is not a logical operator
1210
println!("both");
1311
}
1412

@@ -20,10 +18,8 @@ fn test_or() {
2018
let b = false;
2119

2220
let _ = a or b; //~ ERROR `or` is not a logical operator
23-
//~| ERROR `or` is not a logical operator
2421

2522
if a or b { //~ ERROR `or` is not a logical operator
26-
//~| ERROR `or` is not a logical operator
2723
println!("both");
2824
}
2925
}
@@ -32,7 +28,6 @@ fn test_and_par() {
3228
let a = true;
3329
let b = false;
3430
if (a and b) { //~ ERROR `and` is not a logical operator
35-
//~| ERROR `and` is not a logical operator
3631
println!("both");
3732
}
3833
}
@@ -41,7 +36,6 @@ fn test_or_par() {
4136
let a = true;
4237
let b = false;
4338
if (a or b) { //~ ERROR `or` is not a logical operator
44-
//~| ERROR `or` is not a logical operator
4539
println!("both");
4640
}
4741
}
@@ -50,7 +44,6 @@ fn test_while_and() {
5044
let a = true;
5145
let b = false;
5246
while a and b { //~ ERROR `and` is not a logical operator
53-
//~| ERROR `and` is not a logical operator
5447
println!("both");
5548
}
5649
}
@@ -59,7 +52,6 @@ fn test_while_or() {
5952
let a = true;
6053
let b = false;
6154
while a or b { //~ ERROR `or` is not a logical operator
62-
//~| ERROR `or` is not a logical operator
6355
println!("both");
6456
}
6557
}

src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr

+9-73
Original file line numberDiff line numberDiff line change
@@ -7,133 +7,69 @@ LL | let _ = a and b;
77
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
88

99
error: `and` is not a logical operator
10-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:7:15
11-
|
12-
LL | let _ = a and b;
13-
| ^^^ help: use `&&` to perform logical conjunction
14-
|
15-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
16-
17-
error: `and` is not a logical operator
18-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:10:10
19-
|
20-
LL | if a and b {
21-
| ^^^ help: use `&&` to perform logical conjunction
22-
|
23-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
24-
25-
error: `and` is not a logical operator
26-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:10:10
10+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:9:10
2711
|
2812
LL | if a and b {
2913
| ^^^ help: use `&&` to perform logical conjunction
3014
|
3115
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
3216

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

4125
error: `or` is not a logical operator
42-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:22:15
43-
|
44-
LL | let _ = a or b;
45-
| ^^ help: use `||` to perform logical disjunction
46-
|
47-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
48-
49-
error: `or` is not a logical operator
50-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:25:10
51-
|
52-
LL | if a or b {
53-
| ^^ help: use `||` to perform logical disjunction
54-
|
55-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
56-
57-
error: `or` is not a logical operator
58-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:25:10
26+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:22:10
5927
|
6028
LL | if a or b {
6129
| ^^ help: use `||` to perform logical disjunction
6230
|
6331
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
6432

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

73-
error: `and` is not a logical operator
74-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:34:11
75-
|
76-
LL | if (a and b) {
77-
| ^^^ help: use `&&` to perform logical conjunction
78-
|
79-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
80-
81-
error: `or` is not a logical operator
82-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:43:11
83-
|
84-
LL | if (a or b) {
85-
| ^^ help: use `||` to perform logical disjunction
86-
|
87-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
88-
8941
error: `or` is not a logical operator
90-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:43:11
42+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:38:11
9143
|
9244
LL | if (a or b) {
9345
| ^^ help: use `||` to perform logical disjunction
9446
|
9547
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
9648

9749
error: `and` is not a logical operator
98-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:52:13
50+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:46:13
9951
|
10052
LL | while a and b {
10153
| ^^^ help: use `&&` to perform logical conjunction
10254
|
10355
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
10456

105-
error: `and` is not a logical operator
106-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:52:13
107-
|
108-
LL | while a and b {
109-
| ^^^ help: use `&&` to perform logical conjunction
110-
|
111-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
112-
113-
error: `or` is not a logical operator
114-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:61:13
115-
|
116-
LL | while a or b {
117-
| ^^ help: use `||` to perform logical disjunction
118-
|
119-
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
120-
12157
error: `or` is not a logical operator
122-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:61:13
58+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:54:13
12359
|
12460
LL | while a or b {
12561
| ^^ help: use `||` to perform logical disjunction
12662
|
12763
= note: unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
12864

12965
error[E0308]: mismatched types
130-
--> $DIR/issue-54109-and_instead_of_ampersands.rs:15:33
66+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:13:33
13167
|
13268
LL | let _recovery_witness: () = 0;
13369
| -- ^ expected `()`, found integer
13470
| |
13571
| expected due to this
13672

137-
error: aborting due to 17 previous errors
73+
error: aborting due to 9 previous errors
13874

13975
For more information about this error, try `rustc --explain E0308`.

src/test/ui/did_you_mean/issue-54109-without-witness.fixed

-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ fn test_and() {
1111
let b = false;
1212

1313
let _ = a && b; //~ ERROR `and` is not a logical operator
14-
//~| ERROR `and` is not a logical operator
1514

1615
if a && b { //~ ERROR `and` is not a logical operator
17-
//~| ERROR `and` is not a logical operator
1816
println!("both");
1917
}
2018
}
@@ -24,10 +22,8 @@ fn test_or() {
2422
let b = false;
2523

2624
let _ = a || b; //~ ERROR `or` is not a logical operator
27-
//~| ERROR `or` is not a logical operator
2825

2926
if a || b { //~ ERROR `or` is not a logical operator
30-
//~| ERROR `or` is not a logical operator
3127
println!("both");
3228
}
3329
}
@@ -36,7 +32,6 @@ fn test_and_par() {
3632
let a = true;
3733
let b = false;
3834
if (a && b) { //~ ERROR `and` is not a logical operator
39-
//~| ERROR `and` is not a logical operator
4035
println!("both");
4136
}
4237
}
@@ -45,7 +40,6 @@ fn test_or_par() {
4540
let a = true;
4641
let b = false;
4742
if (a || b) { //~ ERROR `or` is not a logical operator
48-
//~| ERROR `or` is not a logical operator
4943
println!("both");
5044
}
5145
}
@@ -54,7 +48,6 @@ fn test_while_and() {
5448
let a = true;
5549
let b = false;
5650
while a && b { //~ ERROR `and` is not a logical operator
57-
//~| ERROR `and` is not a logical operator
5851
println!("both");
5952
}
6053
}
@@ -63,7 +56,6 @@ fn test_while_or() {
6356
let a = true;
6457
let b = false;
6558
while a || b { //~ ERROR `or` is not a logical operator
66-
//~| ERROR `or` is not a logical operator
6759
println!("both");
6860
}
6961
}

src/test/ui/did_you_mean/issue-54109-without-witness.rs

-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ fn test_and() {
1111
let b = false;
1212

1313
let _ = a and b; //~ ERROR `and` is not a logical operator
14-
//~| ERROR `and` is not a logical operator
1514

1615
if a and b { //~ ERROR `and` is not a logical operator
17-
//~| ERROR `and` is not a logical operator
1816
println!("both");
1917
}
2018
}
@@ -24,10 +22,8 @@ fn test_or() {
2422
let b = false;
2523

2624
let _ = a or b; //~ ERROR `or` is not a logical operator
27-
//~| ERROR `or` is not a logical operator
2825

2926
if a or b { //~ ERROR `or` is not a logical operator
30-
//~| ERROR `or` is not a logical operator
3127
println!("both");
3228
}
3329
}
@@ -36,7 +32,6 @@ fn test_and_par() {
3632
let a = true;
3733
let b = false;
3834
if (a and b) { //~ ERROR `and` is not a logical operator
39-
//~| ERROR `and` is not a logical operator
4035
println!("both");
4136
}
4237
}
@@ -45,7 +40,6 @@ fn test_or_par() {
4540
let a = true;
4641
let b = false;
4742
if (a or b) { //~ ERROR `or` is not a logical operator
48-
//~| ERROR `or` is not a logical operator
4943
println!("both");
5044
}
5145
}
@@ -54,7 +48,6 @@ fn test_while_and() {
5448
let a = true;
5549
let b = false;
5650
while a and b { //~ ERROR `and` is not a logical operator
57-
//~| ERROR `and` is not a logical operator
5851
println!("both");
5952
}
6053
}
@@ -63,7 +56,6 @@ fn test_while_or() {
6356
let a = true;
6457
let b = false;
6558
while a or b { //~ ERROR `or` is not a logical operator
66-
//~| ERROR `or` is not a logical operator
6759
println!("both");
6860
}
6961
}

0 commit comments

Comments
 (0)