diff options
| author | Zachary S <zasample18+github@gmail.com> | 2025-03-17 00:25:15 -0500 |
|---|---|---|
| committer | Zachary S <zasample18+github@gmail.com> | 2025-03-17 01:59:37 -0500 |
| commit | f478853f425fc0207add653b48c49c937acaa94e (patch) | |
| tree | 3f7fe7d662114e8b9f812c37cfe967072858265d /compiler/rustc_parse/src/parser/stmt.rs | |
| parent | 227690a258492c84ae9927d18289208d0180e62f (diff) | |
| download | rust-f478853f425fc0207add653b48c49c937acaa94e.tar.gz rust-f478853f425fc0207add653b48c49c937acaa94e.zip | |
If a label is placed on the block of a loop instead of the header, suggest moving it to the header.
Diffstat (limited to 'compiler/rustc_parse/src/parser/stmt.rs')
| -rw-r--r-- | compiler/rustc_parse/src/parser/stmt.rs | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 0896bd88b4c..0fe247078d5 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -482,7 +482,7 @@ impl<'a> Parser<'a> { /// Parses a block. No inner attributes are allowed. pub fn parse_block(&mut self) -> PResult<'a, P<Block>> { - let (attrs, block) = self.parse_inner_attrs_and_block()?; + let (attrs, block) = self.parse_inner_attrs_and_block(None)?; if let [.., last] = &*attrs { let suggest_to_outer = match &last.kind { ast::AttrKind::Normal(attr) => attr.item.is_valid_for_outer_style(), @@ -660,22 +660,32 @@ impl<'a> Parser<'a> { Err(self.error_block_no_opening_brace_msg(Cow::from(msg))) } - /// Parses a block. Inner attributes are allowed. - pub(super) fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (AttrVec, P<Block>)> { - self.parse_block_common(self.token.span, BlockCheckMode::Default, true) + /// Parses a block. Inner attributes are allowed, block labels are not. + /// + /// If `loop_header` is `Some` and an unexpected block label is encountered, + /// it is suggested to be moved just before `loop_header`, else it is suggested to be removed. + pub(super) fn parse_inner_attrs_and_block( + &mut self, + loop_header: Option<Span>, + ) -> PResult<'a, (AttrVec, P<Block>)> { + self.parse_block_common(self.token.span, BlockCheckMode::Default, true, loop_header) } - /// Parses a block. Inner attributes are allowed. + /// Parses a block. Inner attributes are allowed, block labels are not. + /// + /// If `loop_header` is `Some` and an unexpected block label is encountered, + /// it is suggested to be moved just before `loop_header`, else it is suggested to be removed. pub(super) fn parse_block_common( &mut self, lo: Span, blk_mode: BlockCheckMode, can_be_struct_literal: bool, + loop_header: Option<Span>, ) -> PResult<'a, (AttrVec, P<Block>)> { maybe_whole!(self, NtBlock, |block| (AttrVec::new(), block)); let maybe_ident = self.prev_token.clone(); - self.maybe_recover_unexpected_block_label(); + self.maybe_recover_unexpected_block_label(loop_header); if !self.eat(exp!(OpenBrace)) { return self.error_block_no_opening_brace(); } |
