Skip to content

Commit e324a59

Browse files
committed
Address review comments
- Add doc comment to new type - Restore "only supported directly in conditions of `if` and `while` expressions" note - Rename variant with clearer name
1 parent b011a0a commit e324a59

15 files changed

+362
-4
lines changed

Diff for: compiler/rustc_parse/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ parse_expected_else_block = expected `{"{"}`, found {$first_tok}
196196
.suggestion = add an `if` if this is the condition of a chained `else if` statement
197197

198198
parse_expected_expression_found_let = expected expression, found `let` statement
199+
.note = only supported directly in conditions of `if` and `while` expressions
199200
.not_supported_or = `||` operators are not supported in let chain expressions
200201
.not_supported_parentheses = `let`s wrapped in parentheses are not supported in a context with let chains
201202

Diff for: compiler/rustc_parse/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ pub(crate) struct IfExpressionMissingCondition {
392392

393393
#[derive(Diagnostic)]
394394
#[diag(parse_expected_expression_found_let)]
395+
#[note]
395396
pub(crate) struct ExpectedExpressionFoundLet {
396397
#[primary_span]
397398
pub span: Span,

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@ impl<'a> Parser<'a> {
24612461
let is_recovered = if !restrictions.contains(Restrictions::ALLOW_LET) {
24622462
Some(self.sess.emit_err(errors::ExpectedExpressionFoundLet {
24632463
span: self.token.span,
2464-
reason: ForbiddenLetReason::GenericForbidden,
2464+
reason: ForbiddenLetReason::OtherForbidden,
24652465
}))
24662466
} else {
24672467
None
@@ -3427,7 +3427,7 @@ impl<'a> Parser<'a> {
34273427
#[derive(Clone, Copy, Subdiagnostic)]
34283428
pub(crate) enum ForbiddenLetReason {
34293429
/// `let` is not valid and the source environment is not important
3430-
GenericForbidden,
3430+
OtherForbidden,
34313431
/// A let chain with the `||` operator
34323432
#[note(parse_not_supported_or)]
34333433
NotSupportedOr(#[primary_span] Span),
@@ -3439,6 +3439,15 @@ pub(crate) enum ForbiddenLetReason {
34393439
NotSupportedParentheses(#[primary_span] Span),
34403440
}
34413441

3442+
/// Visitor to check for invalid/unstable use of `ExprKind::Let` that can't
3443+
/// easily be caught in parsing. For example:
3444+
///
3445+
/// ```rust,ignore (example)
3446+
/// // Only know that the let isn't allowed once the `||` token is reached
3447+
/// if let Some(x) = y || true {}
3448+
/// // Only know that the let isn't allowed once the second `=` token is reached.
3449+
/// if let Some(x) = y && z = 1 {}
3450+
/// ```
34423451
struct CondChecker<'a> {
34433452
parser: &'a Parser<'a>,
34443453
forbid_let_reason: Option<ForbiddenLetReason>,
@@ -3495,14 +3504,14 @@ impl MutVisitor for CondChecker<'_> {
34953504
| ExprKind::Tup(_)
34963505
| ExprKind::Paren(_) => {
34973506
let forbid_let_reason = self.forbid_let_reason;
3498-
self.forbid_let_reason = Some(GenericForbidden);
3507+
self.forbid_let_reason = Some(OtherForbidden);
34993508
noop_visit_expr(e, self);
35003509
self.forbid_let_reason = forbid_let_reason;
35013510
}
35023511
ExprKind::Cast(ref mut op, _)
35033512
| ExprKind::Type(ref mut op, _) => {
35043513
let forbid_let_reason = self.forbid_let_reason;
3505-
self.forbid_let_reason = Some(GenericForbidden);
3514+
self.forbid_let_reason = Some(OtherForbidden);
35063515
self.visit_expr(op);
35073516
self.forbid_let_reason = forbid_let_reason;
35083517
}

Diff for: tests/ui/expr/if/bad-if-let-suggestion.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected expression, found `let` statement
33
|
44
LL | if let x = 1 && i = 2 {}
55
| ^^^^^^^^^
6+
|
7+
= note: only supported directly in conditions of `if` and `while` expressions
68

79
error[E0425]: cannot find value `i` in this scope
810
--> $DIR/bad-if-let-suggestion.rs:5:21

Diff for: tests/ui/mir/issue-92893.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected expression, found `let` statement
33
|
44
LL | struct Bug<A = [(); (let a = (), 1).1]> {
55
| ^^^
6+
|
7+
= note: only supported directly in conditions of `if` and `while` expressions
68

79
error: aborting due to previous error
810

Diff for: tests/ui/rfcs/rfc-2294-if-let-guard/feature-gate.stderr

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: expected expression, found `let` statement
44
LL | () if (let 0 = 1) => {}
55
| ^^^^^^^^^
66
|
7+
= note: only supported directly in conditions of `if` and `while` expressions
78
note: `let`s wrapped in parentheses are not supported in a context with let chains
89
--> $DIR/feature-gate.rs:10:16
910
|
@@ -16,6 +17,7 @@ error: expected expression, found `let` statement
1617
LL | () if (((let 0 = 1))) => {}
1718
| ^^^^^^^^^
1819
|
20+
= note: only supported directly in conditions of `if` and `while` expressions
1921
note: `let`s wrapped in parentheses are not supported in a context with let chains
2022
--> $DIR/feature-gate.rs:13:18
2123
|
@@ -28,6 +30,7 @@ error: expected expression, found `let` statement
2830
LL | () if (let 0 = 1) && true => {}
2931
| ^^^^^^^^^
3032
|
33+
= note: only supported directly in conditions of `if` and `while` expressions
3134
note: `let`s wrapped in parentheses are not supported in a context with let chains
3235
--> $DIR/feature-gate.rs:24:16
3336
|
@@ -40,6 +43,7 @@ error: expected expression, found `let` statement
4043
LL | () if true && (let 0 = 1) => {}
4144
| ^^^^^^^^^
4245
|
46+
= note: only supported directly in conditions of `if` and `while` expressions
4347
note: `let`s wrapped in parentheses are not supported in a context with let chains
4448
--> $DIR/feature-gate.rs:27:24
4549
|
@@ -52,6 +56,7 @@ error: expected expression, found `let` statement
5256
LL | () if (let 0 = 1) && (let 0 = 1) => {}
5357
| ^^^^^^^^^
5458
|
59+
= note: only supported directly in conditions of `if` and `while` expressions
5560
note: `let`s wrapped in parentheses are not supported in a context with let chains
5661
--> $DIR/feature-gate.rs:30:16
5762
|
@@ -64,6 +69,7 @@ error: expected expression, found `let` statement
6469
LL | () if (let 0 = 1) && (let 0 = 1) => {}
6570
| ^^^^^^^^^
6671
|
72+
= note: only supported directly in conditions of `if` and `while` expressions
6773
note: `let`s wrapped in parentheses are not supported in a context with let chains
6874
--> $DIR/feature-gate.rs:30:31
6975
|
@@ -76,6 +82,7 @@ error: expected expression, found `let` statement
7682
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
7783
| ^^^^^^^^^
7884
|
85+
= note: only supported directly in conditions of `if` and `while` expressions
7986
note: `let`s wrapped in parentheses are not supported in a context with let chains
8087
--> $DIR/feature-gate.rs:34:42
8188
|
@@ -88,6 +95,7 @@ error: expected expression, found `let` statement
8895
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
8996
| ^^^^^^^^^
9097
|
98+
= note: only supported directly in conditions of `if` and `while` expressions
9199
note: `let`s wrapped in parentheses are not supported in a context with let chains
92100
--> $DIR/feature-gate.rs:34:42
93101
|
@@ -100,6 +108,7 @@ error: expected expression, found `let` statement
100108
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
101109
| ^^^^^^^^^
102110
|
111+
= note: only supported directly in conditions of `if` and `while` expressions
103112
note: `let`s wrapped in parentheses are not supported in a context with let chains
104113
--> $DIR/feature-gate.rs:34:42
105114
|
@@ -111,12 +120,16 @@ error: expected expression, found `let` statement
111120
|
112121
LL | use_expr!((let 0 = 1 && 0 == 0));
113122
| ^^^
123+
|
124+
= note: only supported directly in conditions of `if` and `while` expressions
114125

115126
error: expected expression, found `let` statement
116127
--> $DIR/feature-gate.rs:62:16
117128
|
118129
LL | use_expr!((let 0 = 1));
119130
| ^^^
131+
|
132+
= note: only supported directly in conditions of `if` and `while` expressions
120133

121134
error: no rules expected the token `let`
122135
--> $DIR/feature-gate.rs:70:15

Diff for: tests/ui/rfcs/rfc-2294-if-let-guard/macro-expanded.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LL | ($e:expr) => { let Some(x) = $e }
77
LL | () if m!(Some(5)) => {}
88
| ----------- in this macro invocation
99
|
10+
= note: only supported directly in conditions of `if` and `while` expressions
1011
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
1112

1213
error: aborting due to previous error

Diff for: tests/ui/rfcs/rfc-2294-if-let-guard/parens.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: expected expression, found `let` statement
44
LL | () if (let 0 = 1) => {}
55
| ^^^^^^^^^
66
|
7+
= note: only supported directly in conditions of `if` and `while` expressions
78
note: `let`s wrapped in parentheses are not supported in a context with let chains
89
--> $DIR/parens.rs:10:16
910
|
@@ -16,6 +17,7 @@ error: expected expression, found `let` statement
1617
LL | () if (((let 0 = 1))) => {}
1718
| ^^^^^^^^^
1819
|
20+
= note: only supported directly in conditions of `if` and `while` expressions
1921
note: `let`s wrapped in parentheses are not supported in a context with let chains
2022
--> $DIR/parens.rs:12:18
2123
|
@@ -28,6 +30,7 @@ error: expected expression, found `let` statement
2830
LL | () if (let 0 = 1) => {}
2931
| ^^^^^^^^^
3032
|
33+
= note: only supported directly in conditions of `if` and `while` expressions
3134
note: `let`s wrapped in parentheses are not supported in a context with let chains
3235
--> $DIR/parens.rs:20:16
3336
|
@@ -40,6 +43,7 @@ error: expected expression, found `let` statement
4043
LL | () if (((let 0 = 1))) => {}
4144
| ^^^^^^^^^
4245
|
46+
= note: only supported directly in conditions of `if` and `while` expressions
4347
note: `let`s wrapped in parentheses are not supported in a context with let chains
4448
--> $DIR/parens.rs:22:18
4549
|

Diff for: tests/ui/rfcs/rfc-2497-if-let-chains/ast-validate-guards.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: expected expression, found `let` statement
44
LL | Ok(opt) if let Some(4) = opt || false => {}
55
| ^^^^^^^^^^^^^^^^^
66
|
7+
= note: only supported directly in conditions of `if` and `while` expressions
78
note: `||` operators are not supported in let chain expressions
89
--> $DIR/ast-validate-guards.rs:5:38
910
|

Diff for: tests/ui/rfcs/rfc-2497-if-let-chains/avoid-invalid-mir.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected expression, found `let` statement
33
|
44
LL | !let y = 42;
55
| ^^^
6+
|
7+
= note: only supported directly in conditions of `if` and `while` expressions
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)