about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/expr.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-08-10 06:09:10 +0000
committerbors <bors@rust-lang.org>2022-08-10 06:09:10 +0000
commit1603a70f82240ba2d27f72f964e36614d7620ad3 (patch)
tree92fec792de17c4f5ae761d3b9254ad1fa2a54d68 /compiler/rustc_parse/src/parser/expr.rs
parent0459d2fa736a556332ea9613ad0edf073107cb40 (diff)
parentf6ce6aba6e464644d9bb8cb21e30f9d09078df61 (diff)
downloadrust-1603a70f82240ba2d27f72f964e36614d7620ad3.tar.gz
rust-1603a70f82240ba2d27f72f964e36614d7620ad3.zip
Auto merge of #100356 - matthiaskrgr:rollup-he0vkjc, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - #99573 (Stabilize backtrace)
 - #100069 (Add error if link_ordinal used with unsupported link kind)
 - #100086 (Add more `// unit-test`s to MIR opt tests)
 - #100332 (Rename integer log* methods to ilog*)
 - #100334 (Suggest a missing semicolon before an array)
 - #100340 (Iterate generics_def_id_map in reverse order to fix P-critical issue)
 - #100345 (docs: remove repetition in `is_numeric` function docs)
 - #100352 (Update cargo)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src/parser/expr.rs')
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index e473f4d30cf..b8bd960a5b3 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -1258,8 +1258,11 @@ impl<'a> Parser<'a> {
 
     /// Parse an indexing expression `expr[...]`.
     fn parse_index_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
+        let prev_span = self.prev_token.span;
+        let open_delim_span = self.token.span;
         self.bump(); // `[`
         let index = self.parse_expr()?;
+        self.suggest_missing_semicolon_before_array(prev_span, open_delim_span)?;
         self.expect(&token::CloseDelim(Delimiter::Bracket))?;
         Ok(self.mk_expr(lo.to(self.prev_token.span), self.mk_index(base, index), AttrVec::new()))
     }
@@ -2056,6 +2059,45 @@ impl<'a> Parser<'a> {
         }
     }
 
+    fn suggest_missing_semicolon_before_array(
+        &self,
+        prev_span: Span,
+        open_delim_span: Span,
+    ) -> PResult<'a, ()> {
+        if self.token.kind == token::Comma {
+            let mut snapshot = self.create_snapshot_for_diagnostic();
+            snapshot.bump();
+            match snapshot.parse_seq_to_before_end(
+                &token::CloseDelim(Delimiter::Bracket),
+                SeqSep::trailing_allowed(token::Comma),
+                |p| p.parse_expr(),
+            ) {
+                Ok(_)
+                    // When the close delim is `)`, `token.kind` is expected to be `token::CloseDelim(Delimiter::Parenthesis)`,
+                    // but the actual `token.kind` is `token::CloseDelim(Delimiter::Bracket)`.
+                    // This is because the `token.kind` of the close delim is treated as the same as
+                    // that of the open delim in `TokenTreesReader::parse_token_tree`, even if the delimiters of them are different.
+                    // Therefore, `token.kind` should not be compared here.
+                    if snapshot
+                        .span_to_snippet(snapshot.token.span)
+                        .map_or(false, |snippet| snippet == "]") =>
+                {
+                    let mut err = self.struct_span_err(open_delim_span, "expected `;`, found `[`");
+                    err.span_suggestion_verbose(
+                        prev_span.shrink_to_hi(),
+                        "consider adding `;` here",
+                        ';',
+                        Applicability::MaybeIncorrect,
+                    );
+                    return Err(err);
+                }
+                Ok(_) => (),
+                Err(err) => err.cancel(),
+            }
+        }
+        Ok(())
+    }
+
     /// Parses a block or unsafe block.
     pub(super) fn parse_block_expr(
         &mut self,