Skip to content

Commit e77b9d3

Browse files
committed
refactor parse_field
1 parent a916ac2 commit e77b9d3

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,48 +1866,51 @@ impl<'a> Parser<'a> {
18661866

18671867
/// Parses `ident (COLON expr)?`.
18681868
fn parse_field(&mut self) -> PResult<'a, Field> {
1869-
let attrs = self.parse_outer_attributes()?;
1869+
let attrs = self.parse_outer_attributes()?.into();
18701870
let lo = self.token.span;
18711871

18721872
// Check if a colon exists one ahead. This means we're parsing a fieldname.
1873-
let (fieldname, expr, is_shorthand) =
1874-
if self.look_ahead(1, |t| t == &token::Colon || t == &token::Eq) {
1875-
let fieldname = self.parse_field_name()?;
1876-
1877-
// Check for an equals token. This means the source incorrectly attempts to
1878-
// initialize a field with an eq rather than a colon.
1879-
if self.token == token::Eq {
1880-
self.diagnostic()
1881-
.struct_span_err(self.token.span, "expected `:`, found `=`")
1882-
.span_suggestion(
1883-
fieldname.span.shrink_to_hi().to(self.token.span),
1884-
"replace equals symbol with a colon",
1885-
":".to_string(),
1886-
Applicability::MachineApplicable,
1887-
)
1888-
.emit();
1889-
}
1890-
self.bump(); // `:`
1891-
(fieldname, self.parse_expr()?, false)
1892-
} else {
1893-
let fieldname = self.parse_ident_common(false)?;
1894-
1895-
// Mimic `x: x` for the `x` field shorthand.
1896-
let path = ast::Path::from_ident(fieldname);
1897-
let expr = self.mk_expr(fieldname.span, ExprKind::Path(None, path), AttrVec::new());
1898-
(fieldname, expr, true)
1899-
};
1873+
let is_shorthand = !self.look_ahead(1, |t| t == &token::Colon || t == &token::Eq);
1874+
let (ident, expr) = if is_shorthand {
1875+
// Mimic `x: x` for the `x` field shorthand.
1876+
let ident = self.parse_ident_common(false)?;
1877+
let path = ast::Path::from_ident(ident);
1878+
(ident, self.mk_expr(ident.span, ExprKind::Path(None, path), AttrVec::new()))
1879+
} else {
1880+
let ident = self.parse_field_name()?;
1881+
self.error_on_eq_field_init(ident);
1882+
self.bump(); // `:`
1883+
(ident, self.parse_expr()?)
1884+
};
19001885
Ok(ast::Field {
1901-
ident: fieldname,
1886+
ident,
19021887
span: lo.to(expr.span),
19031888
expr,
19041889
is_shorthand,
1905-
attrs: attrs.into(),
1890+
attrs,
19061891
id: DUMMY_NODE_ID,
19071892
is_placeholder: false,
19081893
})
19091894
}
19101895

1896+
/// Check for `=`. This means the source incorrectly attempts to
1897+
/// initialize a field with an eq rather than a colon.
1898+
fn error_on_eq_field_init(&self, field_name: Ident) {
1899+
if self.token != token::Eq {
1900+
return;
1901+
}
1902+
1903+
self.diagnostic()
1904+
.struct_span_err(self.token.span, "expected `:`, found `=`")
1905+
.span_suggestion(
1906+
field_name.span.shrink_to_hi().to(self.token.span),
1907+
"replace equals symbol with a colon",
1908+
":".to_string(),
1909+
Applicability::MachineApplicable,
1910+
)
1911+
.emit();
1912+
}
1913+
19111914
fn err_dotdotdot_syntax(&self, span: Span) {
19121915
self.struct_span_err(span, "unexpected token: `...`")
19131916
.span_suggestion(

0 commit comments

Comments
 (0)