Rollup merge of #104362 - WaffleLapkin:span_bug_won't_come_on_time_today, r=Aaron1011

Add `delay_span_bug` to `AttrWrapper::take_for_recovery`

`take_for_recovery` should only be used for recovery (when we should already have an error), so using `delay_span_bug` seems appropriate.

cc `@Aaron1011` (you've added the `FIXME` that this pr fixes)
This commit is contained in:
Manish Goregaokar 2022-11-13 21:49:27 -05:00 committed by GitHub
commit 4fdd944af4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 13 deletions

View File

@ -5,7 +5,8 @@
use rustc_ast::{self as ast}; use rustc_ast::{self as ast};
use rustc_ast::{AttrVec, Attribute, HasAttrs, HasTokens}; use rustc_ast::{AttrVec, Attribute, HasAttrs, HasTokens};
use rustc_errors::PResult; use rustc_errors::PResult;
use rustc_span::{sym, Span}; use rustc_session::parse::ParseSess;
use rustc_span::{sym, Span, DUMMY_SP};
use std::convert::TryInto; use std::convert::TryInto;
use std::ops::Range; use std::ops::Range;
@ -39,8 +40,13 @@ pub(super) fn new(attrs: AttrVec, start_pos: usize) -> AttrWrapper {
pub fn empty() -> AttrWrapper { pub fn empty() -> AttrWrapper {
AttrWrapper { attrs: AttrVec::new(), start_pos: usize::MAX } AttrWrapper { attrs: AttrVec::new(), start_pos: usize::MAX }
} }
// FIXME: Delay span bug here?
pub(crate) fn take_for_recovery(self) -> AttrVec { pub(crate) fn take_for_recovery(self, sess: &ParseSess) -> AttrVec {
sess.span_diagnostic.delay_span_bug(
self.attrs.get(0).map(|attr| attr.span).unwrap_or(DUMMY_SP),
"AttrVec is taken for recovery but no error is produced",
);
self.attrs self.attrs
} }

View File

@ -2272,7 +2272,7 @@ fn parse_if_after_cond(&mut self, lo: Span, mut cond: P<Expr>) -> PResult<'a, P<
self.mk_block_err(cond_span.shrink_to_hi()) self.mk_block_err(cond_span.shrink_to_hi())
} }
} else { } else {
let attrs = self.parse_outer_attributes()?.take_for_recovery(); // For recovery. let attrs = self.parse_outer_attributes()?; // For recovery.
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) { let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
self.parse_block()? self.parse_block()?
} else { } else {
@ -2289,7 +2289,7 @@ fn parse_if_after_cond(&mut self, lo: Span, mut cond: P<Expr>) -> PResult<'a, P<
})? })?
} }
}; };
self.error_on_if_block_attrs(lo, false, block.span, &attrs); self.error_on_if_block_attrs(lo, false, block.span, attrs);
block block
}; };
let els = if self.eat_keyword(kw::Else) { Some(self.parse_else_expr()?) } else { None }; let els = if self.eat_keyword(kw::Else) { Some(self.parse_else_expr()?) } else { None };
@ -2350,7 +2350,7 @@ fn parse_let_expr(&mut self) -> PResult<'a, P<Expr>> {
/// Parses an `else { ... }` expression (`else` token already eaten). /// Parses an `else { ... }` expression (`else` token already eaten).
fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> { fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
let else_span = self.prev_token.span; // `else` let else_span = self.prev_token.span; // `else`
let attrs = self.parse_outer_attributes()?.take_for_recovery(); // For recovery. let attrs = self.parse_outer_attributes()?; // For recovery.
let expr = if self.eat_keyword(kw::If) { let expr = if self.eat_keyword(kw::If) {
self.parse_if_expr()? self.parse_if_expr()?
} else if self.check(&TokenKind::OpenDelim(Delimiter::Brace)) { } else if self.check(&TokenKind::OpenDelim(Delimiter::Brace)) {
@ -2385,7 +2385,7 @@ fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
}, },
} }
}; };
self.error_on_if_block_attrs(else_span, true, expr.span, &attrs); self.error_on_if_block_attrs(else_span, true, expr.span, attrs);
Ok(expr) Ok(expr)
} }
@ -2394,8 +2394,13 @@ fn error_on_if_block_attrs(
ctx_span: Span, ctx_span: Span,
is_ctx_else: bool, is_ctx_else: bool,
branch_span: Span, branch_span: Span,
attrs: &[ast::Attribute], attrs: AttrWrapper,
) { ) {
if attrs.is_empty() {
return;
}
let attrs: &[ast::Attribute] = &attrs.take_for_recovery(self.sess);
let (attributes, last) = match attrs { let (attributes, last) = match attrs {
[] => return, [] => return,
[x0 @ xn] | [x0, .., xn] => (x0.span.to(xn.span), xn.span), [x0 @ xn] | [x0, .., xn] => (x0.span.to(xn.span), xn.span),

View File

@ -19,7 +19,7 @@
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, TokenKind}; use rustc_ast::token::{self, Delimiter, TokenKind};
use rustc_ast::util::classify; use rustc_ast::util::classify;
use rustc_ast::{AttrStyle, AttrVec, Attribute, LocalKind, MacCall, MacCallStmt, MacStmtStyle}; use rustc_ast::{AttrStyle, AttrVec, LocalKind, MacCall, MacCallStmt, MacStmtStyle};
use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, HasAttrs, Local, Stmt}; use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, HasAttrs, Local, Stmt};
use rustc_ast::{StmtKind, DUMMY_NODE_ID}; use rustc_ast::{StmtKind, DUMMY_NODE_ID};
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult}; use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
@ -101,7 +101,7 @@ pub(crate) fn parse_stmt_without_recovery(
self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item))) self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))
} else if self.eat(&token::Semi) { } else if self.eat(&token::Semi) {
// Do not attempt to parse an expression if we're done here. // Do not attempt to parse an expression if we're done here.
self.error_outer_attrs(&attrs.take_for_recovery()); self.error_outer_attrs(attrs);
self.mk_stmt(lo, StmtKind::Empty) self.mk_stmt(lo, StmtKind::Empty)
} else if self.token != token::CloseDelim(Delimiter::Brace) { } else if self.token != token::CloseDelim(Delimiter::Brace) {
// Remainder are line-expr stmts. // Remainder are line-expr stmts.
@ -120,7 +120,7 @@ pub(crate) fn parse_stmt_without_recovery(
} }
self.mk_stmt(lo.to(e.span), StmtKind::Expr(e)) self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))
} else { } else {
self.error_outer_attrs(&attrs.take_for_recovery()); self.error_outer_attrs(attrs);
return Ok(None); return Ok(None);
})) }))
} }
@ -199,8 +199,10 @@ fn parse_stmt_mac(&mut self, lo: Span, attrs: AttrVec, path: ast::Path) -> PResu
/// Error on outer attributes in this context. /// Error on outer attributes in this context.
/// Also error if the previous token was a doc comment. /// Also error if the previous token was a doc comment.
fn error_outer_attrs(&self, attrs: &[Attribute]) { fn error_outer_attrs(&self, attrs: AttrWrapper) {
if let [.., last] = attrs { if !attrs.is_empty()
&& let attrs = attrs.take_for_recovery(self.sess)
&& let attrs @ [.., last] = &*attrs {
if last.is_doc_comment() { if last.is_doc_comment() {
self.sess.emit_err(DocCommentDoesNotDocumentAnything { self.sess.emit_err(DocCommentDoesNotDocumentAnything {
span: last.span, span: last.span,