Skip to content

Commit

Permalink
Rollup merge of #69579 - petrochenkov:noprevspan, r=Centril
Browse files Browse the repository at this point in the history
parser: Remove `Parser::prev_span`

Follow-up to #69384.
r? @Centril
  • Loading branch information
JohnTitor authored Mar 1, 2020
2 parents 48ec252 + 7de9a72 commit 4439bb0
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 214 deletions.
12 changes: 6 additions & 6 deletions src/librustc_builtin_macros/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn parse_inline_asm<'a>(

let constraint = parse_asm_str(&mut p)?;

let span = p.prev_span;
let span = p.prev_token.span;

p.expect(&token::OpenDelim(token::Paren))?;
let expr = p.parse_expr()?;
Expand Down Expand Up @@ -202,15 +202,15 @@ fn parse_inline_asm<'a>(
if constraint.as_str().starts_with('=') {
struct_span_err!(
cx.parse_sess.span_diagnostic,
p.prev_span,
p.prev_token.span,
E0662,
"input operand constraint contains '='"
)
.emit();
} else if constraint.as_str().starts_with('+') {
struct_span_err!(
cx.parse_sess.span_diagnostic,
p.prev_span,
p.prev_token.span,
E0663,
"input operand constraint contains '+'"
)
Expand All @@ -233,11 +233,11 @@ fn parse_inline_asm<'a>(
let s = parse_asm_str(&mut p)?;

if OPTIONS.iter().any(|&opt| s == opt) {
cx.span_warn(p.prev_span, "expected a clobber, found an option");
cx.span_warn(p.prev_token.span, "expected a clobber, found an option");
} else if s.as_str().starts_with('{') || s.as_str().ends_with('}') {
struct_span_err!(
cx.parse_sess.span_diagnostic,
p.prev_span,
p.prev_token.span,
E0664,
"clobber should not be surrounded by braces"
)
Expand All @@ -259,7 +259,7 @@ fn parse_inline_asm<'a>(
} else if option == sym::intel {
dialect = AsmDialect::Intel;
} else {
cx.span_warn(p.prev_span, "unrecognized option");
cx.span_warn(p.prev_token.span, "unrecognized option");
}

if p.token == token::Comma {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn parse_assert<'a>(
let custom_message =
if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind {
let mut err = cx.struct_span_warn(parser.token.span, "unexpected string literal");
let comma_span = parser.prev_span.shrink_to_hi();
let comma_span = parser.prev_token.span.shrink_to_hi();
err.span_suggestion_short(
comma_span,
"try adding a comma",
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_parse/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<'a> Parser<'a> {
self.expect(&token::OpenDelim(token::Bracket))?;
let item = self.parse_attr_item()?;
self.expect(&token::CloseDelim(token::Bracket))?;
let hi = self.prev_span;
let hi = self.prev_token.span;

let attr_sp = lo.to(hi);

Expand Down Expand Up @@ -255,7 +255,7 @@ impl<'a> Parser<'a> {
while self.token.kind != token::Eof {
let lo = self.token.span;
let item = self.parse_attr_item()?;
expanded_attrs.push((item, lo.to(self.prev_span)));
expanded_attrs.push((item, lo.to(self.prev_token.span)));
if !self.eat(&token::Comma) {
break;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ impl<'a> Parser<'a> {
let lo = self.token.span;
let path = self.parse_path(PathStyle::Mod)?;
let kind = self.parse_meta_item_kind()?;
let span = lo.to(self.prev_span);
let span = lo.to(self.prev_token.span);
Ok(ast::MetaItem { path, kind, span })
}

Expand Down
52 changes: 26 additions & 26 deletions src/librustc_parse/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,24 @@ impl<'a> Parser<'a> {
};
(
format!("expected one of {}, found {}", expect, actual),
(self.prev_span.shrink_to_hi(), format!("expected one of {}", short_expect)),
(self.prev_token.span.shrink_to_hi(), format!("expected one of {}", short_expect)),
)
} else if expected.is_empty() {
(
format!("unexpected token: {}", actual),
(self.prev_span, "unexpected token after this".to_string()),
(self.prev_token.span, "unexpected token after this".to_string()),
)
} else {
(
format!("expected {}, found {}", expect, actual),
(self.prev_span.shrink_to_hi(), format!("expected {}", expect)),
(self.prev_token.span.shrink_to_hi(), format!("expected {}", expect)),
)
};
self.last_unexpected_token_span = Some(self.token.span);
let mut err = self.struct_span_err(self.token.span, &msg_exp);
let sp = if self.token == token::Eof {
// This is EOF; don't want to point at the following char, but rather the last token.
self.prev_span
self.prev_token.span
} else {
label_sp
};
Expand All @@ -298,7 +298,7 @@ impl<'a> Parser<'a> {
}

let sm = self.sess.source_map();
if self.prev_span == DUMMY_SP {
if self.prev_token.span == DUMMY_SP {
// Account for macro context where the previous span might not be
// available to avoid incorrect output (#54841).
err.span_label(self.token.span, label_exp);
Expand Down Expand Up @@ -577,7 +577,7 @@ impl<'a> Parser<'a> {
match inner_op.kind {
ExprKind::Binary(op, _, _) if op.node.is_comparison() => {
// Respan to include both operators.
let op_span = op.span.to(self.prev_span);
let op_span = op.span.to(self.prev_token.span);
let mut err =
self.struct_span_err(op_span, "comparison operators cannot be chained");

Expand Down Expand Up @@ -632,7 +632,7 @@ impl<'a> Parser<'a> {
// FIXME: actually check that the two expressions in the binop are
// paths and resynthesize new fn call expression instead of using
// `ExprKind::Err` placeholder.
mk_err_expr(self, inner_op.span.to(self.prev_span))
mk_err_expr(self, inner_op.span.to(self.prev_token.span))
}
Err(mut expr_err) => {
expr_err.cancel();
Expand All @@ -654,7 +654,7 @@ impl<'a> Parser<'a> {
// FIXME: actually check that the two expressions in the binop are
// paths and resynthesize new fn call expression instead of using
// `ExprKind::Err` placeholder.
mk_err_expr(self, inner_op.span.to(self.prev_span))
mk_err_expr(self, inner_op.span.to(self.prev_token.span))
}
}
} else {
Expand Down Expand Up @@ -723,7 +723,7 @@ impl<'a> Parser<'a> {

self.bump(); // `+`
let bounds = self.parse_generic_bounds(None)?;
let sum_span = ty.span.to(self.prev_span);
let sum_span = ty.span.to(self.prev_token.span);

let mut err = struct_span_err!(
self.sess.span_diagnostic,
Expand Down Expand Up @@ -790,7 +790,7 @@ impl<'a> Parser<'a> {

let mut path = ast::Path { segments: Vec::new(), span: DUMMY_SP };
self.parse_path_segments(&mut path.segments, T::PATH_STYLE)?;
path.span = ty_span.to(self.prev_span);
path.span = ty_span.to(self.prev_token.span);

let ty_str = self.span_to_snippet(ty_span).unwrap_or_else(|_| pprust::ty_to_string(&ty));
self.struct_span_err(path.span, "missing angle brackets in associated item path")
Expand All @@ -809,9 +809,9 @@ impl<'a> Parser<'a> {

pub(super) fn maybe_consume_incorrect_semicolon(&mut self, items: &[P<Item>]) -> bool {
if self.eat(&token::Semi) {
let mut err = self.struct_span_err(self.prev_span, "expected item, found `;`");
let mut err = self.struct_span_err(self.prev_token.span, "expected item, found `;`");
err.span_suggestion_short(
self.prev_span,
self.prev_token.span,
"remove this semicolon",
String::new(),
Applicability::MachineApplicable,
Expand Down Expand Up @@ -854,10 +854,10 @@ impl<'a> Parser<'a> {
}
// We don't want to point at the following span after DUMMY_SP.
// This happens when the parser finds an empty TokenStream.
_ if self.prev_span == DUMMY_SP => (self.token.span, self.token.span),
_ if self.prev_token.span == DUMMY_SP => (self.token.span, self.token.span),
// EOF, don't want to point at the following char, but rather the last token.
(token::Eof, None) => (self.prev_span, self.token.span),
_ => (self.prev_span.shrink_to_hi(), self.token.span),
(token::Eof, None) => (self.prev_token.span, self.token.span),
_ => (self.prev_token.span.shrink_to_hi(), self.token.span),
};
let msg = format!(
"expected `{}`, found {}",
Expand Down Expand Up @@ -894,10 +894,10 @@ impl<'a> Parser<'a> {
let sm = self.sess.source_map();
let msg = format!("expected `;`, found `{}`", super::token_descr(&self.token));
let appl = Applicability::MachineApplicable;
if self.token.span == DUMMY_SP || self.prev_span == DUMMY_SP {
if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
// Likely inside a macro, can't provide meaninful suggestions.
return self.expect(&token::Semi).map(drop);
} else if !sm.is_multiline(self.prev_span.until(self.token.span)) {
} else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
// The current token is in the same line as the prior token, not recoverable.
} else if self.look_ahead(1, |t| {
t == &token::CloseDelim(token::Brace) || t.can_begin_expr() && t.kind != token::Colon
Expand All @@ -910,7 +910,7 @@ impl<'a> Parser<'a> {
// let x = 32:
// let y = 42;
self.bump();
let sp = self.prev_span;
let sp = self.prev_token.span;
self.struct_span_err(sp, &msg)
.span_suggestion(sp, "change this to `;`", ";".to_string(), appl)
.emit();
Expand All @@ -927,7 +927,7 @@ impl<'a> Parser<'a> {
//
// let x = 32
// let y = 42;
let sp = self.prev_span.shrink_to_hi();
let sp = self.prev_token.span.shrink_to_hi();
self.struct_span_err(sp, &msg)
.span_label(self.token.span, "unexpected token")
.span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
Expand Down Expand Up @@ -961,7 +961,7 @@ impl<'a> Parser<'a> {
self.expect(&token::OpenDelim(token::Paren))?;
let expr = self.parse_expr()?;
self.expect(&token::CloseDelim(token::Paren))?;
Ok((self.prev_span, expr, false))
Ok((self.prev_token.span, expr, false))
}

fn recover_await_prefix(&mut self, await_sp: Span) -> PResult<'a, (Span, P<Expr>, bool)> {
Expand Down Expand Up @@ -1036,10 +1036,10 @@ impl<'a> Parser<'a> {
.span_to_snippet(pat.span.trim_start(begin_par_sp).unwrap())
.unwrap_or_else(|_| pprust::pat_to_string(&pat));

self.struct_span_err(self.prev_span, "unexpected closing `)`")
self.struct_span_err(self.prev_token.span, "unexpected closing `)`")
.span_label(begin_par_sp, "opening `(`")
.span_suggestion(
begin_par_sp.to(self.prev_span),
begin_par_sp.to(self.prev_token.span),
"remove parenthesis in `for` loop",
format!("{} in {}", pat_str, pprust::expr_to_string(&expr)),
// With e.g. `for (x) in y)` this would replace `(x) in y)`
Expand Down Expand Up @@ -1091,7 +1091,7 @@ impl<'a> Parser<'a> {
err.emit();
// Recover from parse error, callers expect the closing delim to be consumed.
self.consume_block(delim, ConsumeClosingDelim::Yes);
self.mk_expr(lo.to(self.prev_span), ExprKind::Err, AttrVec::new())
self.mk_expr(lo.to(self.prev_token.span), ExprKind::Err, AttrVec::new())
}
}
}
Expand Down Expand Up @@ -1138,7 +1138,7 @@ impl<'a> Parser<'a> {
err.span_label(sp, "unclosed delimiter");
}
err.span_suggestion_short(
self.prev_span.shrink_to_hi(),
self.prev_token.span.shrink_to_hi(),
&format!("{} may belong here", delim.to_string()),
delim.to_string(),
Applicability::MaybeIncorrect,
Expand Down Expand Up @@ -1246,9 +1246,9 @@ impl<'a> Parser<'a> {
pub(super) fn check_for_for_in_in_typo(&mut self, in_span: Span) {
if self.eat_keyword(kw::In) {
// a common typo: `for _ in in bar {}`
self.struct_span_err(self.prev_span, "expected iterable, found keyword `in`")
self.struct_span_err(self.prev_token.span, "expected iterable, found keyword `in`")
.span_suggestion_short(
in_span.until(self.prev_span),
in_span.until(self.prev_token.span),
"remove the duplicated `in`",
String::new(),
Applicability::MachineApplicable,
Expand Down
Loading

0 comments on commit 4439bb0

Please sign in to comment.