Skip to content

Commit c74db5a

Browse files
committed
Handle methodcalls & operators in patterns
1 parent f66dcda commit c74db5a

33 files changed

+905
-99
lines changed

compiler/rustc_parse/messages.ftl

+9
Original file line numberDiff line numberDiff line change
@@ -766,12 +766,21 @@ parse_unexpected_const_param_declaration = unexpected `const` parameter declarat
766766
parse_unexpected_default_value_for_lifetime_in_generic_parameters = unexpected default lifetime parameter
767767
.label = lifetime parameters cannot have default values
768768
769+
parse_unexpected_expr_in_pat = expected pattern, found expression
770+
.label = expressions are not allowed in patterns
771+
769772
parse_unexpected_if_with_if = unexpected `if` in the condition expression
770773
.suggestion = remove the `if`
771774
772775
parse_unexpected_lifetime_in_pattern = unexpected lifetime `{$symbol}` in pattern
773776
.suggestion = remove the lifetime
774777
778+
parse_unexpected_methodcall_in_pat = expected pattern, found method call
779+
.label = method calls are not allowed in patterns
780+
781+
parse_unexpected_paren_in_range_pat = range pattern bounds cannot have parentheses
782+
parse_unexpected_paren_in_range_pat_sugg = remove these parentheses
783+
775784
parse_unexpected_parentheses_in_for_head = unexpected parentheses surrounding `for` loop head
776785
.suggestion = remove parentheses in `for` loop
777786

compiler/rustc_parse/src/errors.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22

33
use rustc_ast::token::Token;
44
use rustc_ast::{Path, Visibility};
5-
use rustc_errors::{AddToDiagnostic, Applicability, ErrorGuaranteed, IntoDiagnostic};
5+
use rustc_errors::{AddToDiagnostic, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
66
use rustc_macros::{Diagnostic, Subdiagnostic};
77
use rustc_session::errors::ExprParenthesesNeeded;
88
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
@@ -2372,6 +2372,43 @@ pub(crate) struct ExpectedCommaAfterPatternField {
23722372
pub span: Span,
23732373
}
23742374

2375+
#[derive(Diagnostic)]
2376+
#[diag(parse_unexpected_methodcall_in_pat)]
2377+
pub(crate) struct MethodCallInPattern {
2378+
#[primary_span]
2379+
#[label]
2380+
pub span: Span,
2381+
}
2382+
2383+
#[derive(Diagnostic)]
2384+
#[diag(parse_unexpected_expr_in_pat)]
2385+
pub(crate) struct ExpressionInPattern {
2386+
#[primary_span]
2387+
#[label]
2388+
pub span: Span,
2389+
}
2390+
2391+
#[derive(Diagnostic)]
2392+
#[diag(parse_unexpected_paren_in_range_pat)]
2393+
pub(crate) struct ParenInRangePat {
2394+
#[primary_span]
2395+
pub span: MultiSpan,
2396+
#[subdiagnostic]
2397+
pub sugg: ParenInRangePatSugg,
2398+
}
2399+
2400+
#[derive(Subdiagnostic)]
2401+
#[multipart_suggestion(
2402+
parse_unexpected_paren_in_range_pat_sugg,
2403+
applicability = "machine-applicable"
2404+
)]
2405+
pub(crate) struct ParenInRangePatSugg {
2406+
#[suggestion_part(code = "")]
2407+
pub start_span: Span,
2408+
#[suggestion_part(code = "")]
2409+
pub end_span: Span,
2410+
}
2411+
23752412
#[derive(Diagnostic)]
23762413
#[diag(parse_return_types_use_thin_arrow)]
23772414
pub(crate) struct ReturnTypesUseThinArrow {

compiler/rustc_parse/src/parser/expr.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2915,7 +2915,14 @@ impl<'a> Parser<'a> {
29152915
let is_almost_fat_arrow = TokenKind::FatArrow
29162916
.similar_tokens()
29172917
.is_some_and(|similar_tokens| similar_tokens.contains(&this.token.kind));
2918-
let mut result = if !is_fat_arrow && !is_almost_fat_arrow {
2918+
2919+
// this avoids the compiler saying that a `,` or `}` was expected even though
2920+
// the pattern isn't a never pattern (and thus an arm body is required)
2921+
let armless =
2922+
matches!(this.token.kind, token::Comma | token::CloseDelim(Delimiter::Brace))
2923+
|| (!is_fat_arrow && !is_almost_fat_arrow && pat.could_be_never_pattern());
2924+
2925+
let mut result = if armless {
29192926
// A pattern without a body, allowed for never patterns.
29202927
arm_body = None;
29212928
this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)])

0 commit comments

Comments
 (0)