Skip to content

Commit

Permalink
Handle methodcalls & operators in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ShE3py committed Dec 11, 2023
1 parent f66dcda commit c74db5a
Show file tree
Hide file tree
Showing 33 changed files with 905 additions and 99 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -766,12 +766,21 @@ parse_unexpected_const_param_declaration = unexpected `const` parameter declarat
parse_unexpected_default_value_for_lifetime_in_generic_parameters = unexpected default lifetime parameter
.label = lifetime parameters cannot have default values
parse_unexpected_expr_in_pat = expected pattern, found expression
.label = expressions are not allowed in patterns
parse_unexpected_if_with_if = unexpected `if` in the condition expression
.suggestion = remove the `if`
parse_unexpected_lifetime_in_pattern = unexpected lifetime `{$symbol}` in pattern
.suggestion = remove the lifetime
parse_unexpected_methodcall_in_pat = expected pattern, found method call
.label = method calls are not allowed in patterns
parse_unexpected_paren_in_range_pat = range pattern bounds cannot have parentheses
parse_unexpected_paren_in_range_pat_sugg = remove these parentheses
parse_unexpected_parentheses_in_for_head = unexpected parentheses surrounding `for` loop head
.suggestion = remove parentheses in `for` loop
Expand Down
39 changes: 38 additions & 1 deletion compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;

use rustc_ast::token::Token;
use rustc_ast::{Path, Visibility};
use rustc_errors::{AddToDiagnostic, Applicability, ErrorGuaranteed, IntoDiagnostic};
use rustc_errors::{AddToDiagnostic, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
Expand Down Expand Up @@ -2372,6 +2372,43 @@ pub(crate) struct ExpectedCommaAfterPatternField {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_unexpected_methodcall_in_pat)]
pub(crate) struct MethodCallInPattern {
#[primary_span]
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_unexpected_expr_in_pat)]
pub(crate) struct ExpressionInPattern {
#[primary_span]
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_unexpected_paren_in_range_pat)]
pub(crate) struct ParenInRangePat {
#[primary_span]
pub span: MultiSpan,
#[subdiagnostic]
pub sugg: ParenInRangePatSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
parse_unexpected_paren_in_range_pat_sugg,
applicability = "machine-applicable"
)]
pub(crate) struct ParenInRangePatSugg {
#[suggestion_part(code = "")]
pub start_span: Span,
#[suggestion_part(code = "")]
pub end_span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_return_types_use_thin_arrow)]
pub(crate) struct ReturnTypesUseThinArrow {
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,14 @@ impl<'a> Parser<'a> {
let is_almost_fat_arrow = TokenKind::FatArrow
.similar_tokens()
.is_some_and(|similar_tokens| similar_tokens.contains(&this.token.kind));
let mut result = if !is_fat_arrow && !is_almost_fat_arrow {

// this avoids the compiler saying that a `,` or `}` was expected even though
// the pattern isn't a never pattern (and thus an arm body is required)
let armless =
matches!(this.token.kind, token::Comma | token::CloseDelim(Delimiter::Brace))
|| (!is_fat_arrow && !is_almost_fat_arrow && pat.could_be_never_pattern());

let mut result = if armless {
// A pattern without a body, allowed for never patterns.
arm_body = None;
this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)])
Expand Down
Loading

0 comments on commit c74db5a

Please sign in to comment.