Skip to content

Commit 701b974

Browse files
committed
extract find_struct_error_after_field_looking_code
1 parent 8480b31 commit 701b974

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,9 +1780,7 @@ impl<'a> Parser<'a> {
17801780
if self.eat(&token::DotDot) {
17811781
let exp_span = self.prev_span;
17821782
match self.parse_expr() {
1783-
Ok(e) => {
1784-
base = Some(e);
1785-
}
1783+
Ok(e) => base = Some(e),
17861784
Err(mut e) => {
17871785
e.emit();
17881786
self.recover_stmt();
@@ -1792,24 +1790,9 @@ impl<'a> Parser<'a> {
17921790
break;
17931791
}
17941792

1795-
let mut recovery_field = None;
1796-
if let token::Ident(name, _) = self.token.kind {
1797-
if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) {
1798-
// Use in case of error after field-looking code: `S { foo: () with a }`.
1799-
recovery_field = Some(ast::Field {
1800-
ident: Ident::new(name, self.token.span),
1801-
span: self.token.span,
1802-
expr: self.mk_expr(self.token.span, ExprKind::Err, AttrVec::new()),
1803-
is_shorthand: false,
1804-
attrs: AttrVec::new(),
1805-
id: DUMMY_NODE_ID,
1806-
is_placeholder: false,
1807-
});
1808-
}
1809-
}
1810-
let mut parsed_field = None;
1811-
match self.parse_field() {
1812-
Ok(f) => parsed_field = Some(f),
1793+
let recovery_field = self.find_struct_error_after_field_looking_code();
1794+
let parsed_field = match self.parse_field() {
1795+
Ok(f) => Some(f),
18131796
Err(mut e) => {
18141797
e.span_label(struct_sp, "while parsing this struct");
18151798
e.emit();
@@ -1823,8 +1806,9 @@ impl<'a> Parser<'a> {
18231806
break;
18241807
}
18251808
}
1809+
None
18261810
}
1827-
}
1811+
};
18281812

18291813
match self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)]) {
18301814
Ok(_) => {
@@ -1847,7 +1831,26 @@ impl<'a> Parser<'a> {
18471831

18481832
let span = lo.to(self.token.span);
18491833
self.expect(&token::CloseDelim(token::Brace))?;
1850-
return Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs));
1834+
Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs))
1835+
}
1836+
1837+
/// Use in case of error after field-looking code: `S { foo: () with a }`.
1838+
fn find_struct_error_after_field_looking_code(&self) -> Option<Field> {
1839+
if let token::Ident(name, _) = self.token.kind {
1840+
if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) {
1841+
let span = self.token.span;
1842+
return Some(ast::Field {
1843+
ident: Ident::new(name, span),
1844+
span,
1845+
expr: self.mk_expr_err(span),
1846+
is_shorthand: false,
1847+
attrs: AttrVec::new(),
1848+
id: DUMMY_NODE_ID,
1849+
is_placeholder: false,
1850+
});
1851+
}
1852+
}
1853+
None
18511854
}
18521855

18531856
fn recover_struct_comma_after_dotdot(&mut self, span: Span) {

0 commit comments

Comments
 (0)