Skip to content
/ rust Public
forked from rust-lang/rust

Commit a926c73

Browse files
authored
Rollup merge of rust-lang#114081 - nnethercote:desugar_doc_comments-cleanups, r=petrochenkov
`desugar_doc_comments` cleanups r? `@petrochenkov`
2 parents fa21a8c + 34b218e commit a926c73

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

compiler/rustc_parse/src/parser/attr_wrapper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
107107
let tokens =
108108
std::iter::once((FlatToken::Token(self.start_token.0.clone()), self.start_token.1))
109109
.chain((0..self.num_calls).map(|_| {
110-
let token = cursor_snapshot.next(cursor_snapshot.desugar_doc_comments);
110+
let token = cursor_snapshot.next();
111111
(FlatToken::Token(token.0), token.1)
112112
}))
113113
.take(self.num_calls);

compiler/rustc_parse/src/parser/mod.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ pub struct Parser<'a> {
138138
// Important: This must only be advanced from `bump` to ensure that
139139
// `token_cursor.num_next_calls` is updated properly.
140140
token_cursor: TokenCursor,
141-
desugar_doc_comments: bool,
142141
/// This field is used to keep track of how many left angle brackets we have seen. This is
143142
/// required in order to detect extra leading left angle brackets (`<` characters) and error
144143
/// appropriately.
@@ -225,6 +224,9 @@ struct TokenCursor {
225224
// because it's the outermost token stream which never has delimiters.
226225
stack: Vec<(TokenTreeCursor, Delimiter, DelimSpan)>,
227226

227+
// We need to desugar doc comments from `/// foo` form into `#[doc =
228+
// r"foo"]` form when parsing declarative macro inputs in `parse_tt`,
229+
// because some declarative macros look for `doc` attributes.
228230
desugar_doc_comments: bool,
229231

230232
// Counts the number of calls to `{,inlined_}next`.
@@ -255,33 +257,38 @@ struct TokenCursor {
255257
}
256258

257259
impl TokenCursor {
258-
fn next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) {
259-
self.inlined_next(desugar_doc_comments)
260+
fn next(&mut self) -> (Token, Spacing) {
261+
self.inlined_next()
260262
}
261263

262264
/// This always-inlined version should only be used on hot code paths.
263265
#[inline(always)]
264-
fn inlined_next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) {
266+
fn inlined_next(&mut self) -> (Token, Spacing) {
265267
loop {
266268
// FIXME: we currently don't return `Delimiter` open/close delims. To fix #67062 we will
267269
// need to, whereupon the `delim != Delimiter::Invisible` conditions below can be
268270
// removed.
269271
if let Some(tree) = self.tree_cursor.next_ref() {
270272
match tree {
271-
&TokenTree::Token(ref token, spacing) => match (desugar_doc_comments, token) {
272-
(true, &Token { kind: token::DocComment(_, attr_style, data), span }) => {
273-
let desugared = self.desugar(attr_style, data, span);
274-
self.tree_cursor.replace_prev_and_rewind(desugared);
275-
// Continue to get the first token of the desugared doc comment.
276-
}
277-
_ => {
278-
debug_assert!(!matches!(
279-
token.kind,
280-
token::OpenDelim(_) | token::CloseDelim(_)
281-
));
282-
return (token.clone(), spacing);
273+
&TokenTree::Token(ref token, spacing) => {
274+
match (self.desugar_doc_comments, token) {
275+
(
276+
true,
277+
&Token { kind: token::DocComment(_, attr_style, data), span },
278+
) => {
279+
let desugared = self.desugar(attr_style, data, span);
280+
self.tree_cursor.replace_prev_and_rewind(desugared);
281+
// Continue to get the first token of the desugared doc comment.
282+
}
283+
_ => {
284+
debug_assert!(!matches!(
285+
token.kind,
286+
token::OpenDelim(_) | token::CloseDelim(_)
287+
));
288+
return (token.clone(), spacing);
289+
}
283290
}
284-
},
291+
}
285292
&TokenTree::Delimited(sp, delim, ref tts) => {
286293
let trees = tts.clone().into_trees();
287294
self.stack.push((mem::replace(&mut self.tree_cursor, trees), delim, sp));
@@ -463,7 +470,6 @@ impl<'a> Parser<'a> {
463470
desugar_doc_comments,
464471
break_last_token: false,
465472
},
466-
desugar_doc_comments,
467473
unmatched_angle_bracket_count: 0,
468474
max_angle_bracket_count: 0,
469475
last_unexpected_token_span: None,
@@ -1107,7 +1113,7 @@ impl<'a> Parser<'a> {
11071113
pub fn bump(&mut self) {
11081114
// Note: destructuring here would give nicer code, but it was found in #96210 to be slower
11091115
// than `.0`/`.1` access.
1110-
let mut next = self.token_cursor.inlined_next(self.desugar_doc_comments);
1116+
let mut next = self.token_cursor.inlined_next();
11111117
self.token_cursor.num_next_calls += 1;
11121118
// We've retrieved an token from the underlying
11131119
// cursor, so we no longer need to worry about
@@ -1157,7 +1163,7 @@ impl<'a> Parser<'a> {
11571163
let mut i = 0;
11581164
let mut token = Token::dummy();
11591165
while i < dist {
1160-
token = cursor.next(/* desugar_doc_comments */ false).0;
1166+
token = cursor.next().0;
11611167
if matches!(
11621168
token.kind,
11631169
token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible)

0 commit comments

Comments
 (0)