Skip to content

Commit 33dcf1e

Browse files
committed
Use two-variant enum over bool
1 parent 505c470 commit 33dcf1e

File tree

7 files changed

+38
-19
lines changed

7 files changed

+38
-19
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ impl Expr {
13911391
path.clone(),
13921392
TraitBoundModifiers::NONE,
13931393
self.span,
1394-
false,
1394+
Grouping::None,
13951395
))),
13961396
_ => None,
13971397
}
@@ -3361,6 +3361,13 @@ pub struct TraitRef {
33613361
pub ref_id: NodeId,
33623362
}
33633363

3364+
/// Whether a `PolyTraitRef` is enclosed in parentheses or not.
3365+
#[derive(Clone, Encodable, Decodable, Debug)]
3366+
pub enum Grouping {
3367+
None,
3368+
Parenthesized,
3369+
}
3370+
33643371
#[derive(Clone, Encodable, Decodable, Debug)]
33653372
pub struct PolyTraitRef {
33663373
/// The `'a` in `for<'a> Foo<&'a T>`.
@@ -3374,8 +3381,9 @@ pub struct PolyTraitRef {
33743381

33753382
pub span: Span,
33763383

3377-
/// Whether first and last character of `span` are an opening and a closing paren.
3378-
pub parens: bool,
3384+
/// When `Parenthesized`, the first and last character of `span` are an opening
3385+
/// and a closing paren respectively.
3386+
pub grouping: Grouping,
33793387
}
33803388

33813389
impl PolyTraitRef {
@@ -3384,14 +3392,14 @@ impl PolyTraitRef {
33843392
path: Path,
33853393
modifiers: TraitBoundModifiers,
33863394
span: Span,
3387-
parens: bool,
3395+
grouping: Grouping,
33883396
) -> Self {
33893397
PolyTraitRef {
33903398
bound_generic_params: generic_params,
33913399
modifiers,
33923400
trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
33933401
span,
3394-
parens,
3402+
grouping,
33953403
}
33963404
}
33973405
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ macro_rules! common_visitor_and_walkers {
11421142
vis: &mut V,
11431143
p: &$($lt)? $($mut)? PolyTraitRef,
11441144
) -> V::Result {
1145-
let PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ } = p;
1145+
let PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, grouping: _ } = p;
11461146
try_visit!(visit_modifiers(vis, modifiers));
11471147
try_visit!(visit_generic_params(vis, bound_generic_params));
11481148
try_visit!(vis.visit_trait_ref(trait_ref));

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12091209
modifiers: TraitBoundModifiers::NONE,
12101210
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
12111211
span: t.span,
1212-
parens: false,
1212+
grouping: ast::Grouping::None,
12131213
},
12141214
itctx,
12151215
);

compiler/rustc_expand/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a> ExtCtxt<'a> {
195195
},
196196
trait_ref: self.trait_ref(path),
197197
span,
198-
parens: false,
198+
grouping: ast::Grouping::None,
199199
}
200200
}
201201

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ impl EarlyLintPass for UnusedParens {
13441344
.map(|s| s.ident.name == kw::PathRoot)
13451345
.unwrap_or(false);
13461346

1347-
if poly_trait_ref.parens
1347+
if let ast::Grouping::Parenthesized = poly_trait_ref.grouping
13481348
&& (last || !fn_with_explicit_ret_ty)
13491349
&& !dyn2015_exception
13501350
{

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'a> Parser<'a> {
310310
path,
311311
lo,
312312
parse_plus,
313-
false,
313+
ast::Grouping::None,
314314
)?;
315315
let err = self.dcx().create_err(errors::TransposeDynOrImpl {
316316
span: kw.span,
@@ -338,7 +338,13 @@ impl<'a> Parser<'a> {
338338
} else {
339339
let path = self.parse_path(PathStyle::Type)?;
340340
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
341-
self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus, false)?
341+
self.parse_remaining_bounds_path(
342+
lifetime_defs,
343+
path,
344+
lo,
345+
parse_plus,
346+
ast::Grouping::None,
347+
)?
342348
}
343349
}
344350
} else if self.eat_keyword(exp!(Impl)) {
@@ -418,9 +424,13 @@ impl<'a> Parser<'a> {
418424
let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
419425
match ty.kind {
420426
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
421-
TyKind::Path(None, path) if maybe_bounds => {
422-
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true, true)
423-
}
427+
TyKind::Path(None, path) if maybe_bounds => self.parse_remaining_bounds_path(
428+
ThinVec::new(),
429+
path,
430+
lo,
431+
true,
432+
ast::Grouping::Parenthesized,
433+
),
424434
// For `('a) + …`, we know that `'a` in type position already lead to an error being
425435
// emitted. To reduce output, let's indirectly suppress E0178 (bad `+` in type) and
426436
// other irrelevant consequential errors.
@@ -500,14 +510,14 @@ impl<'a> Parser<'a> {
500510
path: ast::Path,
501511
lo: Span,
502512
parse_plus: bool,
503-
has_parens: bool,
513+
grouping: ast::Grouping,
504514
) -> PResult<'a, TyKind> {
505515
let poly_trait_ref = PolyTraitRef::new(
506516
generic_params,
507517
path,
508518
TraitBoundModifiers::NONE,
509519
lo.to(self.prev_token.span),
510-
has_parens,
520+
grouping,
511521
);
512522
let bounds = vec![GenericBound::Trait(poly_trait_ref)];
513523
self.parse_remaining_bounds(bounds, parse_plus)
@@ -839,7 +849,7 @@ impl<'a> Parser<'a> {
839849
Ok(TyKind::MacCall(P(MacCall { path, args: self.parse_delim_args()? })))
840850
} else if allow_plus == AllowPlus::Yes && self.check_plus() {
841851
// `Trait1 + Trait2 + 'a`
842-
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true, false)
852+
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true, ast::Grouping::None)
843853
} else {
844854
// Just a type path.
845855
Ok(TyKind::Path(None, path))
@@ -1197,12 +1207,13 @@ impl<'a> Parser<'a> {
11971207
}
11981208
}
11991209

1210+
let grouping = if has_parens { ast::Grouping::Parenthesized } else { ast::Grouping::None };
12001211
let poly_trait = PolyTraitRef::new(
12011212
lifetime_defs,
12021213
path,
12031214
modifiers,
12041215
lo.to(self.prev_token.span),
1205-
has_parens,
1216+
grouping,
12061217
);
12071218
Ok(GenericBound::Trait(poly_trait))
12081219
}

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3832,7 +3832,7 @@ fn mk_where_bound_predicate(
38323832
ref_id: DUMMY_NODE_ID,
38333833
},
38343834
span: DUMMY_SP,
3835-
parens: false,
3835+
grouping: ast::Grouping::None,
38363836
})],
38373837
};
38383838

0 commit comments

Comments
 (0)