Skip to content

Commit

Permalink
Merge pull request #1214 from dtolnay/patstructrest
Browse files Browse the repository at this point in the history
Parse Struct { #[attr] .. } in pattern position
  • Loading branch information
dtolnay committed Sep 18, 2022
2 parents caa54bd + 0c3ff6c commit 2de2d8c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
35 changes: 19 additions & 16 deletions src/pat.rs
Expand Up @@ -400,11 +400,11 @@ pub mod parsing {
}

if input.peek(token::Brace) {
let pat = pat_struct(input, path)?;
let pat = pat_struct(begin.fork(), input, path)?;
if qself.is_some() {
Ok(Pat::Verbatim(verbatim::between(begin, input)))
} else {
Ok(Pat::Struct(pat))
Ok(pat)
}
} else if input.peek(token::Paren) {
let pat = pat_tuple_struct(input, path)?;
Expand Down Expand Up @@ -465,13 +465,23 @@ pub mod parsing {
})
}

fn pat_struct(input: ParseStream, path: Path) -> Result<PatStruct> {
fn pat_struct(begin: ParseBuffer, input: ParseStream, path: Path) -> Result<Pat> {
let content;
let brace_token = braced!(content in input);

let mut fields = Punctuated::new();
while !content.is_empty() && !content.peek(Token![..]) {
let value = content.call(field_pat)?;
let mut dot2_token = None;
while !content.is_empty() {
let attrs = content.call(Attribute::parse_outer)?;
if content.peek(Token![..]) {
dot2_token = Some(content.parse()?);
if !attrs.is_empty() {
return Ok(Pat::Verbatim(verbatim::between(begin, input)));
}
break;
}
let mut value = content.call(field_pat)?;
value.attrs = attrs;
fields.push_value(value);
if content.is_empty() {
break;
Expand All @@ -480,19 +490,13 @@ pub mod parsing {
fields.push_punct(punct);
}

let dot2_token = if fields.empty_or_trailing() && content.peek(Token![..]) {
Some(content.parse()?)
} else {
None
};

Ok(PatStruct {
Ok(Pat::Struct(PatStruct {
attrs: Vec::new(),
path,
brace_token,
fields,
dot2_token,
})
}))
}

impl Member {
Expand All @@ -505,7 +509,6 @@ pub mod parsing {
}

fn field_pat(input: ParseStream) -> Result<FieldPat> {
let attrs = input.call(Attribute::parse_outer)?;
let boxed: Option<Token![box]> = input.parse()?;
let by_ref: Option<Token![ref]> = input.parse()?;
let mutability: Option<Token![mut]> = input.parse()?;
Expand All @@ -515,7 +518,7 @@ pub mod parsing {
|| member.is_unnamed()
{
return Ok(FieldPat {
attrs,
attrs: Vec::new(),
member,
colon_token: input.parse()?,
pat: Box::new(multi_pat_with_leading_vert(input)?),
Expand Down Expand Up @@ -544,7 +547,7 @@ pub mod parsing {
}

Ok(FieldPat {
attrs,
attrs: Vec::new(),
member: Member::Named(ident),
colon_token: None,
pat: Box::new(pat),
Expand Down
3 changes: 0 additions & 3 deletions tests/repo/mod.rs
Expand Up @@ -14,9 +14,6 @@ const REVISION: &str = "98ad6a5519651af36e246c0335c964dd52c554ba";

#[rustfmt::skip]
static EXCLUDE_FILES: &[&str] = &[
// TODO: Struct { #[attr] .. } in pattern position
"src/tools/rust-analyzer/crates/parser/test_data/parser/inline/ok/0102_record_pat_field_list.rs",

// TODO: static async closures, for<> move closures
"src/tools/rust-analyzer/crates/parser/test_data/parser/inline/ok/0106_lambda_expr.rs",

Expand Down

0 comments on commit 2de2d8c

Please sign in to comment.