Skip to content

Commit da3629b

Browse files
committed
Auto merge of #67112 - Centril:expr-polish, r=estebank
Refactor expression parsing thoroughly Based on #66994 together with which this has refactored basically the entirety of `expr.rs`. r? @estebank
2 parents 25434f8 + 7a246ac commit da3629b

31 files changed

+585
-537
lines changed

src/librustc_parse/parser/attr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{Parser, PathStyle, TokenType};
22
use rustc_errors::PResult;
33
use syntax::ast;
44
use syntax::attr;
5+
use syntax::print::pprust;
56
use syntax::token::{self, Nonterminal};
67
use syntax::util::comments;
78
use syntax_pos::{Span, Symbol};
@@ -154,7 +155,7 @@ impl<'a> Parser<'a> {
154155
(attr_sp, item, style)
155156
}
156157
_ => {
157-
let token_str = self.this_token_to_string();
158+
let token_str = pprust::token_to_string(&self.token);
158159
return Err(self.fatal(&format!("expected `#`, found `{}`", token_str)));
159160
}
160161
};
@@ -329,7 +330,7 @@ impl<'a> Parser<'a> {
329330
Err(ref mut err) => err.cancel(),
330331
}
331332

332-
let found = self.this_token_to_string();
333+
let found = pprust::token_to_string(&self.token);
333334
let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
334335
Err(self.diagnostic().struct_span_err(self.token.span, &msg))
335336
}

src/librustc_parse/parser/diagnostics.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<'a> Parser<'a> {
200200
pub(super) fn expected_ident_found(&self) -> DiagnosticBuilder<'a> {
201201
let mut err = self.struct_span_err(
202202
self.token.span,
203-
&format!("expected identifier, found {}", self.this_token_descr()),
203+
&format!("expected identifier, found {}", super::token_descr(&self.token)),
204204
);
205205
let valid_follow = &[
206206
TokenKind::Eq,
@@ -225,7 +225,7 @@ impl<'a> Parser<'a> {
225225
);
226226
}
227227
}
228-
if let Some(token_descr) = self.token_descr() {
228+
if let Some(token_descr) = super::token_descr_opt(&self.token) {
229229
err.span_label(self.token.span, format!("expected identifier, found {}", token_descr));
230230
} else {
231231
err.span_label(self.token.span, "expected identifier");
@@ -272,7 +272,7 @@ impl<'a> Parser<'a> {
272272
expected.sort_by_cached_key(|x| x.to_string());
273273
expected.dedup();
274274
let expect = tokens_to_string(&expected[..]);
275-
let actual = self.this_token_descr();
275+
let actual = super::token_descr(&self.token);
276276
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
277277
let short_expect = if expected.len() > 6 {
278278
format!("{} possible tokens", expected.len())
@@ -815,7 +815,7 @@ impl<'a> Parser<'a> {
815815
t: &TokenKind,
816816
) -> PResult<'a, bool /* recovered */> {
817817
let token_str = pprust::token_kind_to_string(t);
818-
let this_token_str = self.this_token_descr();
818+
let this_token_str = super::token_descr(&self.token);
819819
let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
820820
// Point at the end of the macro call when reaching end of macro arguments.
821821
(token::Eof, Some(_)) => {
@@ -862,7 +862,7 @@ impl<'a> Parser<'a> {
862862
return Ok(());
863863
}
864864
let sm = self.sess.source_map();
865-
let msg = format!("expected `;`, found `{}`", self.this_token_descr());
865+
let msg = format!("expected `;`, found `{}`", super::token_descr(&self.token));
866866
let appl = Applicability::MachineApplicable;
867867
if self.token.span == DUMMY_SP || self.prev_span == DUMMY_SP {
868868
// Likely inside a macro, can't provide meaninful suggestions.
@@ -1270,7 +1270,7 @@ impl<'a> Parser<'a> {
12701270
}
12711271

12721272
pub(super) fn expected_semi_or_open_brace<T>(&mut self) -> PResult<'a, T> {
1273-
let token_str = self.this_token_descr();
1273+
let token_str = super::token_descr(&self.token);
12741274
let mut err = self.fatal(&format!("expected `;` or `{{`, found {}", token_str));
12751275
err.span_label(self.token.span, "expected `;` or `{`");
12761276
Err(err)
@@ -1447,7 +1447,7 @@ impl<'a> Parser<'a> {
14471447
}
14481448
_ => (
14491449
self.token.span,
1450-
format!("expected expression, found {}", self.this_token_descr(),),
1450+
format!("expected expression, found {}", super::token_descr(&self.token),),
14511451
),
14521452
};
14531453
let mut err = self.struct_span_err(span, &msg);

0 commit comments

Comments
 (0)