diff options
| author | bors <bors@rust-lang.org> | 2022-03-18 00:35:19 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-03-18 00:35:19 +0000 |
| commit | cd119057160cedea245aa2679add56723f3dc784 (patch) | |
| tree | 21335c52669bb2665024cf37adf8d892925d9d31 /compiler/rustc_parse/src/parser | |
| parent | 4ca56d2b7bbe275bc6c9f3cd698c6e0719a07182 (diff) | |
| parent | 4493826d07bf38cca058b4d9e75bce14ceeeaab9 (diff) | |
| download | rust-cd119057160cedea245aa2679add56723f3dc784.tar.gz rust-cd119057160cedea245aa2679add56723f3dc784.zip | |
Auto merge of #95056 - Dylan-DPC:rollup-swtuw2n, r=Dylan-DPC
Rollup of 10 pull requests
Successful merges:
- #91133 (Improve `unsafe` diagnostic)
- #93222 (Make ErrorReported impossible to construct outside `rustc_errors`)
- #93745 (Stabilize ADX target feature)
- #94309 ([generator_interior] Be more precise with scopes of borrowed places)
- #94698 (Remove redundant code from copy-suggestions)
- #94731 (Suggest adding `{ .. }` around a const function call with arguments)
- #94960 (Fix many spelling mistakes)
- #94982 (Add deprecated_safe feature gate and attribute, cc #94978)
- #94997 (debuginfo: Fix ICE when generating name for type that produces a layout error.)
- #95000 (Fixed wrong type name in comment)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/attr.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 87 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 22 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/item.rs | 36 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/path.rs | 13 |
5 files changed, 117 insertions, 43 deletions
diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index 8f759ae84fa..754ae12bbea 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -151,7 +151,7 @@ impl<'a> Parser<'a> { span: Span, attr_type: OuterAttributeType, ) -> Option<Span> { - let mut snapshot = self.clone(); + let mut snapshot = self.create_snapshot_for_diagnostic(); let lo = span.lo() + BytePos(match attr_type { OuterAttributeType::Attribute => 1, diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 1909e9ee749..b5b628a3f55 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -5,6 +5,7 @@ use super::{ SemiColonMode, SeqSep, TokenExpectType, TokenType, }; +use crate::lexer::UnmatchedBrace; use rustc_ast as ast; use rustc_ast::ptr::P; use rustc_ast::token::{self, Lit, LitKind, TokenKind}; @@ -21,6 +22,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Handler, PResult}; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, Ident}; use rustc_span::{MultiSpan, Span, SpanSnippetError, DUMMY_SP}; +use std::ops::{Deref, DerefMut}; use std::mem::take; @@ -154,6 +156,28 @@ impl AttemptLocalParseRecovery { } } +// SnapshotParser is used to create a snapshot of the parser +// without causing duplicate errors being emitted when the `Parser` +// is dropped. +pub(super) struct SnapshotParser<'a> { + parser: Parser<'a>, + unclosed_delims: Vec<UnmatchedBrace>, +} + +impl<'a> Deref for SnapshotParser<'a> { + type Target = Parser<'a>; + + fn deref(&self) -> &Self::Target { + &self.parser + } +} + +impl<'a> DerefMut for SnapshotParser<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.parser + } +} + impl<'a> Parser<'a> { pub(super) fn span_err<S: Into<MultiSpan>>( &self, @@ -179,6 +203,25 @@ impl<'a> Parser<'a> { &self.sess.span_diagnostic } + /// Relace `self` with `snapshot.parser` and extend `unclosed_delims` with `snapshot.unclosed_delims`. + /// This is to avoid losing unclosed delims errors `create_snapshot_for_diagnostic` clears. + pub(super) fn restore_snapshot(&mut self, snapshot: SnapshotParser<'a>) { + *self = snapshot.parser; + self.unclosed_delims.extend(snapshot.unclosed_delims.clone()); + } + + /// Create a snapshot of the `Parser`. + pub(super) fn create_snapshot_for_diagnostic(&self) -> SnapshotParser<'a> { + let mut snapshot = self.clone(); + let unclosed_delims = self.unclosed_delims.clone(); + // Clear `unclosed_delims` in snapshot to avoid + // duplicate errors being emitted when the `Parser` + // is dropped (which may or may not happen, depending + // if the parsing the snapshot is created for is successful) + snapshot.unclosed_delims.clear(); + SnapshotParser { parser: snapshot, unclosed_delims } + } + pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> { self.sess.source_map().span_to_snippet(span) } @@ -438,7 +481,7 @@ impl<'a> Parser<'a> { // fn foo() -> Foo { // field: value, // } - let mut snapshot = self.clone(); + let mut snapshot = self.create_snapshot_for_diagnostic(); let path = Path { segments: vec![], span: self.prev_token.span.shrink_to_lo(), tokens: None }; let struct_expr = snapshot.parse_struct_expr(None, path, AttrVec::new(), false); @@ -464,7 +507,7 @@ impl<'a> Parser<'a> { Applicability::MaybeIncorrect, ) .emit(); - *self = snapshot; + self.restore_snapshot(snapshot); let mut tail = self.mk_block( vec![self.mk_stmt_err(expr.span)], s, @@ -678,7 +721,7 @@ impl<'a> Parser<'a> { /// angle brackets. pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) { if token::ModSep == self.token.kind && segment.args.is_none() { - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); self.bump(); let lo = self.token.span; match self.parse_angle_args(None) { @@ -712,14 +755,14 @@ impl<'a> Parser<'a> { .emit(); } else { // This doesn't look like an invalid turbofish, can't recover parse state. - *self = snapshot; + self.restore_snapshot(snapshot); } } Err(err) => { // We couldn't parse generic parameters, unlikely to be a turbofish. Rely on // generic parse error instead. err.cancel(); - *self = snapshot; + self.restore_snapshot(snapshot); } } } @@ -825,7 +868,7 @@ impl<'a> Parser<'a> { // `x == y < z` (BinOpKind::Eq, AssocOp::Less | AssocOp::LessEqual | AssocOp::Greater | AssocOp::GreaterEqual) => { // Consume `z`/outer-op-rhs. - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); match self.parse_expr() { Ok(r2) => { // We are sure that outer-op-rhs could be consumed, the suggestion is @@ -835,14 +878,14 @@ impl<'a> Parser<'a> { } Err(expr_err) => { expr_err.cancel(); - *self = snapshot; + self.restore_snapshot(snapshot); false } } } // `x > y == z` (BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge, AssocOp::Equal) => { - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); // At this point it is always valid to enclose the lhs in parentheses, no // further checks are necessary. match self.parse_expr() { @@ -852,7 +895,7 @@ impl<'a> Parser<'a> { } Err(expr_err) => { expr_err.cancel(); - *self = snapshot; + self.restore_snapshot(snapshot); false } } @@ -917,7 +960,7 @@ impl<'a> Parser<'a> { || outer_op.node == AssocOp::Greater { if outer_op.node == AssocOp::Less { - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); self.bump(); // So far we have parsed `foo<bar<`, consume the rest of the type args. let modifiers = @@ -929,7 +972,7 @@ impl<'a> Parser<'a> { { // We don't have `foo< bar >(` or `foo< bar >::`, so we rewind the // parser and bail out. - *self = snapshot.clone(); + self.restore_snapshot(snapshot); } } return if token::ModSep == self.token.kind { @@ -937,7 +980,7 @@ impl<'a> Parser<'a> { // `foo< bar >::` suggest(&mut err); - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); self.bump(); // `::` // Consume the rest of the likely `foo<bar>::new()` or return at `foo<bar>`. @@ -954,7 +997,7 @@ impl<'a> Parser<'a> { expr_err.cancel(); // Not entirely sure now, but we bubble the error up with the // suggestion. - *self = snapshot; + self.restore_snapshot(snapshot); Err(err) } } @@ -1008,7 +1051,7 @@ impl<'a> Parser<'a> { } fn consume_fn_args(&mut self) -> Result<(), ()> { - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); self.bump(); // `(` // Consume the fn call arguments. @@ -1018,7 +1061,7 @@ impl<'a> Parser<'a> { if self.token.kind == token::Eof { // Not entirely sure that what we consumed were fn arguments, rollback. - *self = snapshot; + self.restore_snapshot(snapshot); Err(()) } else { // 99% certain that the suggestion is correct, continue parsing. @@ -1959,12 +2002,12 @@ impl<'a> Parser<'a> { } fn recover_const_param_decl(&mut self, ty_generics: Option<&Generics>) -> Option<GenericArg> { - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); let param = match self.parse_const_param(vec![]) { Ok(param) => param, Err(err) => { err.cancel(); - *self = snapshot; + self.restore_snapshot(snapshot); return None; } }; @@ -2056,7 +2099,7 @@ impl<'a> Parser<'a> { // We perform these checks and early return to avoid taking a snapshot unnecessarily. return Err(err); } - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); if is_op_or_dot { self.bump(); } @@ -2101,7 +2144,7 @@ impl<'a> Parser<'a> { err.cancel(); } } - *self = snapshot; + self.restore_snapshot(snapshot); Err(err) } @@ -2161,7 +2204,7 @@ impl<'a> Parser<'a> { let span = self.token.span; // We only emit "unexpected `:`" error here if we can successfully parse the // whole pattern correctly in that case. - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); // Create error for "unexpected `:`". match self.expected_one_of_not_found(&[], &[]) { @@ -2173,7 +2216,7 @@ impl<'a> Parser<'a> { // reasonable error. inner_err.cancel(); err.cancel(); - *self = snapshot; + self.restore_snapshot(snapshot); } Ok(mut pat) => { // We've parsed the rest of the pattern. @@ -2252,7 +2295,7 @@ impl<'a> Parser<'a> { } _ => { // Carry on as if we had not done anything. This should be unreachable. - *self = snapshot; + self.restore_snapshot(snapshot); } }; first_pat diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 550d79a898c..ef006d5fcda 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -703,7 +703,7 @@ impl<'a> Parser<'a> { ExprKind::Path(None, ast::Path { segments, .. }), TokenKind::Ident(kw::For | kw::Loop | kw::While, false), ) if segments.len() == 1 => { - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); let label = Label { ident: Ident::from_str_and_span( &format!("'{}", segments[0].ident), @@ -725,7 +725,7 @@ impl<'a> Parser<'a> { } Err(err) => { err.cancel(); - *self = snapshot; + self.restore_snapshot(snapshot); } } } @@ -1885,7 +1885,7 @@ impl<'a> Parser<'a> { lo: Span, attrs: AttrVec, ) -> Option<P<Expr>> { - let mut snapshot = self.clone(); + let mut snapshot = self.create_snapshot_for_diagnostic(); match snapshot.parse_array_or_repeat_expr(attrs, token::Brace) { Ok(arr) => { let hi = snapshot.prev_token.span; @@ -1901,7 +1901,7 @@ impl<'a> Parser<'a> { .note("to define an array, one would use square brackets instead of curly braces") .emit(); - *self = snapshot; + self.restore_snapshot(snapshot); Some(self.mk_expr_err(arr.span)) } Err(e) => { @@ -2369,7 +2369,7 @@ impl<'a> Parser<'a> { if self.token.kind != token::Semi { return None; } - let start_snapshot = self.clone(); + let start_snapshot = self.create_snapshot_for_diagnostic(); let semi_sp = self.token.span; self.bump(); // `;` let mut stmts = @@ -2417,15 +2417,15 @@ impl<'a> Parser<'a> { return Some(err(self, stmts)); } if self.token.kind == token::Comma { - *self = start_snapshot; + self.restore_snapshot(start_snapshot); return None; } - let pre_pat_snapshot = self.clone(); + let pre_pat_snapshot = self.create_snapshot_for_diagnostic(); match self.parse_pat_no_top_alt(None) { Ok(_pat) => { if self.token.kind == token::FatArrow { // Reached arm end. - *self = pre_pat_snapshot; + self.restore_snapshot(pre_pat_snapshot); return Some(err(self, stmts)); } } @@ -2434,21 +2434,21 @@ impl<'a> Parser<'a> { } } - *self = pre_pat_snapshot; + self.restore_snapshot(pre_pat_snapshot); match self.parse_stmt_without_recovery(true, ForceCollect::No) { // Consume statements for as long as possible. Ok(Some(stmt)) => { stmts.push(stmt); } Ok(None) => { - *self = start_snapshot; + self.restore_snapshot(start_snapshot); break; } // We couldn't parse either yet another statement missing it's // enclosing block nor the next arm's pattern or closing brace. Err(stmt_err) => { stmt_err.cancel(); - *self = start_snapshot; + self.restore_snapshot(start_snapshot); break; } } diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index b582f060395..e949059099c 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -212,10 +212,10 @@ impl<'a> Parser<'a> { if let Err(mut e) = self.expect_semi() { match tree.kind { UseTreeKind::Glob => { - e.note("the wildcard token must be last on the path").emit(); + 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").emit(); + e.note("glob-like brace syntax must be last on the path"); } _ => (), } @@ -999,10 +999,32 @@ impl<'a> Parser<'a> { attrs: &mut Vec<Attribute>, unsafety: Unsafe, ) -> PResult<'a, ItemInfo> { + let sp_start = self.prev_token.span; let abi = self.parse_abi(); // ABI? - let items = self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?; - let module = ast::ForeignMod { unsafety, abi, items }; - Ok((Ident::empty(), ItemKind::ForeignMod(module))) + match self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No)) { + Ok(items) => { + let module = ast::ForeignMod { unsafety, abi, items }; + Ok((Ident::empty(), ItemKind::ForeignMod(module))) + } + Err(mut err) => { + let current_qual_sp = self.prev_token.span; + let current_qual_sp = current_qual_sp.to(sp_start); + if let Ok(current_qual) = self.span_to_snippet(current_qual_sp) { + if err.message() == "expected `{`, found keyword `unsafe`" { + let invalid_qual_sp = self.token.uninterpolated_span(); + let invalid_qual = self.span_to_snippet(invalid_qual_sp).unwrap(); + + err.span_suggestion( + current_qual_sp.to(invalid_qual_sp), + &format!("`{}` must come before `{}`", invalid_qual, current_qual), + format!("{} {}", invalid_qual, current_qual), + Applicability::MachineApplicable, + ).note("keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`"); + } + } + Err(err) + } + } } /// Parses a foreign item (one in an `extern { ... }` block). @@ -1485,7 +1507,7 @@ impl<'a> Parser<'a> { // Make sure an error was emitted (either by recovering an angle bracket, // or by finding an identifier as the next token), since we're // going to continue parsing - assert!(self.sess.span_diagnostic.has_errors()); + assert!(self.sess.span_diagnostic.has_errors().is_some()); } else { return Err(err); } @@ -1948,7 +1970,7 @@ impl<'a> Parser<'a> { // We use an over-approximation here. // `const const`, `fn const` won't parse, but we're not stepping over other syntax either. // `pub` is added in case users got confused with the ordering like `async pub fn`, - // only if it wasn't preceeded by `default` as `default pub` is invalid. + // only if it wasn't preceded by `default` as `default pub` is invalid. let quals: &[Symbol] = if check_pub { &[kw::Pub, kw::Const, kw::Async, kw::Unsafe, kw::Extern] } else { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 5e537d7b95c..17c57867cf9 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -624,9 +624,18 @@ impl<'a> Parser<'a> { GenericArg::Const(self.parse_const_arg()?) } else if self.check_type() { // Parse type argument. + let is_const_fn = self.look_ahead(1, |t| t.kind == token::OpenDelim(token::Paren)); + let mut snapshot = self.create_snapshot_for_diagnostic(); match self.parse_ty() { Ok(ty) => GenericArg::Type(ty), Err(err) => { + if is_const_fn { + if let Ok(expr) = (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None) + { + self.restore_snapshot(snapshot); + return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span))); + } + } // Try to recover from possible `const` arg without braces. return self.recover_const_arg(start, err).map(Some); } @@ -636,7 +645,7 @@ impl<'a> Parser<'a> { } else { // Fall back by trying to parse a const-expr expression. If we successfully do so, // then we should report an error that it needs to be wrapped in braces. - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); match self.parse_expr_res(Restrictions::CONST_EXPR, None) { Ok(expr) => { return Ok(Some(self.dummy_const_arg_needs_braces( @@ -645,7 +654,7 @@ impl<'a> Parser<'a> { ))); } Err(err) => { - *self = snapshot; + self.restore_snapshot(snapshot); err.cancel(); return Ok(None); } |
