diff options
| author | bors <bors@rust-lang.org> | 2022-06-08 08:05:47 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-06-08 08:05:47 +0000 |
| commit | e45d9973b2665897a768312e971b82cc62633103 (patch) | |
| tree | 7010049e4a0bd89610767791bb05b7696aa27d8f /compiler/rustc_parse/src/parser | |
| parent | 64a7aa7016de32f4d991c30bfa40d3911e18a213 (diff) | |
| parent | 1660b4b70cfb43a03680f12d61dd89fb50e6a54e (diff) | |
| download | rust-e45d9973b2665897a768312e971b82cc62633103.tar.gz rust-e45d9973b2665897a768312e971b82cc62633103.zip | |
Auto merge of #97860 - Dylan-DPC:rollup-t3vxos8, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - #97595 (Remove unwrap from get_vtable) - #97597 (Preserve unused pointer to address casts) - #97819 (Recover `import` instead of `use` in item) - #97823 (Recover missing comma after match arm) - #97851 (Use repr(C) when depending on struct layout in ptr tests) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 51 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/item.rs | 69 |
2 files changed, 87 insertions, 33 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index b786c52e688..324e04b1981 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2718,13 +2718,12 @@ impl<'a> Parser<'a> { )); } this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)]) - .map_err(|mut err| { - match (sm.span_to_lines(expr.span), sm.span_to_lines(arm_start_span)) { - (Ok(ref expr_lines), Ok(ref arm_start_lines)) - if arm_start_lines.lines[0].end_col - == expr_lines.lines[0].end_col - && expr_lines.lines.len() == 2 - && this.token == token::FatArrow => + .or_else(|mut err| { + if this.token == token::FatArrow { + if let Ok(expr_lines) = sm.span_to_lines(expr.span) + && let Ok(arm_start_lines) = sm.span_to_lines(arm_start_span) + && arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col + && expr_lines.lines.len() == 2 { // We check whether there's any trailing code in the parse span, // if there isn't, we very likely have the following: @@ -2743,15 +2742,41 @@ impl<'a> Parser<'a> { ",".to_owned(), Applicability::MachineApplicable, ); + return Err(err); } - _ => { - err.span_label( - arrow_span, - "while parsing the `match` arm starting here", - ); + } else { + // FIXME(compiler-errors): We could also recover `; PAT =>` here + + // Try to parse a following `PAT =>`, if successful + // then we should recover. + let mut snapshot = this.create_snapshot_for_diagnostic(); + let pattern_follows = snapshot + .parse_pat_allow_top_alt( + None, + RecoverComma::Yes, + RecoverColon::Yes, + CommaRecoveryMode::EitherTupleOrPipe, + ) + .map_err(|err| err.cancel()) + .is_ok(); + if pattern_follows && snapshot.check(&TokenKind::FatArrow) { + err.cancel(); + this.struct_span_err( + hi.shrink_to_hi(), + "expected `,` following `match` arm", + ) + .span_suggestion( + hi.shrink_to_hi(), + "missing a comma here to end this `match` arm", + ",".to_owned(), + Applicability::MachineApplicable, + ) + .emit(); + return Ok(true); } } - err + err.span_label(arrow_span, "while parsing the `match` arm starting here"); + Err(err) })?; } else { this.eat(&token::Comma); diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 6720399aacb..48c3c467bec 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -204,25 +204,7 @@ impl<'a> Parser<'a> { let mut def = || mem::replace(def, Defaultness::Final); let info = if self.eat_keyword(kw::Use) { - // USE ITEM - let tree = self.parse_use_tree()?; - - // If wildcard or glob-like brace syntax doesn't have `;`, - // the user may not know `*` or `{}` should be the last. - if let Err(mut e) = self.expect_semi() { - match tree.kind { - UseTreeKind::Glob => { - e.note("the wildcard token must be last on the path"); - } - UseTreeKind::Nested(..) => { - e.note("glob-like brace syntax must be last on the path"); - } - _ => (), - } - return Err(e); - } - - (Ident::empty(), ItemKind::Use(tree)) + self.parse_use_item()? } else if self.check_fn_front_matter(def_final) { // FUNCTION ITEM let (ident, sig, generics, body) = self.parse_fn(attrs, fn_parse_mode, lo, vis)?; @@ -288,7 +270,12 @@ impl<'a> Parser<'a> { } else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() { // MACRO_RULES ITEM self.parse_item_macro_rules(vis, has_bang)? - } else if vis.kind.is_pub() && self.isnt_macro_invocation() { + } else if self.isnt_macro_invocation() + && (self.token.is_ident_named(Symbol::intern("import")) + || self.token.is_ident_named(Symbol::intern("using"))) + { + return self.recover_import_as_use(); + } else if self.isnt_macro_invocation() && vis.kind.is_pub() { self.recover_missing_kw_before_item()?; return Ok(None); } else if macros_allowed && self.check_path() { @@ -300,6 +287,48 @@ impl<'a> Parser<'a> { Ok(Some(info)) } + fn recover_import_as_use(&mut self) -> PResult<'a, Option<(Ident, ItemKind)>> { + let span = self.token.span; + let token_name = super::token_descr(&self.token); + let snapshot = self.create_snapshot_for_diagnostic(); + self.bump(); + match self.parse_use_item() { + Ok(u) => { + self.struct_span_err(span, format!("expected item, found {token_name}")) + .span_suggestion_short( + span, + "items are imported using the `use` keyword", + "use".to_owned(), + Applicability::MachineApplicable, + ) + .emit(); + Ok(Some(u)) + } + Err(e) => { + e.cancel(); + self.restore_snapshot(snapshot); + Ok(None) + } + } + } + + fn parse_use_item(&mut self) -> PResult<'a, (Ident, ItemKind)> { + let tree = self.parse_use_tree()?; + if let Err(mut e) = self.expect_semi() { + match tree.kind { + UseTreeKind::Glob => { + e.note("the wildcard token must be last on the path"); + } + UseTreeKind::Nested(..) => { + e.note("glob-like brace syntax must be last on the path"); + } + _ => (), + } + return Err(e); + } + Ok((Ident::empty(), ItemKind::Use(tree))) + } + /// When parsing a statement, would the start of a path be an item? pub(super) fn is_path_start_item(&mut self) -> bool { self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }` |
