Skip to content

Commit 920bed1

Browse files
committed
Don't create an empty LazyTokenStream
1 parent 37b25e8 commit 920bed1

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

compiler/rustc_parse/src/parser/attr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a> Parser<'a> {
7272
}
7373
})?;
7474
if let Some(mut attr) = attr {
75-
attr.tokens = Some(tokens);
75+
attr.tokens = tokens;
7676
attrs.push(attr);
7777
} else {
7878
break;
@@ -176,7 +176,7 @@ impl<'a> Parser<'a> {
176176
};
177177
if capture_tokens {
178178
let (mut item, tokens) = self.collect_tokens(do_parse)?;
179-
item.tokens = Some(tokens);
179+
item.tokens = tokens;
180180
item
181181
} else {
182182
do_parse(self)?
@@ -213,7 +213,7 @@ impl<'a> Parser<'a> {
213213
}
214214
})?;
215215
if let Some(mut attr) = attr {
216-
attr.tokens = Some(tokens);
216+
attr.tokens = tokens;
217217
attrs.push(attr);
218218
} else {
219219
break;

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ impl<'a> Parser<'a> {
11161116
) -> PResult<'a, P<Expr>> {
11171117
if needs_tokens {
11181118
let (mut expr, tokens) = self.collect_tokens(f)?;
1119-
expr.tokens = Some(tokens);
1119+
expr.tokens = tokens;
11201120
Ok(expr)
11211121
} else {
11221122
f(self)

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a> Parser<'a> {
151151
if let Some(tokens) = tokens {
152152
if let Some(item) = &mut item {
153153
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
154-
item.tokens = Some(tokens);
154+
item.tokens = tokens;
155155
}
156156
}
157157
}

compiler/rustc_parse/src/parser/mod.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1178,8 +1178,9 @@ impl<'a> Parser<'a> {
11781178

11791179
/// Records all tokens consumed by the provided callback,
11801180
/// including the current token. These tokens are collected
1181-
/// into a `TokenStream`, and returned along with the result
1182-
/// of the callback.
1181+
/// into a `LazyTokenStream`, and returned along with the result
1182+
/// of the callback. The returned `LazyTokenStream` will be `None`
1183+
/// if not tokens were captured.
11831184
///
11841185
/// Note: If your callback consumes an opening delimiter
11851186
/// (including the case where you call `collect_tokens`
@@ -1195,7 +1196,7 @@ impl<'a> Parser<'a> {
11951196
pub fn collect_tokens<R>(
11961197
&mut self,
11971198
f: impl FnOnce(&mut Self) -> PResult<'a, R>,
1198-
) -> PResult<'a, (R, LazyTokenStream)> {
1199+
) -> PResult<'a, (R, Option<LazyTokenStream>)> {
11991200
let start_token = (self.token.clone(), self.token_spacing);
12001201
let mut cursor_snapshot = self.token_cursor.clone();
12011202

@@ -1205,6 +1206,11 @@ impl<'a> Parser<'a> {
12051206
let num_calls = new_calls - cursor_snapshot.num_next_calls;
12061207
let desugar_doc_comments = self.desugar_doc_comments;
12071208

1209+
// We didn't capture any tokens
1210+
if num_calls == 0 {
1211+
return Ok((ret, None));
1212+
}
1213+
12081214
// Produces a `TokenStream` on-demand. Using `cursor_snapshot`
12091215
// and `num_calls`, we can reconstruct the `TokenStream` seen
12101216
// by the callback. This allows us to avoid producing a `TokenStream`
@@ -1233,7 +1239,7 @@ impl<'a> Parser<'a> {
12331239
};
12341240
let stream = LazyTokenStream::new(LazyTokenStreamInner::Lazy(Box::new(lazy_cb)));
12351241

1236-
Ok((ret, stream))
1242+
Ok((ret, Some(stream)))
12371243
}
12381244

12391245
/// `::{` or `::*`

compiler/rustc_parse/src/parser/nonterminal.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a> Parser<'a> {
103103
// If we captured tokens during parsing (due to outer attributes),
104104
// use those.
105105
if item.tokens.is_none() {
106-
item.tokens = Some(tokens);
106+
item.tokens = tokens;
107107
}
108108
token::NtItem(item)
109109
}
@@ -115,7 +115,7 @@ impl<'a> Parser<'a> {
115115
let (mut block, tokens) = self.collect_tokens(|this| this.parse_block())?;
116116
// We have have eaten an NtBlock, which could already have tokens
117117
if block.tokens.is_none() {
118-
block.tokens = Some(tokens);
118+
block.tokens = tokens;
119119
}
120120
token::NtBlock(block)
121121
}
@@ -124,7 +124,7 @@ impl<'a> Parser<'a> {
124124
match stmt {
125125
Some(mut s) => {
126126
if s.tokens.is_none() {
127-
s.tokens = Some(tokens);
127+
s.tokens = tokens;
128128
}
129129
token::NtStmt(s)
130130
}
@@ -137,7 +137,7 @@ impl<'a> Parser<'a> {
137137
let (mut pat, tokens) = self.collect_tokens(|this| this.parse_pat(None))?;
138138
// We have have eaten an NtPat, which could already have tokens
139139
if pat.tokens.is_none() {
140-
pat.tokens = Some(tokens);
140+
pat.tokens = tokens;
141141
}
142142
token::NtPat(pat)
143143
}
@@ -146,7 +146,7 @@ impl<'a> Parser<'a> {
146146
// If we captured tokens during parsing (due to outer attributes),
147147
// use those.
148148
if expr.tokens.is_none() {
149-
expr.tokens = Some(tokens);
149+
expr.tokens = tokens;
150150
}
151151
token::NtExpr(expr)
152152
}
@@ -155,15 +155,15 @@ impl<'a> Parser<'a> {
155155
self.collect_tokens(|this| this.parse_literal_maybe_minus())?;
156156
// We have have eaten a nonterminal, which could already have tokens
157157
if lit.tokens.is_none() {
158-
lit.tokens = Some(tokens);
158+
lit.tokens = tokens;
159159
}
160160
token::NtLiteral(lit)
161161
}
162162
NonterminalKind::Ty => {
163163
let (mut ty, tokens) = self.collect_tokens(|this| this.parse_ty())?;
164164
// We have an eaten an NtTy, which could already have tokens
165165
if ty.tokens.is_none() {
166-
ty.tokens = Some(tokens);
166+
ty.tokens = tokens;
167167
}
168168
token::NtTy(ty)
169169
}
@@ -183,15 +183,15 @@ impl<'a> Parser<'a> {
183183
self.collect_tokens(|this| this.parse_path(PathStyle::Type))?;
184184
// We have have eaten an NtPath, which could already have tokens
185185
if path.tokens.is_none() {
186-
path.tokens = Some(tokens);
186+
path.tokens = tokens;
187187
}
188188
token::NtPath(path)
189189
}
190190
NonterminalKind::Meta => {
191191
let (mut attr, tokens) = self.collect_tokens(|this| this.parse_attr_item(false))?;
192192
// We may have eaten a nonterminal, which could already have tokens
193193
if attr.tokens.is_none() {
194-
attr.tokens = Some(tokens);
194+
attr.tokens = tokens;
195195
}
196196
token::NtMeta(P(attr))
197197
}
@@ -201,7 +201,7 @@ impl<'a> Parser<'a> {
201201
self.collect_tokens(|this| this.parse_visibility(FollowedByType::Yes))?;
202202
// We may have etan an `NtVis`, which could already have tokens
203203
if vis.tokens.is_none() {
204-
vis.tokens = Some(tokens);
204+
vis.tokens = tokens;
205205
}
206206
token::NtVis(vis)
207207
}

0 commit comments

Comments
 (0)