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`.
This commit is contained in:
Nicholas Nethercote 2024-08-06 10:17:46 +10:00
parent 55906aa240
commit c8098be41f
8 changed files with 52 additions and 40 deletions

View File

@ -8,7 +8,9 @@ use rustc_span::{sym, BytePos, Span};
use thin_vec::ThinVec; use thin_vec::ThinVec;
use tracing::debug; use tracing::debug;
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle}; use super::{
AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle, Trailing,
};
use crate::{errors, fluent_generated as fluent, maybe_whole}; use crate::{errors, fluent_generated as fluent, maybe_whole};
// Public for rustfmt usage // Public for rustfmt usage
@ -273,7 +275,7 @@ impl<'a> Parser<'a> {
if is_unsafe { if is_unsafe {
this.expect(&token::CloseDelim(Delimiter::Parenthesis))?; this.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
} }
Ok((ast::AttrItem { unsafety, path, args, tokens: None }, false)) Ok((ast::AttrItem { unsafety, path, args, tokens: None }, Trailing::No))
}; };
// Attr items don't have attributes. // Attr items don't have attributes.
self.collect_tokens_trailing_token(AttrWrapper::empty(), force_collect, do_parse) self.collect_tokens_trailing_token(AttrWrapper::empty(), force_collect, do_parse)

View File

@ -12,7 +12,7 @@ use rustc_span::{sym, Span, DUMMY_SP};
use super::{ use super::{
Capturing, FlatToken, ForceCollect, NodeRange, NodeReplacement, Parser, ParserRange, Capturing, FlatToken, ForceCollect, NodeRange, NodeReplacement, Parser, ParserRange,
TokenCursor, TokenCursor, Trailing,
}; };
/// A wrapper type to ensure that the parser handles outer attributes correctly. /// A wrapper type to ensure that the parser handles outer attributes correctly.
@ -168,7 +168,7 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
/// Parses code with `f`. If appropriate, it records the tokens (in /// Parses code with `f`. If appropriate, it records the tokens (in
/// `LazyAttrTokenStream` form) that were parsed in the result, accessible /// `LazyAttrTokenStream` form) that were parsed in the result, accessible
/// via the `HasTokens` trait. The second (bool) part of the callback's /// via the `HasTokens` trait. The `Trailing` part of the callback's
/// result indicates if an extra token should be captured, e.g. a comma or /// result indicates if an extra token should be captured, e.g. a comma or
/// semicolon. /// semicolon.
/// ///
@ -201,7 +201,7 @@ impl<'a> Parser<'a> {
&mut self, &mut self,
attrs: AttrWrapper, attrs: AttrWrapper,
force_collect: ForceCollect, force_collect: ForceCollect,
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, bool)>, f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, Trailing)>,
) -> PResult<'a, R> { ) -> PResult<'a, R> {
// We must collect if anything could observe the collected tokens, i.e. // We must collect if anything could observe the collected tokens, i.e.
// if any of the following conditions hold. // if any of the following conditions hold.
@ -234,9 +234,9 @@ impl<'a> Parser<'a> {
// `Parser::parse_inner_attributes`. // `Parser::parse_inner_attributes`.
let (mut ret, capture_trailing) = { let (mut ret, capture_trailing) = {
let prev_capturing = mem::replace(&mut self.capture_state.capturing, Capturing::Yes); let prev_capturing = mem::replace(&mut self.capture_state.capturing, Capturing::Yes);
let ret_and_trailing = f(self, attrs.attrs); let f_res = f(self, attrs.attrs);
self.capture_state.capturing = prev_capturing; self.capture_state.capturing = prev_capturing;
ret_and_trailing? f_res?
}; };
// When we're not in `capture_cfg` mode, then skip collecting and // When we're not in `capture_cfg` mode, then skip collecting and
@ -282,7 +282,7 @@ impl<'a> Parser<'a> {
let parser_replacements_end = self.capture_state.parser_replacements.len(); let parser_replacements_end = self.capture_state.parser_replacements.len();
assert!( assert!(
!(self.break_last_token && capture_trailing), !(self.break_last_token && matches!(capture_trailing, Trailing::Yes)),
"Cannot set break_last_token and have trailing token" "Cannot set break_last_token and have trailing token"
); );

View File

@ -2462,7 +2462,7 @@ impl<'a> Parser<'a> {
id: DUMMY_NODE_ID, id: DUMMY_NODE_ID,
is_placeholder: false, is_placeholder: false,
}, },
this.token == token::Comma, Trailing::from(this.token == token::Comma),
)) ))
}) })
} }
@ -3243,7 +3243,7 @@ impl<'a> Parser<'a> {
id: DUMMY_NODE_ID, id: DUMMY_NODE_ID,
is_placeholder: false, is_placeholder: false,
}, },
false, Trailing::No,
)) ))
}) })
} }
@ -3752,7 +3752,7 @@ impl<'a> Parser<'a> {
id: DUMMY_NODE_ID, id: DUMMY_NODE_ID,
is_placeholder: false, is_placeholder: false,
}, },
this.token == token::Comma, Trailing::from(this.token == token::Comma),
)) ))
}) })
} }
@ -3848,12 +3848,14 @@ impl<'a> Parser<'a> {
) -> PResult<'a, P<Expr>> { ) -> PResult<'a, P<Expr>> {
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let res = f(this, attrs)?; let res = f(this, attrs)?;
let trailing = (this.restrictions.contains(Restrictions::STMT_EXPR) let trailing = Trailing::from(
&& this.token == token::Semi) this.restrictions.contains(Restrictions::STMT_EXPR)
// FIXME: pass an additional condition through from the place && this.token == token::Semi
// where we know we need a comma, rather than assuming that // FIXME: pass an additional condition through from the place
// `#[attr] expr,` always captures a trailing comma. // where we know we need a comma, rather than assuming that
|| this.token == token::Comma; // `#[attr] expr,` always captures a trailing comma.
|| this.token == token::Comma,
);
Ok((res, trailing)) Ok((res, trailing))
}) })
} }

View File

@ -7,7 +7,7 @@ use rustc_span::symbol::{kw, Ident};
use rustc_span::Span; use rustc_span::Span;
use thin_vec::ThinVec; use thin_vec::ThinVec;
use super::{ForceCollect, Parser}; use super::{ForceCollect, Parser, Trailing};
use crate::errors::{ use crate::errors::{
self, MultipleWhereClauses, UnexpectedDefaultValueForLifetimeInGenericParameters, self, MultipleWhereClauses, UnexpectedDefaultValueForLifetimeInGenericParameters,
UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody, UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody,
@ -228,13 +228,13 @@ impl<'a> Parser<'a> {
span: where_predicate.span(), span: where_predicate.span(),
}); });
// FIXME - try to continue parsing other generics? // FIXME - try to continue parsing other generics?
return Ok((None, false)); return Ok((None, Trailing::No));
} }
Err(err) => { Err(err) => {
err.cancel(); err.cancel();
// FIXME - maybe we should overwrite 'self' outside of `collect_tokens`? // FIXME - maybe we should overwrite 'self' outside of `collect_tokens`?
this.restore_snapshot(snapshot); this.restore_snapshot(snapshot);
return Ok((None, false)); return Ok((None, Trailing::No));
} }
} }
} else { } else {
@ -248,14 +248,14 @@ impl<'a> Parser<'a> {
.emit_err(errors::AttrWithoutGenerics { span: attrs[0].span }); .emit_err(errors::AttrWithoutGenerics { span: attrs[0].span });
} }
} }
return Ok((None, false)); return Ok((None, Trailing::No));
}; };
if !this.eat(&token::Comma) { if !this.eat(&token::Comma) {
done = true; done = true;
} }
// We just ate the comma, so no need to capture the trailing token. // We just ate the comma, so no need to capture the trailing token.
Ok((param, false)) Ok((param, Trailing::No))
})?; })?;
if let Some(param) = param { if let Some(param) = param {

View File

@ -145,7 +145,7 @@ impl<'a> Parser<'a> {
let span = lo.to(this.prev_token.span); let span = lo.to(this.prev_token.span);
let id = DUMMY_NODE_ID; let id = DUMMY_NODE_ID;
let item = Item { ident, attrs, id, kind, vis, span, tokens: None }; let item = Item { ident, attrs, id, kind, vis, span, tokens: None };
return Ok((Some(item), false)); return Ok((Some(item), Trailing::No));
} }
// At this point, we have failed to parse an item. // At this point, we have failed to parse an item.
@ -160,7 +160,7 @@ impl<'a> Parser<'a> {
if !attrs_allowed { if !attrs_allowed {
this.recover_attrs_no_item(&attrs)?; this.recover_attrs_no_item(&attrs)?;
} }
Ok((None, false)) Ok((None, Trailing::No))
}) })
} }
@ -1554,7 +1554,7 @@ impl<'a> Parser<'a> {
let vis = this.parse_visibility(FollowedByType::No)?; let vis = this.parse_visibility(FollowedByType::No)?;
if !this.recover_nested_adt_item(kw::Enum)? { if !this.recover_nested_adt_item(kw::Enum)? {
return Ok((None, false)); return Ok((None, Trailing::No));
} }
let ident = this.parse_field_ident("enum", vlo)?; let ident = this.parse_field_ident("enum", vlo)?;
@ -1566,7 +1566,7 @@ impl<'a> Parser<'a> {
this.bump(); this.bump();
this.parse_delim_args()?; this.parse_delim_args()?;
return Ok((None, this.token == token::Comma)); return Ok((None, Trailing::from(this.token == token::Comma)));
} }
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) { let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
@ -1623,7 +1623,7 @@ impl<'a> Parser<'a> {
is_placeholder: false, is_placeholder: false,
}; };
Ok((Some(vr), this.token == token::Comma)) Ok((Some(vr), Trailing::from(this.token == token::Comma)))
}, },
) )
.map_err(|mut err| { .map_err(|mut err| {
@ -1815,7 +1815,7 @@ impl<'a> Parser<'a> {
attrs, attrs,
is_placeholder: false, is_placeholder: false,
}, },
p.token == token::Comma, Trailing::from(p.token == token::Comma),
)) ))
}) })
}) })
@ -1830,7 +1830,8 @@ impl<'a> Parser<'a> {
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let lo = this.token.span; let lo = this.token.span;
let vis = this.parse_visibility(FollowedByType::No)?; let vis = this.parse_visibility(FollowedByType::No)?;
this.parse_single_struct_field(adt_ty, lo, vis, attrs).map(|field| (field, false)) this.parse_single_struct_field(adt_ty, lo, vis, attrs)
.map(|field| (field, Trailing::No))
}) })
} }
@ -2810,7 +2811,7 @@ impl<'a> Parser<'a> {
if let Some(mut param) = this.parse_self_param()? { if let Some(mut param) = this.parse_self_param()? {
param.attrs = attrs; param.attrs = attrs;
let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) }; let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
return Ok((res?, false)); return Ok((res?, Trailing::No));
} }
let is_name_required = match this.token.kind { let is_name_required = match this.token.kind {
@ -2826,7 +2827,7 @@ impl<'a> Parser<'a> {
this.parameter_without_type(&mut err, pat, is_name_required, first_param) this.parameter_without_type(&mut err, pat, is_name_required, first_param)
{ {
let guar = err.emit(); let guar = err.emit();
Ok((dummy_arg(ident, guar), false)) Ok((dummy_arg(ident, guar), Trailing::No))
} else { } else {
Err(err) Err(err)
}; };
@ -2869,7 +2870,7 @@ impl<'a> Parser<'a> {
Ok(( Ok((
Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty }, Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
false, Trailing::No,
)) ))
}) })
} }

View File

@ -388,6 +388,12 @@ enum Trailing {
Yes, Yes,
} }
impl From<bool> for Trailing {
fn from(b: bool) -> Trailing {
if b { Trailing::Yes } else { Trailing::No }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum TokenDescription { pub(super) enum TokenDescription {
ReservedIdentifier, ReservedIdentifier,
@ -1549,7 +1555,7 @@ impl<'a> Parser<'a> {
self.collect_tokens_trailing_token( self.collect_tokens_trailing_token(
AttrWrapper::empty(), AttrWrapper::empty(),
ForceCollect::Yes, ForceCollect::Yes,
|this, _attrs| Ok((f(this)?, false)), |this, _attrs| Ok((f(this)?, Trailing::No)),
) )
} }

View File

@ -1318,7 +1318,7 @@ impl<'a> Parser<'a> {
last_non_comma_dotdot_span = Some(this.prev_token.span); last_non_comma_dotdot_span = Some(this.prev_token.span);
// We just ate a comma, so there's no need to capture a trailing token. // We just ate a comma, so there's no need to capture a trailing token.
Ok((field, false)) Ok((field, Trailing::No))
})?; })?;
fields.push(field) fields.push(field)

View File

@ -21,6 +21,7 @@ use super::pat::{PatternLocation, RecoverComma};
use super::path::PathStyle; use super::path::PathStyle;
use super::{ use super::{
AttrWrapper, BlockMode, FnParseMode, ForceCollect, Parser, Restrictions, SemiColonMode, AttrWrapper, BlockMode, FnParseMode, ForceCollect, Parser, Restrictions, SemiColonMode,
Trailing,
}; };
use crate::errors::MalformedLoopLabel; use crate::errors::MalformedLoopLabel;
use crate::{errors, maybe_whole}; use crate::{errors, maybe_whole};
@ -68,7 +69,7 @@ impl<'a> Parser<'a> {
self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| { self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| {
this.expect_keyword(kw::Let)?; this.expect_keyword(kw::Let)?;
let local = this.parse_local(attrs)?; let local = this.parse_local(attrs)?;
let trailing = capture_semi && this.token == token::Semi; let trailing = Trailing::from(capture_semi && this.token == token::Semi);
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), trailing)) Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), trailing))
})? })?
} else if self.is_kw_followed_by_ident(kw::Mut) && self.may_recover() { } else if self.is_kw_followed_by_ident(kw::Mut) && self.may_recover() {
@ -106,7 +107,7 @@ impl<'a> Parser<'a> {
let stmt = self.collect_tokens_trailing_token( let stmt = self.collect_tokens_trailing_token(
AttrWrapper::empty(), AttrWrapper::empty(),
force_collect, force_collect,
|this, _empty_attrs| Ok((this.parse_stmt_path_start(lo, attrs)?, false)), |this, _empty_attrs| Ok((this.parse_stmt_path_start(lo, attrs)?, Trailing::No)),
); );
match stmt { match stmt {
Ok(stmt) => stmt, Ok(stmt) => stmt,
@ -133,7 +134,7 @@ impl<'a> Parser<'a> {
AttrWrapper::empty(), AttrWrapper::empty(),
force_collect, force_collect,
|this, _empty_attrs| { |this, _empty_attrs| {
Ok((this.parse_expr_res(Restrictions::STMT_EXPR, attrs)?, false)) Ok((this.parse_expr_res(Restrictions::STMT_EXPR, attrs)?, Trailing::No))
}, },
)?; )?;
if matches!(e.kind, ExprKind::Assign(..)) && self.eat_keyword(kw::Else) { if matches!(e.kind, ExprKind::Assign(..)) && self.eat_keyword(kw::Else) {
@ -155,7 +156,7 @@ impl<'a> Parser<'a> {
if this.eat(&token::Not) { if this.eat(&token::Not) {
let stmt_mac = this.parse_stmt_mac(lo, attrs, path)?; let stmt_mac = this.parse_stmt_mac(lo, attrs, path)?;
return Ok((stmt_mac, this.token == token::Semi)); return Ok((stmt_mac, Trailing::from(this.token == token::Semi)));
} }
let expr = if this.eat(&token::OpenDelim(Delimiter::Brace)) { let expr = if this.eat(&token::OpenDelim(Delimiter::Brace)) {
@ -169,7 +170,7 @@ impl<'a> Parser<'a> {
this.parse_expr_dot_or_call_with(attrs, expr, lo) this.parse_expr_dot_or_call_with(attrs, expr, lo)
})?; })?;
// `DUMMY_SP` will get overwritten later in this function // `DUMMY_SP` will get overwritten later in this function
Ok((this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)), false)) Ok((this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)), Trailing::No))
})?; })?;
if let StmtKind::Expr(expr) = stmt.kind { if let StmtKind::Expr(expr) = stmt.kind {
@ -242,7 +243,7 @@ impl<'a> Parser<'a> {
let stmt = self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| { let stmt = self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| {
let local = this.parse_local(attrs)?; let local = this.parse_local(attrs)?;
// FIXME - maybe capture semicolon in recovery? // FIXME - maybe capture semicolon in recovery?
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), false)) Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), Trailing::No))
})?; })?;
self.dcx() self.dcx()
.emit_err(errors::InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) }); .emit_err(errors::InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) });