Skip to content

Commit c8098be

Browse files
committed
Convert a bool to Trailing.
This pre-existing type is suitable for use with the return value of the `f` parameter in `collect_tokens_trailing_token`. The more descriptive name will be useful because the next commit will add another boolean value to the return value of `f`.
1 parent 55906aa commit c8098be

File tree

8 files changed

+52
-40
lines changed

8 files changed

+52
-40
lines changed

Diff for: compiler/rustc_parse/src/parser/attr.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_span::{sym, BytePos, Span};
88
use thin_vec::ThinVec;
99
use tracing::debug;
1010

11-
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle};
11+
use super::{
12+
AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle, Trailing,
13+
};
1214
use crate::{errors, fluent_generated as fluent, maybe_whole};
1315

1416
// Public for rustfmt usage
@@ -273,7 +275,7 @@ impl<'a> Parser<'a> {
273275
if is_unsafe {
274276
this.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
275277
}
276-
Ok((ast::AttrItem { unsafety, path, args, tokens: None }, false))
278+
Ok((ast::AttrItem { unsafety, path, args, tokens: None }, Trailing::No))
277279
};
278280
// Attr items don't have attributes.
279281
self.collect_tokens_trailing_token(AttrWrapper::empty(), force_collect, do_parse)

Diff for: compiler/rustc_parse/src/parser/attr_wrapper.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_span::{sym, Span, DUMMY_SP};
1212

1313
use super::{
1414
Capturing, FlatToken, ForceCollect, NodeRange, NodeReplacement, Parser, ParserRange,
15-
TokenCursor,
15+
TokenCursor, Trailing,
1616
};
1717

1818
/// A wrapper type to ensure that the parser handles outer attributes correctly.
@@ -168,7 +168,7 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
168168
impl<'a> Parser<'a> {
169169
/// Parses code with `f`. If appropriate, it records the tokens (in
170170
/// `LazyAttrTokenStream` form) that were parsed in the result, accessible
171-
/// via the `HasTokens` trait. The second (bool) part of the callback's
171+
/// via the `HasTokens` trait. The `Trailing` part of the callback's
172172
/// result indicates if an extra token should be captured, e.g. a comma or
173173
/// semicolon.
174174
///
@@ -201,7 +201,7 @@ impl<'a> Parser<'a> {
201201
&mut self,
202202
attrs: AttrWrapper,
203203
force_collect: ForceCollect,
204-
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, bool)>,
204+
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, Trailing)>,
205205
) -> PResult<'a, R> {
206206
// We must collect if anything could observe the collected tokens, i.e.
207207
// if any of the following conditions hold.
@@ -234,9 +234,9 @@ impl<'a> Parser<'a> {
234234
// `Parser::parse_inner_attributes`.
235235
let (mut ret, capture_trailing) = {
236236
let prev_capturing = mem::replace(&mut self.capture_state.capturing, Capturing::Yes);
237-
let ret_and_trailing = f(self, attrs.attrs);
237+
let f_res = f(self, attrs.attrs);
238238
self.capture_state.capturing = prev_capturing;
239-
ret_and_trailing?
239+
f_res?
240240
};
241241

242242
// When we're not in `capture_cfg` mode, then skip collecting and
@@ -282,7 +282,7 @@ impl<'a> Parser<'a> {
282282
let parser_replacements_end = self.capture_state.parser_replacements.len();
283283

284284
assert!(
285-
!(self.break_last_token && capture_trailing),
285+
!(self.break_last_token && matches!(capture_trailing, Trailing::Yes)),
286286
"Cannot set break_last_token and have trailing token"
287287
);
288288

Diff for: compiler/rustc_parse/src/parser/expr.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,7 @@ impl<'a> Parser<'a> {
24622462
id: DUMMY_NODE_ID,
24632463
is_placeholder: false,
24642464
},
2465-
this.token == token::Comma,
2465+
Trailing::from(this.token == token::Comma),
24662466
))
24672467
})
24682468
}
@@ -3243,7 +3243,7 @@ impl<'a> Parser<'a> {
32433243
id: DUMMY_NODE_ID,
32443244
is_placeholder: false,
32453245
},
3246-
false,
3246+
Trailing::No,
32473247
))
32483248
})
32493249
}
@@ -3752,7 +3752,7 @@ impl<'a> Parser<'a> {
37523752
id: DUMMY_NODE_ID,
37533753
is_placeholder: false,
37543754
},
3755-
this.token == token::Comma,
3755+
Trailing::from(this.token == token::Comma),
37563756
))
37573757
})
37583758
}
@@ -3848,12 +3848,14 @@ impl<'a> Parser<'a> {
38483848
) -> PResult<'a, P<Expr>> {
38493849
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
38503850
let res = f(this, attrs)?;
3851-
let trailing = (this.restrictions.contains(Restrictions::STMT_EXPR)
3852-
&& this.token == token::Semi)
3853-
// FIXME: pass an additional condition through from the place
3854-
// where we know we need a comma, rather than assuming that
3855-
// `#[attr] expr,` always captures a trailing comma.
3856-
|| this.token == token::Comma;
3851+
let trailing = Trailing::from(
3852+
this.restrictions.contains(Restrictions::STMT_EXPR)
3853+
&& this.token == token::Semi
3854+
// FIXME: pass an additional condition through from the place
3855+
// where we know we need a comma, rather than assuming that
3856+
// `#[attr] expr,` always captures a trailing comma.
3857+
|| this.token == token::Comma,
3858+
);
38573859
Ok((res, trailing))
38583860
})
38593861
}

Diff for: compiler/rustc_parse/src/parser/generics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::symbol::{kw, Ident};
77
use rustc_span::Span;
88
use thin_vec::ThinVec;
99

10-
use super::{ForceCollect, Parser};
10+
use super::{ForceCollect, Parser, Trailing};
1111
use crate::errors::{
1212
self, MultipleWhereClauses, UnexpectedDefaultValueForLifetimeInGenericParameters,
1313
UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody,
@@ -228,13 +228,13 @@ impl<'a> Parser<'a> {
228228
span: where_predicate.span(),
229229
});
230230
// FIXME - try to continue parsing other generics?
231-
return Ok((None, false));
231+
return Ok((None, Trailing::No));
232232
}
233233
Err(err) => {
234234
err.cancel();
235235
// FIXME - maybe we should overwrite 'self' outside of `collect_tokens`?
236236
this.restore_snapshot(snapshot);
237-
return Ok((None, false));
237+
return Ok((None, Trailing::No));
238238
}
239239
}
240240
} else {
@@ -248,14 +248,14 @@ impl<'a> Parser<'a> {
248248
.emit_err(errors::AttrWithoutGenerics { span: attrs[0].span });
249249
}
250250
}
251-
return Ok((None, false));
251+
return Ok((None, Trailing::No));
252252
};
253253

254254
if !this.eat(&token::Comma) {
255255
done = true;
256256
}
257257
// We just ate the comma, so no need to capture the trailing token.
258-
Ok((param, false))
258+
Ok((param, Trailing::No))
259259
})?;
260260

261261
if let Some(param) = param {

Diff for: compiler/rustc_parse/src/parser/item.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a> Parser<'a> {
145145
let span = lo.to(this.prev_token.span);
146146
let id = DUMMY_NODE_ID;
147147
let item = Item { ident, attrs, id, kind, vis, span, tokens: None };
148-
return Ok((Some(item), false));
148+
return Ok((Some(item), Trailing::No));
149149
}
150150

151151
// At this point, we have failed to parse an item.
@@ -160,7 +160,7 @@ impl<'a> Parser<'a> {
160160
if !attrs_allowed {
161161
this.recover_attrs_no_item(&attrs)?;
162162
}
163-
Ok((None, false))
163+
Ok((None, Trailing::No))
164164
})
165165
}
166166

@@ -1554,7 +1554,7 @@ impl<'a> Parser<'a> {
15541554

15551555
let vis = this.parse_visibility(FollowedByType::No)?;
15561556
if !this.recover_nested_adt_item(kw::Enum)? {
1557-
return Ok((None, false));
1557+
return Ok((None, Trailing::No));
15581558
}
15591559
let ident = this.parse_field_ident("enum", vlo)?;
15601560

@@ -1566,7 +1566,7 @@ impl<'a> Parser<'a> {
15661566
this.bump();
15671567
this.parse_delim_args()?;
15681568

1569-
return Ok((None, this.token == token::Comma));
1569+
return Ok((None, Trailing::from(this.token == token::Comma)));
15701570
}
15711571

15721572
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
@@ -1623,7 +1623,7 @@ impl<'a> Parser<'a> {
16231623
is_placeholder: false,
16241624
};
16251625

1626-
Ok((Some(vr), this.token == token::Comma))
1626+
Ok((Some(vr), Trailing::from(this.token == token::Comma)))
16271627
},
16281628
)
16291629
.map_err(|mut err| {
@@ -1815,7 +1815,7 @@ impl<'a> Parser<'a> {
18151815
attrs,
18161816
is_placeholder: false,
18171817
},
1818-
p.token == token::Comma,
1818+
Trailing::from(p.token == token::Comma),
18191819
))
18201820
})
18211821
})
@@ -1830,7 +1830,8 @@ impl<'a> Parser<'a> {
18301830
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
18311831
let lo = this.token.span;
18321832
let vis = this.parse_visibility(FollowedByType::No)?;
1833-
this.parse_single_struct_field(adt_ty, lo, vis, attrs).map(|field| (field, false))
1833+
this.parse_single_struct_field(adt_ty, lo, vis, attrs)
1834+
.map(|field| (field, Trailing::No))
18341835
})
18351836
}
18361837

@@ -2810,7 +2811,7 @@ impl<'a> Parser<'a> {
28102811
if let Some(mut param) = this.parse_self_param()? {
28112812
param.attrs = attrs;
28122813
let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
2813-
return Ok((res?, false));
2814+
return Ok((res?, Trailing::No));
28142815
}
28152816

28162817
let is_name_required = match this.token.kind {
@@ -2826,7 +2827,7 @@ impl<'a> Parser<'a> {
28262827
this.parameter_without_type(&mut err, pat, is_name_required, first_param)
28272828
{
28282829
let guar = err.emit();
2829-
Ok((dummy_arg(ident, guar), false))
2830+
Ok((dummy_arg(ident, guar), Trailing::No))
28302831
} else {
28312832
Err(err)
28322833
};
@@ -2869,7 +2870,7 @@ impl<'a> Parser<'a> {
28692870

28702871
Ok((
28712872
Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
2872-
false,
2873+
Trailing::No,
28732874
))
28742875
})
28752876
}

Diff for: compiler/rustc_parse/src/parser/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ enum Trailing {
388388
Yes,
389389
}
390390

391+
impl From<bool> for Trailing {
392+
fn from(b: bool) -> Trailing {
393+
if b { Trailing::Yes } else { Trailing::No }
394+
}
395+
}
396+
391397
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
392398
pub(super) enum TokenDescription {
393399
ReservedIdentifier,
@@ -1549,7 +1555,7 @@ impl<'a> Parser<'a> {
15491555
self.collect_tokens_trailing_token(
15501556
AttrWrapper::empty(),
15511557
ForceCollect::Yes,
1552-
|this, _attrs| Ok((f(this)?, false)),
1558+
|this, _attrs| Ok((f(this)?, Trailing::No)),
15531559
)
15541560
}
15551561

Diff for: compiler/rustc_parse/src/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ impl<'a> Parser<'a> {
13181318
last_non_comma_dotdot_span = Some(this.prev_token.span);
13191319

13201320
// We just ate a comma, so there's no need to capture a trailing token.
1321-
Ok((field, false))
1321+
Ok((field, Trailing::No))
13221322
})?;
13231323

13241324
fields.push(field)

Diff for: compiler/rustc_parse/src/parser/stmt.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use super::pat::{PatternLocation, RecoverComma};
2121
use super::path::PathStyle;
2222
use super::{
2323
AttrWrapper, BlockMode, FnParseMode, ForceCollect, Parser, Restrictions, SemiColonMode,
24+
Trailing,
2425
};
2526
use crate::errors::MalformedLoopLabel;
2627
use crate::{errors, maybe_whole};
@@ -68,7 +69,7 @@ impl<'a> Parser<'a> {
6869
self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| {
6970
this.expect_keyword(kw::Let)?;
7071
let local = this.parse_local(attrs)?;
71-
let trailing = capture_semi && this.token == token::Semi;
72+
let trailing = Trailing::from(capture_semi && this.token == token::Semi);
7273
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), trailing))
7374
})?
7475
} else if self.is_kw_followed_by_ident(kw::Mut) && self.may_recover() {
@@ -106,7 +107,7 @@ impl<'a> Parser<'a> {
106107
let stmt = self.collect_tokens_trailing_token(
107108
AttrWrapper::empty(),
108109
force_collect,
109-
|this, _empty_attrs| Ok((this.parse_stmt_path_start(lo, attrs)?, false)),
110+
|this, _empty_attrs| Ok((this.parse_stmt_path_start(lo, attrs)?, Trailing::No)),
110111
);
111112
match stmt {
112113
Ok(stmt) => stmt,
@@ -133,7 +134,7 @@ impl<'a> Parser<'a> {
133134
AttrWrapper::empty(),
134135
force_collect,
135136
|this, _empty_attrs| {
136-
Ok((this.parse_expr_res(Restrictions::STMT_EXPR, attrs)?, false))
137+
Ok((this.parse_expr_res(Restrictions::STMT_EXPR, attrs)?, Trailing::No))
137138
},
138139
)?;
139140
if matches!(e.kind, ExprKind::Assign(..)) && self.eat_keyword(kw::Else) {
@@ -155,7 +156,7 @@ impl<'a> Parser<'a> {
155156

156157
if this.eat(&token::Not) {
157158
let stmt_mac = this.parse_stmt_mac(lo, attrs, path)?;
158-
return Ok((stmt_mac, this.token == token::Semi));
159+
return Ok((stmt_mac, Trailing::from(this.token == token::Semi)));
159160
}
160161

161162
let expr = if this.eat(&token::OpenDelim(Delimiter::Brace)) {
@@ -169,7 +170,7 @@ impl<'a> Parser<'a> {
169170
this.parse_expr_dot_or_call_with(attrs, expr, lo)
170171
})?;
171172
// `DUMMY_SP` will get overwritten later in this function
172-
Ok((this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)), false))
173+
Ok((this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)), Trailing::No))
173174
})?;
174175

175176
if let StmtKind::Expr(expr) = stmt.kind {
@@ -242,7 +243,7 @@ impl<'a> Parser<'a> {
242243
let stmt = self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| {
243244
let local = this.parse_local(attrs)?;
244245
// FIXME - maybe capture semicolon in recovery?
245-
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), false))
246+
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), Trailing::No))
246247
})?;
247248
self.dcx()
248249
.emit_err(errors::InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) });

0 commit comments

Comments
 (0)