Remove now unreachable parse recovery code
StructLiteralNeedingParens is no longer reachable always giving precedence to StructLiteralNotAllowedHere. As an aside: The former error struct shouldn't've existed in the first place. We should've just used the latter in this branch.
This commit is contained in:
parent
82796dd858
commit
9f336ce2eb
6 changed files with 24 additions and 76 deletions
|
@ -757,10 +757,6 @@ parse_struct_literal_body_without_path =
|
||||||
struct literal body without path
|
struct literal body without path
|
||||||
.suggestion = you might have forgotten to add the struct literal inside the block
|
.suggestion = you might have forgotten to add the struct literal inside the block
|
||||||
|
|
||||||
parse_struct_literal_needing_parens =
|
|
||||||
invalid struct literal
|
|
||||||
.suggestion = you might need to surround the struct literal with parentheses
|
|
||||||
|
|
||||||
parse_struct_literal_not_allowed_here = struct literals are not allowed here
|
parse_struct_literal_not_allowed_here = struct literals are not allowed here
|
||||||
.suggestion = surround the struct literal with parentheses
|
.suggestion = surround the struct literal with parentheses
|
||||||
|
|
||||||
|
|
|
@ -1272,24 +1272,6 @@ pub(crate) struct StructLiteralBodyWithoutPathSugg {
|
||||||
pub after: Span,
|
pub after: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
|
||||||
#[diag(parse_struct_literal_needing_parens)]
|
|
||||||
pub(crate) struct StructLiteralNeedingParens {
|
|
||||||
#[primary_span]
|
|
||||||
pub span: Span,
|
|
||||||
#[subdiagnostic]
|
|
||||||
pub sugg: StructLiteralNeedingParensSugg,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Subdiagnostic)]
|
|
||||||
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
|
|
||||||
pub(crate) struct StructLiteralNeedingParensSugg {
|
|
||||||
#[suggestion_part(code = "(")]
|
|
||||||
pub before: Span,
|
|
||||||
#[suggestion_part(code = ")")]
|
|
||||||
pub after: Span,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(parse_unmatched_angle_brackets)]
|
#[diag(parse_unmatched_angle_brackets)]
|
||||||
pub(crate) struct UnmatchedAngleBrackets {
|
pub(crate) struct UnmatchedAngleBrackets {
|
||||||
|
|
|
@ -40,9 +40,8 @@ use crate::errors::{
|
||||||
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
|
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
|
||||||
IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
|
IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
|
||||||
QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
|
QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
|
||||||
StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens, StructLiteralNeedingParensSugg,
|
StructLiteralBodyWithoutPathSugg, SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma,
|
||||||
SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator,
|
TernaryOperator, UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
|
||||||
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
|
|
||||||
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
|
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
|
||||||
};
|
};
|
||||||
use crate::parser::attr::InnerAttrPolicy;
|
use crate::parser::attr::InnerAttrPolicy;
|
||||||
|
@ -949,7 +948,6 @@ impl<'a> Parser<'a> {
|
||||||
lo: Span,
|
lo: Span,
|
||||||
s: BlockCheckMode,
|
s: BlockCheckMode,
|
||||||
maybe_struct_name: token::Token,
|
maybe_struct_name: token::Token,
|
||||||
can_be_struct_literal: bool,
|
|
||||||
) -> Option<PResult<'a, P<Block>>> {
|
) -> Option<PResult<'a, P<Block>>> {
|
||||||
if self.token.is_ident() && self.look_ahead(1, |t| t == &token::Colon) {
|
if self.token.is_ident() && self.look_ahead(1, |t| t == &token::Colon) {
|
||||||
// We might be having a struct literal where people forgot to include the path:
|
// We might be having a struct literal where people forgot to include the path:
|
||||||
|
@ -971,34 +969,13 @@ impl<'a> Parser<'a> {
|
||||||
// fn foo() -> Foo {
|
// fn foo() -> Foo {
|
||||||
// field: value,
|
// field: value,
|
||||||
// }
|
// }
|
||||||
let guar = err.delay_as_bug();
|
|
||||||
self.restore_snapshot(snapshot);
|
|
||||||
if maybe_struct_name.is_ident() && can_be_struct_literal {
|
|
||||||
// Account for `if Example { a: one(), }.is_pos() {}`.
|
|
||||||
// expand `before` so that we take care of module path such as:
|
|
||||||
// `foo::Bar { ... } `
|
|
||||||
// we expect to suggest `(foo::Bar { ... })` instead of `foo::(Bar { ... })`
|
|
||||||
let sm = self.psess.source_map();
|
|
||||||
let before = maybe_struct_name.span.shrink_to_lo();
|
|
||||||
if let Ok(extend_before) = sm.span_extend_prev_while(before, |t| {
|
|
||||||
t.is_alphanumeric() || t == ':' || t == '_'
|
|
||||||
}) {
|
|
||||||
Err(self.dcx().create_err(StructLiteralNeedingParens {
|
|
||||||
span: maybe_struct_name.span.to(expr.span),
|
|
||||||
sugg: StructLiteralNeedingParensSugg {
|
|
||||||
before: extend_before.shrink_to_lo(),
|
|
||||||
after: expr.span.shrink_to_hi(),
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Suggest:
|
// Suggest:
|
||||||
// fn foo() -> Foo { Path {
|
// fn foo() -> Foo { Path {
|
||||||
// field: value,
|
// field: value,
|
||||||
// } }
|
// } }
|
||||||
self.dcx().emit_err(StructLiteralBodyWithoutPath {
|
err.cancel();
|
||||||
|
self.restore_snapshot(snapshot);
|
||||||
|
let guar = self.dcx().emit_err(StructLiteralBodyWithoutPath {
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
sugg: StructLiteralBodyWithoutPathSugg {
|
sugg: StructLiteralBodyWithoutPathSugg {
|
||||||
before: expr.span.shrink_to_lo(),
|
before: expr.span.shrink_to_lo(),
|
||||||
|
@ -1011,9 +988,8 @@ impl<'a> Parser<'a> {
|
||||||
lo.to(self.prev_token.span),
|
lo.to(self.prev_token.span),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
(Err(err), Ok(tail)) => {
|
(Err(err), Ok(tail)) => {
|
||||||
// We have a block tail that contains a somehow valid type ascription expr.
|
// We have a block tail that contains a somehow valid expr.
|
||||||
err.cancel();
|
err.cancel();
|
||||||
Ok(tail)
|
Ok(tail)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2296,7 +2296,7 @@ impl<'a> Parser<'a> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let (attrs, blk) = self.parse_block_common(lo, blk_mode, true, None)?;
|
let (attrs, blk) = self.parse_block_common(lo, blk_mode, None)?;
|
||||||
Ok(self.mk_expr_with_attrs(blk.span, ExprKind::Block(blk, opt_label), attrs))
|
Ok(self.mk_expr_with_attrs(blk.span, ExprKind::Block(blk, opt_label), attrs))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2529,7 +2529,7 @@ impl<'a> Parser<'a> {
|
||||||
*sig_hi = self.prev_token.span;
|
*sig_hi = self.prev_token.span;
|
||||||
(AttrVec::new(), None)
|
(AttrVec::new(), None)
|
||||||
} else if self.check(exp!(OpenBrace)) || self.token.is_whole_block() {
|
} else if self.check(exp!(OpenBrace)) || self.token.is_whole_block() {
|
||||||
self.parse_block_common(self.token.span, BlockCheckMode::Default, false, None)
|
self.parse_block_common(self.token.span, BlockCheckMode::Default, None)
|
||||||
.map(|(attrs, body)| (attrs, Some(body)))?
|
.map(|(attrs, body)| (attrs, Some(body)))?
|
||||||
} else if self.token == token::Eq {
|
} else if self.token == token::Eq {
|
||||||
// Recover `fn foo() = $expr;`.
|
// Recover `fn foo() = $expr;`.
|
||||||
|
|
|
@ -668,7 +668,7 @@ impl<'a> Parser<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
loop_header: Option<Span>,
|
loop_header: Option<Span>,
|
||||||
) -> PResult<'a, (AttrVec, P<Block>)> {
|
) -> PResult<'a, (AttrVec, P<Block>)> {
|
||||||
self.parse_block_common(self.token.span, BlockCheckMode::Default, true, loop_header)
|
self.parse_block_common(self.token.span, BlockCheckMode::Default, loop_header)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a block. Inner attributes are allowed, block labels are not.
|
/// Parses a block. Inner attributes are allowed, block labels are not.
|
||||||
|
@ -679,7 +679,6 @@ impl<'a> Parser<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
lo: Span,
|
lo: Span,
|
||||||
blk_mode: BlockCheckMode,
|
blk_mode: BlockCheckMode,
|
||||||
can_be_struct_literal: bool,
|
|
||||||
loop_header: Option<Span>,
|
loop_header: Option<Span>,
|
||||||
) -> PResult<'a, (AttrVec, P<Block>)> {
|
) -> PResult<'a, (AttrVec, P<Block>)> {
|
||||||
maybe_whole!(self, NtBlock, |block| (AttrVec::new(), block));
|
maybe_whole!(self, NtBlock, |block| (AttrVec::new(), block));
|
||||||
|
@ -691,12 +690,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let attrs = self.parse_inner_attributes()?;
|
let attrs = self.parse_inner_attributes()?;
|
||||||
let tail = match self.maybe_suggest_struct_literal(
|
let tail = match self.maybe_suggest_struct_literal(lo, blk_mode, maybe_ident) {
|
||||||
lo,
|
|
||||||
blk_mode,
|
|
||||||
maybe_ident,
|
|
||||||
can_be_struct_literal,
|
|
||||||
) {
|
|
||||||
Some(tail) => tail?,
|
Some(tail) => tail?,
|
||||||
None => self.parse_block_tail(lo, blk_mode, AttemptLocalParseRecovery::Yes)?,
|
None => self.parse_block_tail(lo, blk_mode, AttemptLocalParseRecovery::Yes)?,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue