-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #108427 - y21:for-else-diagnostic, r=compiler-errors
Recover from for-else and while-else This recovers from attempts at writing for-else or while-else loops, which might help users coming from e.g. Python. ```rs for _ in 0..0 { // ... } else { // ... } ``` Combined with trying to store it in a let binding, the current diagnostic can be a bit confusing. It mentions let-else and suggests wrapping the loop in parentheses, which the user probably doesn't want. let-else doesn't make sense for `for` and `while` loops, as they are of type `()` (which already is an irrefutable pattern and doesn't need let-else). <details> <summary>Current diagnostic</summary> ```rs error: right curly brace `}` before `else` in a `let...else` statement not allowed --> src/main.rs:4:5 | 4 | } else { | ^ | help: wrap the expression in parentheses | 2 ~ let _x = (for _ in 0..0 { 3 | 4 ~ }) else { | ``` </details> Some questions: - Can the wording for the error message be improved? Would "for...else loops are not allowed" fit better? - Should we be more "conservative" in case we want to support this in the future (i.e. say "for...else loops are **currently** not allowed/supported")? - Is there a better way than storing a `&'static str` for the loop type? It is used for substituting the placeholder in the locale file (since it can emit either `for...else` or `while...else`). Maybe there is an enum I could use that I couldn't find
- Loading branch information
Showing
20 changed files
with
218 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
for _ in 0..1 { | ||
//~^ NOTE `else` is attached to this loop | ||
} else { | ||
//~^ ERROR `for...else` loops are not supported | ||
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: `for...else` loops are not supported | ||
--> $DIR/for-else-err.rs:4:7 | ||
| | ||
LL | for _ in 0..1 { | ||
| --- `else` is attached to this loop | ||
LL | | ||
LL | } else { | ||
| _______^ | ||
LL | | | ||
LL | | | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
let _ = for _ in 0..1 { | ||
//~^ NOTE `else` is attached to this loop | ||
} else { | ||
//~^ ERROR `for...else` loops are not supported | ||
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: `for...else` loops are not supported | ||
--> $DIR/for-else-let-else-err.rs:4:7 | ||
| | ||
LL | let _ = for _ in 0..1 { | ||
| --- `else` is attached to this loop | ||
LL | | ||
LL | } else { | ||
| _______^ | ||
LL | | | ||
LL | | | ||
LL | | }; | ||
| |_____^ | ||
| | ||
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn main() { | ||
let Some(1) = loop { | ||
//~^ NOTE `else` is attached to this loop | ||
break Some(1) | ||
} else { | ||
//~^ ERROR `loop...else` loops are not supported | ||
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
return; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error: `loop...else` loops are not supported | ||
--> $DIR/loop-else-break-with-value.rs:5:7 | ||
| | ||
LL | let Some(1) = loop { | ||
| ---- `else` is attached to this loop | ||
... | ||
LL | } else { | ||
| _______^ | ||
LL | | | ||
LL | | | ||
LL | | return; | ||
LL | | }; | ||
| |_____^ | ||
| | ||
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
loop { | ||
//~^ NOTE `else` is attached to this loop | ||
} else { | ||
//~^ ERROR `loop...else` loops are not supported | ||
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: `loop...else` loops are not supported | ||
--> $DIR/loop-else-err.rs:4:7 | ||
| | ||
LL | loop { | ||
| ---- `else` is attached to this loop | ||
LL | | ||
LL | } else { | ||
| _______^ | ||
LL | | | ||
LL | | | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
let _ = loop { | ||
//~^ NOTE `else` is attached to this loop | ||
} else { | ||
//~^ ERROR `loop...else` loops are not supported | ||
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: `loop...else` loops are not supported | ||
--> $DIR/loop-else-let-else-err.rs:4:7 | ||
| | ||
LL | let _ = loop { | ||
| ---- `else` is attached to this loop | ||
LL | | ||
LL | } else { | ||
| _______^ | ||
LL | | | ||
LL | | | ||
LL | | }; | ||
| |_____^ | ||
| | ||
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
while false { | ||
//~^ NOTE `else` is attached to this loop | ||
} else { | ||
//~^ ERROR `while...else` loops are not supported | ||
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: `while...else` loops are not supported | ||
--> $DIR/while-else-err.rs:4:7 | ||
| | ||
LL | while false { | ||
| ----- `else` is attached to this loop | ||
LL | | ||
LL | } else { | ||
| _______^ | ||
LL | | | ||
LL | | | ||
LL | | }; | ||
| |_____^ | ||
| | ||
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
let _ = while false { | ||
//~^ NOTE `else` is attached to this loop | ||
} else { | ||
//~^ ERROR `while...else` loops are not supported | ||
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: `while...else` loops are not supported | ||
--> $DIR/while-else-let-else-err.rs:4:7 | ||
| | ||
LL | let _ = while false { | ||
| ----- `else` is attached to this loop | ||
LL | | ||
LL | } else { | ||
| _______^ | ||
LL | | | ||
LL | | | ||
LL | | }; | ||
| |_____^ | ||
| | ||
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run | ||
|
||
error: aborting due to previous error | ||
|