diff options
| author | Matthew Jasper <mjjasper1@gmail.com> | 2023-09-13 15:00:31 +0000 |
|---|---|---|
| committer | Matthew Jasper <mjjasper1@gmail.com> | 2023-09-13 15:00:31 +0000 |
| commit | e324a59eb6ba1a7883bf23ff42d425ca96960f2a (patch) | |
| tree | 47e9cdac618841a8a4ca59962db91bbb8419ac3e /compiler/rustc_parse | |
| parent | b011a0a13b4354080b3add0bb3f4445ddb8fd13b (diff) | |
| download | rust-e324a59eb6ba1a7883bf23ff42d425ca96960f2a.tar.gz rust-e324a59eb6ba1a7883bf23ff42d425ca96960f2a.zip | |
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
Diffstat (limited to 'compiler/rustc_parse')
| -rw-r--r-- | compiler/rustc_parse/messages.ftl | 1 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/errors.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 17 |
3 files changed, 15 insertions, 4 deletions
diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 66fa40e51d5..2c4bc7bb568 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -196,6 +196,7 @@ parse_expected_else_block = expected `{"{"}`, found {$first_tok} .suggestion = add an `if` if this is the condition of a chained `else if` statement parse_expected_expression_found_let = expected expression, found `let` statement + .note = only supported directly in conditions of `if` and `while` expressions .not_supported_or = `||` operators are not supported in let chain expressions .not_supported_parentheses = `let`s wrapped in parentheses are not supported in a context with let chains diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 10b659d811a..5d3ec683552 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -392,6 +392,7 @@ pub(crate) struct IfExpressionMissingCondition { #[derive(Diagnostic)] #[diag(parse_expected_expression_found_let)] +#[note] pub(crate) struct ExpectedExpressionFoundLet { #[primary_span] pub span: Span, diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 43cd8fe55df..f4cee3a661e 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2461,7 +2461,7 @@ impl<'a> Parser<'a> { let is_recovered = if !restrictions.contains(Restrictions::ALLOW_LET) { Some(self.sess.emit_err(errors::ExpectedExpressionFoundLet { span: self.token.span, - reason: ForbiddenLetReason::GenericForbidden, + reason: ForbiddenLetReason::OtherForbidden, })) } else { None @@ -3427,7 +3427,7 @@ impl<'a> Parser<'a> { #[derive(Clone, Copy, Subdiagnostic)] pub(crate) enum ForbiddenLetReason { /// `let` is not valid and the source environment is not important - GenericForbidden, + OtherForbidden, /// A let chain with the `||` operator #[note(parse_not_supported_or)] NotSupportedOr(#[primary_span] Span), @@ -3439,6 +3439,15 @@ pub(crate) enum ForbiddenLetReason { NotSupportedParentheses(#[primary_span] Span), } +/// Visitor to check for invalid/unstable use of `ExprKind::Let` that can't +/// easily be caught in parsing. For example: +/// +/// ```rust,ignore (example) +/// // Only know that the let isn't allowed once the `||` token is reached +/// if let Some(x) = y || true {} +/// // Only know that the let isn't allowed once the second `=` token is reached. +/// if let Some(x) = y && z = 1 {} +/// ``` struct CondChecker<'a> { parser: &'a Parser<'a>, forbid_let_reason: Option<ForbiddenLetReason>, @@ -3495,14 +3504,14 @@ impl MutVisitor for CondChecker<'_> { | ExprKind::Tup(_) | ExprKind::Paren(_) => { let forbid_let_reason = self.forbid_let_reason; - self.forbid_let_reason = Some(GenericForbidden); + self.forbid_let_reason = Some(OtherForbidden); noop_visit_expr(e, self); self.forbid_let_reason = forbid_let_reason; } ExprKind::Cast(ref mut op, _) | ExprKind::Type(ref mut op, _) => { let forbid_let_reason = self.forbid_let_reason; - self.forbid_let_reason = Some(GenericForbidden); + self.forbid_let_reason = Some(OtherForbidden); self.visit_expr(op); self.forbid_let_reason = forbid_let_reason; } |
