Tweak handling of "struct like start" where a struct isn't supported
This improves the case where someone tries to write a `match` expr where the patterns have type ascription syntax. Makes them less verbose, by giving up on the first encounter in the block, and makes them more accurate by only treating them as a struct literal if successfuly parsed as such.
This commit is contained in:
@@ -2748,28 +2748,7 @@ impl<'a> Parser<'a> {
|
||||
if token::Colon != self.token.kind {
|
||||
return first_pat;
|
||||
}
|
||||
if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..))
|
||||
|| !self.look_ahead(1, |token| token.is_non_reserved_ident())
|
||||
{
|
||||
let mut snapshot_type = self.create_snapshot_for_diagnostic();
|
||||
snapshot_type.bump(); // `:`
|
||||
match snapshot_type.parse_ty() {
|
||||
Err(inner_err) => {
|
||||
inner_err.cancel();
|
||||
}
|
||||
Ok(ty) => {
|
||||
let Err(mut err) = self.expected_one_of_not_found(&[], &[]) else {
|
||||
return first_pat;
|
||||
};
|
||||
err.span_label(ty.span, "specifying the type of a pattern isn't supported");
|
||||
self.restore_snapshot(snapshot_type);
|
||||
let span = first_pat.span.to(ty.span);
|
||||
first_pat = self.mk_pat(span, PatKind::Wild);
|
||||
err.emit();
|
||||
}
|
||||
}
|
||||
return first_pat;
|
||||
}
|
||||
|
||||
// The pattern looks like it might be a path with a `::` -> `:` typo:
|
||||
// `match foo { bar:baz => {} }`
|
||||
let colon_span = self.token.span;
|
||||
@@ -2857,7 +2836,13 @@ impl<'a> Parser<'a> {
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
} else {
|
||||
first_pat = self.mk_pat(new_span, PatKind::Wild);
|
||||
first_pat = self.mk_pat(
|
||||
new_span,
|
||||
PatKind::Err(
|
||||
self.dcx()
|
||||
.span_delayed_bug(colon_span, "recovered bad path pattern"),
|
||||
),
|
||||
);
|
||||
}
|
||||
self.restore_snapshot(snapshot_pat);
|
||||
}
|
||||
@@ -2870,7 +2855,14 @@ impl<'a> Parser<'a> {
|
||||
err.span_label(ty.span, "specifying the type of a pattern isn't supported");
|
||||
self.restore_snapshot(snapshot_type);
|
||||
let new_span = first_pat.span.to(ty.span);
|
||||
first_pat = self.mk_pat(new_span, PatKind::Wild);
|
||||
first_pat =
|
||||
self.mk_pat(
|
||||
new_span,
|
||||
PatKind::Err(self.dcx().span_delayed_bug(
|
||||
colon_span,
|
||||
"recovered bad pattern with type",
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
err.emit();
|
||||
|
||||
@@ -3612,36 +3612,55 @@ impl<'a> Parser<'a> {
|
||||
self.token.is_keyword(kw::Async) && self.is_gen_block(kw::Gen, 1)
|
||||
}
|
||||
|
||||
fn is_certainly_not_a_block(&self) -> bool {
|
||||
// `{ ident, ` and `{ ident: ` cannot start a block.
|
||||
self.look_ahead(1, |t| t.is_ident())
|
||||
&& self.look_ahead(2, |t| t == &token::Comma || t == &token::Colon)
|
||||
}
|
||||
|
||||
fn maybe_parse_struct_expr(
|
||||
&mut self,
|
||||
qself: &Option<Box<ast::QSelf>>,
|
||||
path: &ast::Path,
|
||||
) -> Option<PResult<'a, Box<Expr>>> {
|
||||
let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
|
||||
if struct_allowed || self.is_certainly_not_a_block() {
|
||||
if let Err(err) = self.expect(exp!(OpenBrace)) {
|
||||
return Some(Err(err));
|
||||
let is_ident = self.look_ahead(1, |t| t.is_ident());
|
||||
let is_comma = self.look_ahead(2, |t| t == &token::Comma);
|
||||
let is_colon = self.look_ahead(2, |t| t == &token::Colon);
|
||||
match (struct_allowed, is_ident, is_comma, is_colon) {
|
||||
(false, true, true, _) | (false, true, _, true) => {
|
||||
// We have something like `match foo { bar,` or `match foo { bar:`, which means the
|
||||
// user might have meant to write a struct literal as part of the `match`
|
||||
// discriminant.
|
||||
let snapshot = self.create_snapshot_for_diagnostic();
|
||||
if let Err(err) = self.expect(exp!(OpenBrace)) {
|
||||
return Some(Err(err));
|
||||
}
|
||||
match self.parse_expr_struct(qself.clone(), path.clone(), false) {
|
||||
Ok(expr) => {
|
||||
// This is a struct literal, but we don't accept them here.
|
||||
self.dcx().emit_err(errors::StructLiteralNotAllowedHere {
|
||||
span: expr.span,
|
||||
sub: errors::StructLiteralNotAllowedHereSugg {
|
||||
left: path.span.shrink_to_lo(),
|
||||
right: expr.span.shrink_to_hi(),
|
||||
},
|
||||
});
|
||||
Some(Ok(expr))
|
||||
}
|
||||
Err(err) => {
|
||||
// We couldn't parse a valid struct, rollback and let the parser emit an
|
||||
// error elsewhere.
|
||||
err.cancel();
|
||||
self.restore_snapshot(snapshot);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
let expr = self.parse_expr_struct(qself.clone(), path.clone(), true);
|
||||
if let (Ok(expr), false) = (&expr, struct_allowed) {
|
||||
// This is a struct literal, but we don't can't accept them here.
|
||||
self.dcx().emit_err(errors::StructLiteralNotAllowedHere {
|
||||
span: expr.span,
|
||||
sub: errors::StructLiteralNotAllowedHereSugg {
|
||||
left: path.span.shrink_to_lo(),
|
||||
right: expr.span.shrink_to_hi(),
|
||||
},
|
||||
});
|
||||
(true, _, _, _) => {
|
||||
// A struct is accepted here, try to parse it and rely on `parse_expr_struct` for
|
||||
// any kind of recovery.
|
||||
if let Err(err) = self.expect(exp!(OpenBrace)) {
|
||||
return Some(Err(err));
|
||||
}
|
||||
Some(self.parse_expr_struct(qself.clone(), path.clone(), true))
|
||||
}
|
||||
return Some(expr);
|
||||
(false, _, _, _) => None,
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(super) fn parse_struct_fields(
|
||||
|
||||
Reference in New Issue
Block a user