Skip to content

Commit 9f336ce

Browse files
committed
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.
1 parent 82796dd commit 9f336ce

File tree

6 files changed

+24
-76
lines changed

6 files changed

+24
-76
lines changed

compiler/rustc_parse/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,6 @@ parse_struct_literal_body_without_path =
757757
struct literal body without path
758758
.suggestion = you might have forgotten to add the struct literal inside the block
759759
760-
parse_struct_literal_needing_parens =
761-
invalid struct literal
762-
.suggestion = you might need to surround the struct literal with parentheses
763-
764760
parse_struct_literal_not_allowed_here = struct literals are not allowed here
765761
.suggestion = surround the struct literal with parentheses
766762

compiler/rustc_parse/src/errors.rs

-18
Original file line numberDiff line numberDiff line change
@@ -1272,24 +1272,6 @@ pub(crate) struct StructLiteralBodyWithoutPathSugg {
12721272
pub after: Span,
12731273
}
12741274

1275-
#[derive(Diagnostic)]
1276-
#[diag(parse_struct_literal_needing_parens)]
1277-
pub(crate) struct StructLiteralNeedingParens {
1278-
#[primary_span]
1279-
pub span: Span,
1280-
#[subdiagnostic]
1281-
pub sugg: StructLiteralNeedingParensSugg,
1282-
}
1283-
1284-
#[derive(Subdiagnostic)]
1285-
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
1286-
pub(crate) struct StructLiteralNeedingParensSugg {
1287-
#[suggestion_part(code = "(")]
1288-
pub before: Span,
1289-
#[suggestion_part(code = ")")]
1290-
pub after: Span,
1291-
}
1292-
12931275
#[derive(Diagnostic)]
12941276
#[diag(parse_unmatched_angle_brackets)]
12951277
pub(crate) struct UnmatchedAngleBrackets {

compiler/rustc_parse/src/parser/diagnostics.rs

+20-44
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ use crate::errors::{
4040
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
4141
IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
4242
QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
43-
StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens, StructLiteralNeedingParensSugg,
44-
SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator,
45-
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
43+
StructLiteralBodyWithoutPathSugg, SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma,
44+
TernaryOperator, UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
4645
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
4746
};
4847
use crate::parser::attr::InnerAttrPolicy;
@@ -949,7 +948,6 @@ impl<'a> Parser<'a> {
949948
lo: Span,
950949
s: BlockCheckMode,
951950
maybe_struct_name: token::Token,
952-
can_be_struct_literal: bool,
953951
) -> Option<PResult<'a, P<Block>>> {
954952
if self.token.is_ident() && self.look_ahead(1, |t| t == &token::Colon) {
955953
// We might be having a struct literal where people forgot to include the path:
@@ -971,49 +969,27 @@ impl<'a> Parser<'a> {
971969
// fn foo() -> Foo {
972970
// field: value,
973971
// }
974-
let guar = err.delay_as_bug();
972+
// Suggest:
973+
// fn foo() -> Foo { Path {
974+
// field: value,
975+
// } }
976+
err.cancel();
975977
self.restore_snapshot(snapshot);
976-
if maybe_struct_name.is_ident() && can_be_struct_literal {
977-
// Account for `if Example { a: one(), }.is_pos() {}`.
978-
// expand `before` so that we take care of module path such as:
979-
// `foo::Bar { ... } `
980-
// we expect to suggest `(foo::Bar { ... })` instead of `foo::(Bar { ... })`
981-
let sm = self.psess.source_map();
982-
let before = maybe_struct_name.span.shrink_to_lo();
983-
if let Ok(extend_before) = sm.span_extend_prev_while(before, |t| {
984-
t.is_alphanumeric() || t == ':' || t == '_'
985-
}) {
986-
Err(self.dcx().create_err(StructLiteralNeedingParens {
987-
span: maybe_struct_name.span.to(expr.span),
988-
sugg: StructLiteralNeedingParensSugg {
989-
before: extend_before.shrink_to_lo(),
990-
after: expr.span.shrink_to_hi(),
991-
},
992-
}))
993-
} else {
994-
return None;
995-
}
996-
} else {
997-
// Suggest:
998-
// fn foo() -> Foo { Path {
999-
// field: value,
1000-
// } }
1001-
self.dcx().emit_err(StructLiteralBodyWithoutPath {
1002-
span: expr.span,
1003-
sugg: StructLiteralBodyWithoutPathSugg {
1004-
before: expr.span.shrink_to_lo(),
1005-
after: expr.span.shrink_to_hi(),
1006-
},
1007-
});
1008-
Ok(self.mk_block(
1009-
thin_vec![self.mk_stmt_err(expr.span, guar)],
1010-
s,
1011-
lo.to(self.prev_token.span),
1012-
))
1013-
}
978+
let guar = self.dcx().emit_err(StructLiteralBodyWithoutPath {
979+
span: expr.span,
980+
sugg: StructLiteralBodyWithoutPathSugg {
981+
before: expr.span.shrink_to_lo(),
982+
after: expr.span.shrink_to_hi(),
983+
},
984+
});
985+
Ok(self.mk_block(
986+
thin_vec![self.mk_stmt_err(expr.span, guar)],
987+
s,
988+
lo.to(self.prev_token.span),
989+
))
1014990
}
1015991
(Err(err), Ok(tail)) => {
1016-
// We have a block tail that contains a somehow valid type ascription expr.
992+
// We have a block tail that contains a somehow valid expr.
1017993
err.cancel();
1018994
Ok(tail)
1019995
}

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,7 @@ impl<'a> Parser<'a> {
22962296
});
22972297
}
22982298

2299-
let (attrs, blk) = self.parse_block_common(lo, blk_mode, true, None)?;
2299+
let (attrs, blk) = self.parse_block_common(lo, blk_mode, None)?;
23002300
Ok(self.mk_expr_with_attrs(blk.span, ExprKind::Block(blk, opt_label), attrs))
23012301
}
23022302

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ impl<'a> Parser<'a> {
25292529
*sig_hi = self.prev_token.span;
25302530
(AttrVec::new(), None)
25312531
} else if self.check(exp!(OpenBrace)) || self.token.is_whole_block() {
2532-
self.parse_block_common(self.token.span, BlockCheckMode::Default, false, None)
2532+
self.parse_block_common(self.token.span, BlockCheckMode::Default, None)
25332533
.map(|(attrs, body)| (attrs, Some(body)))?
25342534
} else if self.token == token::Eq {
25352535
// Recover `fn foo() = $expr;`.

compiler/rustc_parse/src/parser/stmt.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ impl<'a> Parser<'a> {
668668
&mut self,
669669
loop_header: Option<Span>,
670670
) -> PResult<'a, (AttrVec, P<Block>)> {
671-
self.parse_block_common(self.token.span, BlockCheckMode::Default, true, loop_header)
671+
self.parse_block_common(self.token.span, BlockCheckMode::Default, loop_header)
672672
}
673673

674674
/// Parses a block. Inner attributes are allowed, block labels are not.
@@ -679,7 +679,6 @@ impl<'a> Parser<'a> {
679679
&mut self,
680680
lo: Span,
681681
blk_mode: BlockCheckMode,
682-
can_be_struct_literal: bool,
683682
loop_header: Option<Span>,
684683
) -> PResult<'a, (AttrVec, P<Block>)> {
685684
maybe_whole!(self, NtBlock, |block| (AttrVec::new(), block));
@@ -691,12 +690,7 @@ impl<'a> Parser<'a> {
691690
}
692691

693692
let attrs = self.parse_inner_attributes()?;
694-
let tail = match self.maybe_suggest_struct_literal(
695-
lo,
696-
blk_mode,
697-
maybe_ident,
698-
can_be_struct_literal,
699-
) {
693+
let tail = match self.maybe_suggest_struct_literal(lo, blk_mode, maybe_ident) {
700694
Some(tail) => tail?,
701695
None => self.parse_block_tail(lo, blk_mode, AttemptLocalParseRecovery::Yes)?,
702696
};

0 commit comments

Comments
 (0)