Skip to content

Commit 5c5fa77

Browse files
committed
review comments
1 parent 2416017 commit 5c5fa77

File tree

11 files changed

+73
-66
lines changed

11 files changed

+73
-66
lines changed

src/libfmt_macros/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ mod tests {
627627
use super::*;
628628

629629
fn same(fmt: &'static str, p: &[Piece<'static>]) {
630-
let parser = Parser::new(fmt, None, vec![], false, None);
630+
let parser = Parser::new(fmt, None, vec![], false);
631631
assert!(parser.collect::<Vec<Piece<'static>>>() == p);
632632
}
633633

@@ -643,7 +643,7 @@ mod tests {
643643
}
644644

645645
fn musterr(s: &str) {
646-
let mut p = Parser::new(s, None, vec![], false, None);
646+
let mut p = Parser::new(s, None, vec![], false);
647647
p.next();
648648
assert!(!p.errors.is_empty());
649649
}

src/libsyntax/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'a> StripUnconfigured<'a> {
9898
self.sess.span_diagnostic.struct_span_err(attr.span, "bad `cfg_attr` attribute")
9999
.span_label(attr.span, "missing condition and attribute")
100100
.note("`cfg_attr` must be of the form: \
101-
`#[cfg_attr(condition, attribute)]`")
101+
`#[cfg_attr(condition, attribute, other_attribute, ...)]`")
102102
.note("for more information, visit \
103103
<https://doc.rust-lang.org/reference/conditional-compilation.html\
104104
#the-cfg_attr-attribute>")

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::parse::{self, parser, DirectoryOwnership};
1111
use crate::parse::token;
1212
use crate::ptr::P;
1313
use crate::symbol::{kw, sym, Ident, Symbol};
14-
use crate::ThinVec;
14+
use crate::{ThinVec, MACRO_ARGUMENTS};
1515
use crate::tokenstream::{self, TokenStream};
1616

1717
use errors::{DiagnosticBuilder, DiagnosticId};
@@ -850,11 +850,7 @@ impl<'a> ExtCtxt<'a> {
850850
}
851851

852852
pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree]) -> parser::Parser<'a> {
853-
parse::stream_to_parser(
854-
self.parse_sess,
855-
tts.iter().cloned().collect(),
856-
Some("macro arguments"),
857-
)
853+
parse::stream_to_parser(self.parse_sess, tts.iter().cloned().collect(), MACRO_ARGUMENTS)
858854
}
859855
pub fn source_map(&self) -> &'a SourceMap { self.parse_sess.source_map() }
860856
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ pub fn parse(
664664
directory,
665665
recurse_into_modules,
666666
true,
667-
Some("macro arguments"),
667+
crate::MACRO_ARGUMENTS,
668668
);
669669

670670
// A queue of possible matcher positions. We initialize it with the matcher position in which

src/libsyntax/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub use rustc_data_structures::thin_vec::ThinVec;
3131
use ast::AttrId;
3232
use syntax_pos::edition::Edition;
3333

34+
const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
35+
3436
// A variant of 'try!' that panics on an Err. This is used as a crutch on the
3537
// way towards a non-panic!-prone parser. It should be used for fatal parsing
3638
// errors; eventually we plan to convert all code using panictry to just use

src/libsyntax/parse/diagnostics.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::symbol::kw;
1313
use crate::ThinVec;
1414
use errors::{Applicability, DiagnosticBuilder};
1515
use log::debug;
16-
use syntax_pos::Span;
16+
use syntax_pos::{Span, DUMMY_SP};
1717

1818
pub trait RecoverQPath: Sized + 'static {
1919
const PATH_STYLE: PathStyle = PathStyle::Expr;
@@ -201,7 +201,7 @@ impl<'a> Parser<'a> {
201201

202202
let mut path = ast::Path {
203203
segments: Vec::new(),
204-
span: syntax_pos::DUMMY_SP,
204+
span: DUMMY_SP,
205205
};
206206
self.parse_path_segments(&mut path.segments, T::PATH_STYLE)?;
207207
path.span = ty_span.to(self.prev_span);
@@ -267,6 +267,58 @@ impl<'a> Parser<'a> {
267267
}
268268
}
269269

270+
/// Create a `DiagnosticBuilder` for an unexpected token `t` and try to recover if it is a
271+
/// closing delimiter.
272+
pub fn unexpected_try_recover(
273+
&mut self,
274+
t: &token::Token,
275+
) -> PResult<'a, bool /* recovered */> {
276+
let token_str = pprust::token_to_string(t);
277+
let this_token_str = self.this_token_descr();
278+
let (prev_sp, sp) = match (&self.token, self.subparser_name) {
279+
// Point at the end of the macro call when reaching end of macro arguments.
280+
(token::Token::Eof, Some(_)) => {
281+
let sp = self.sess.source_map().next_point(self.span);
282+
(sp, sp)
283+
}
284+
// We don't want to point at the following span after DUMMY_SP.
285+
// This happens when the parser finds an empty TokenStream.
286+
_ if self.prev_span == DUMMY_SP => (self.span, self.span),
287+
// EOF, don't want to point at the following char, but rather the last token.
288+
(token::Token::Eof, None) => (self.prev_span, self.span),
289+
_ => (self.sess.source_map().next_point(self.prev_span), self.span),
290+
};
291+
let msg = format!(
292+
"expected `{}`, found {}",
293+
token_str,
294+
match (&self.token, self.subparser_name) {
295+
(token::Token::Eof, Some(origin)) => format!("end of {}", origin),
296+
_ => this_token_str,
297+
},
298+
);
299+
let mut err = self.struct_span_err(sp, &msg);
300+
let label_exp = format!("expected `{}`", token_str);
301+
match self.recover_closing_delimiter(&[t.clone()], err) {
302+
Err(e) => err = e,
303+
Ok(recovered) => {
304+
return Ok(recovered);
305+
}
306+
}
307+
let cm = self.sess.source_map();
308+
match (cm.lookup_line(prev_sp.lo()), cm.lookup_line(sp.lo())) {
309+
(Ok(ref a), Ok(ref b)) if a.line == b.line => {
310+
// When the spans are in the same line, it means that the only content
311+
// between them is whitespace, point only at the found token.
312+
err.span_label(sp, label_exp);
313+
}
314+
_ => {
315+
err.span_label(prev_sp, label_exp);
316+
err.span_label(sp, "unexpected token");
317+
}
318+
}
319+
Err(err)
320+
}
321+
270322
/// Consume alternative await syntaxes like `await <expr>`, `await? <expr>`, `await(<expr>)`
271323
/// and `await { <expr> }`.
272324
crate fn parse_incorrect_await_syntax(

src/libsyntax/parse/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn maybe_source_file_to_parser(
248248
// must preserve old name for now, because quote! from the *existing*
249249
// compiler expands into it
250250
pub fn new_parser_from_tts(sess: &ParseSess, tts: Vec<TokenTree>) -> Parser<'_> {
251-
stream_to_parser(sess, tts.into_iter().collect(), Some("macro arguments"))
251+
stream_to_parser(sess, tts.into_iter().collect(), crate::MACRO_ARGUMENTS)
252252
}
253253

254254

@@ -331,9 +331,9 @@ pub fn maybe_file_to_stream(
331331
pub fn stream_to_parser<'a>(
332332
sess: &'a ParseSess,
333333
stream: TokenStream,
334-
is_subparser: Option<&'static str>,
334+
subparser_name: Option<&'static str>,
335335
) -> Parser<'a> {
336-
Parser::new(sess, stream, None, true, false, is_subparser)
336+
Parser::new(sess, stream, None, true, false, subparser_name)
337337
}
338338

339339
/// Given stream, the `ParseSess` and the base directory, produces a parser.

src/libsyntax/parse/parser.rs

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ pub struct Parser<'a> {
233233
/// error.
234234
crate unclosed_delims: Vec<UnmatchedBrace>,
235235
last_unexpected_token_span: Option<Span>,
236-
/// If `true`, this `Parser` is not parsing Rust code but rather a macro call.
237-
is_subparser: Option<&'static str>,
236+
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
237+
crate subparser_name: Option<&'static str>,
238238
}
239239

240240
impl<'a> Drop for Parser<'a> {
@@ -541,7 +541,7 @@ impl<'a> Parser<'a> {
541541
directory: Option<Directory<'a>>,
542542
recurse_into_file_modules: bool,
543543
desugar_doc_comments: bool,
544-
is_subparser: Option<&'static str>,
544+
subparser_name: Option<&'static str>,
545545
) -> Self {
546546
let mut parser = Parser {
547547
sess,
@@ -572,7 +572,7 @@ impl<'a> Parser<'a> {
572572
max_angle_bracket_count: 0,
573573
unclosed_delims: Vec::new(),
574574
last_unexpected_token_span: None,
575-
is_subparser,
575+
subparser_name,
576576
};
577577

578578
let tok = parser.next_tok();
@@ -636,56 +636,13 @@ impl<'a> Parser<'a> {
636636
}
637637

638638
/// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
639-
pub fn expect(&mut self, t: &token::Token) -> PResult<'a, bool /* recovered */> {
639+
pub fn expect(&mut self, t: &token::Token) -> PResult<'a, bool /* recovered */> {
640640
if self.expected_tokens.is_empty() {
641641
if self.token == *t {
642642
self.bump();
643643
Ok(false)
644644
} else {
645-
let token_str = pprust::token_to_string(t);
646-
let this_token_str = self.this_token_descr();
647-
let (prev_sp, sp) = match (&self.token, self.is_subparser) {
648-
// Point at the end of the macro call when reaching end of macro arguments.
649-
(token::Token::Eof, Some(_)) => {
650-
let sp = self.sess.source_map().next_point(self.span);
651-
(sp, sp)
652-
}
653-
// We don't want to point at the following span after DUMMY_SP.
654-
// This happens when the parser finds an empty TokenStream.
655-
_ if self.prev_span == DUMMY_SP => (self.span, self.span),
656-
// EOF, don't want to point at the following char, but rather the last token.
657-
(token::Token::Eof, None) => (self.prev_span, self.span),
658-
_ => (self.sess.source_map().next_point(self.prev_span), self.span),
659-
};
660-
let msg = format!(
661-
"expected `{}`, found {}",
662-
token_str,
663-
match (&self.token, self.is_subparser) {
664-
(token::Token::Eof, Some(origin)) => format!("end of {}", origin),
665-
_ => this_token_str,
666-
},
667-
);
668-
let mut err = self.struct_span_err(sp, &msg);
669-
let label_exp = format!("expected `{}`", token_str);
670-
match self.recover_closing_delimiter(&[t.clone()], err) {
671-
Err(e) => err = e,
672-
Ok(recovered) => {
673-
return Ok(recovered);
674-
}
675-
}
676-
let cm = self.sess.source_map();
677-
match (cm.lookup_line(prev_sp.lo()), cm.lookup_line(sp.lo())) {
678-
(Ok(ref a), Ok(ref b)) if a.line == b.line => {
679-
// When the spans are in the same line, it means that the only content
680-
// between them is whitespace, point only at the found token.
681-
err.span_label(sp, label_exp);
682-
}
683-
_ => {
684-
err.span_label(prev_sp, label_exp);
685-
err.span_label(sp, "unexpected token");
686-
}
687-
}
688-
Err(err)
645+
self.unexpected_try_recover(t)
689646
}
690647
} else {
691648
self.expect_one_of(slice::from_ref(t), &[])
@@ -2644,7 +2601,7 @@ impl<'a> Parser<'a> {
26442601
}
26452602
Err(mut err) => {
26462603
self.cancel(&mut err);
2647-
let (span, msg) = match (&self.token, self.is_subparser) {
2604+
let (span, msg) = match (&self.token, self.subparser_name) {
26482605
(&token::Token::Eof, Some(origin)) => {
26492606
let sp = self.sess.source_map().next_point(self.span);
26502607
(sp, format!( "expected expression, found end of {}", origin))

src/test/ui/malformed/malformed-special-attrs.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: bad `cfg_attr` attribute
44
LL | #[cfg_attr]
55
| ^^^^^^^^^^^ missing condition and attribute
66
|
7-
= note: `cfg_attr` must be of the form: `#[cfg_attr(condition, attribute)]`
7+
= note: `cfg_attr` must be of the form: `#[cfg_attr(condition, attribute, other_attribute, ...)]`
88
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
99

1010
error: expected `(`, found `=`

0 commit comments

Comments
 (0)