Skip to content

Commit

Permalink
Rollup merge of rust-lang#75658 - tgnottingham:issue-75599, r=estebank
Browse files Browse the repository at this point in the history
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 rust-lang#75599

---

First time contributor! Let me know if there are any conventions or policies I should be following that I missed here. Thanks :)
  • Loading branch information
JohnTitor authored Aug 19, 2020
2 parents e6e917e + ff73a40 commit e6fe523
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 170 deletions.
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl<'a> Parser<'a> {
}

fn should_continue_as_assoc_expr(&mut self, lhs: &Expr) -> bool {
match (self.expr_is_complete(lhs), self.check_assoc_op().map(|op| op.node)) {
match (self.expr_is_complete(lhs), AssocOp::from_token(&self.token)) {
// Semi-statement forms are odd:
// See https://github.com/rust-lang/rust/issues/29071
(true, None) => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ fn test_and() {
let b = false;

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

if a and b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}

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

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

if a or b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Expand All @@ -32,7 +28,6 @@ fn test_and_par() {
let a = true;
let b = false;
if (a and b) { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}
Expand All @@ -41,7 +36,6 @@ fn test_or_par() {
let a = true;
let b = false;
if (a or b) { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Expand All @@ -50,7 +44,6 @@ fn test_while_and() {
let a = true;
let b = false;
while a and b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}
Expand All @@ -59,7 +52,6 @@ fn test_while_or() {
let a = true;
let b = false;
while a or b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,133 +7,69 @@ LL | let _ = a and b;
= 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:7:15
|
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

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:10: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

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:10:10
--> $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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:22:15
--> $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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:22: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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:25: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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:25:10
--> $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

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:34:11
--> $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

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:43: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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:43:11
--> $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

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:52:13
--> $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

error: `and` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:52: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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:61: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

error: `or` is not a logical operator
--> $DIR/issue-54109-and_instead_of_ampersands.rs:61:13
--> $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

error[E0308]: mismatched types
--> $DIR/issue-54109-and_instead_of_ampersands.rs:15:33
--> $DIR/issue-54109-and_instead_of_ampersands.rs:13:33
|
LL | let _recovery_witness: () = 0;
| -- ^ expected `()`, found integer
| |
| expected due to this

error: aborting due to 17 previous errors
error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0308`.
8 changes: 0 additions & 8 deletions src/test/ui/did_you_mean/issue-54109-without-witness.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ fn test_and() {
let b = false;

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

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

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

if a || b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Expand All @@ -36,7 +32,6 @@ fn test_and_par() {
let a = true;
let b = false;
if (a && b) { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}
Expand All @@ -45,7 +40,6 @@ fn test_or_par() {
let a = true;
let b = false;
if (a || b) { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Expand All @@ -54,7 +48,6 @@ fn test_while_and() {
let a = true;
let b = false;
while a && b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}
Expand All @@ -63,7 +56,6 @@ fn test_while_or() {
let a = true;
let b = false;
while a || b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
8 changes: 0 additions & 8 deletions src/test/ui/did_you_mean/issue-54109-without-witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ fn test_and() {
let b = false;

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

if a and b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}
Expand All @@ -24,10 +22,8 @@ fn test_or() {
let b = false;

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

if a or b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Expand All @@ -36,7 +32,6 @@ fn test_and_par() {
let a = true;
let b = false;
if (a and b) { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}
Expand All @@ -45,7 +40,6 @@ fn test_or_par() {
let a = true;
let b = false;
if (a or b) { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Expand All @@ -54,7 +48,6 @@ fn test_while_and() {
let a = true;
let b = false;
while a and b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}
Expand All @@ -63,7 +56,6 @@ fn test_while_or() {
let a = true;
let b = false;
while a or b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Loading

0 comments on commit e6fe523

Please sign in to comment.