Skip to content

Commit 38bd4fb

Browse files
committed
rename to BoundKind and add comments
1 parent 4375b36 commit 38bd4fb

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed

compiler/rustc_ast/src/visit.rs

+26-14
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,21 @@ pub enum FnCtxt {
3333
}
3434

3535
#[derive(Copy, Clone, Debug)]
36-
pub enum BoundCtxt {
37-
Normal,
36+
pub enum BoundKind {
37+
/// Trait bounds in generics bounds and type/trait alias.
38+
/// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.
39+
Bound,
40+
41+
/// Trait bounds in `impl` type.
42+
/// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.
43+
Impl,
44+
45+
/// Trait bounds in trait object type.
46+
/// E.g., `dyn Bound1 + Bound2 + Bound3`.
3847
TraitObject,
48+
49+
/// Super traits of a trait.
50+
/// E.g., `trait A: B`
3951
SuperTraits,
4052
}
4153

@@ -146,7 +158,7 @@ pub trait Visitor<'ast>: Sized {
146158
fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
147159
walk_trait_ref(self, t)
148160
}
149-
fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundCtxt) {
161+
fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) {
150162
walk_param_bound(self, bounds)
151163
}
152164
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
@@ -318,7 +330,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
318330
ItemKind::GlobalAsm(ref asm) => walk_inline_asm(visitor, asm),
319331
ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
320332
visitor.visit_generics(generics);
321-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
333+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
322334
walk_list!(visitor, visit_ty, ty);
323335
}
324336
ItemKind::Enum(ref enum_definition, ref generics) => {
@@ -353,12 +365,12 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
353365
ref items,
354366
}) => {
355367
visitor.visit_generics(generics);
356-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::SuperTraits);
368+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
357369
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
358370
}
359371
ItemKind::TraitAlias(ref generics, ref bounds) => {
360372
visitor.visit_generics(generics);
361-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
373+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
362374
}
363375
ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
364376
ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
@@ -424,10 +436,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
424436
visitor.visit_anon_const(length)
425437
}
426438
TyKind::TraitObject(ref bounds, ..) => {
427-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::TraitObject);
439+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
428440
}
429441
TyKind::ImplTrait(_, ref bounds) => {
430-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
442+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
431443
}
432444
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
433445
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
@@ -513,7 +525,7 @@ pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'
513525
Term::Const(c) => visitor.visit_anon_const(c),
514526
},
515527
AssocConstraintKind::Bound { ref bounds } => {
516-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
528+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
517529
}
518530
}
519531
}
@@ -576,7 +588,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
576588
}
577589
ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
578590
visitor.visit_generics(generics);
579-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
591+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
580592
walk_list!(visitor, visit_ty, ty);
581593
}
582594
ForeignItemKind::MacCall(mac) => {
@@ -595,7 +607,7 @@ pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericB
595607
pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
596608
visitor.visit_ident(param.ident);
597609
walk_list!(visitor, visit_attribute, param.attrs.iter());
598-
walk_list!(visitor, visit_param_bound, &param.bounds, BoundCtxt::Normal);
610+
walk_list!(visitor, visit_param_bound, &param.bounds, BoundKind::Bound);
599611
match param.kind {
600612
GenericParamKind::Lifetime => (),
601613
GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
@@ -622,14 +634,14 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a
622634
..
623635
}) => {
624636
visitor.visit_ty(bounded_ty);
625-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
637+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
626638
walk_list!(visitor, visit_generic_param, bound_generic_params);
627639
}
628640
WherePredicate::RegionPredicate(WhereRegionPredicate {
629641
ref lifetime, ref bounds, ..
630642
}) => {
631643
visitor.visit_lifetime(lifetime);
632-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
644+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
633645
}
634646
WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
635647
visitor.visit_ty(lhs_ty);
@@ -682,7 +694,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
682694
}
683695
AssocItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
684696
visitor.visit_generics(generics);
685-
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
697+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
686698
walk_list!(visitor, visit_ty, ty);
687699
}
688700
AssocItemKind::MacCall(mac) => {

compiler/rustc_ast_passes/src/ast_validation.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use itertools::{Either, Itertools};
1010
use rustc_ast::ptr::P;
11-
use rustc_ast::visit::{self, AssocCtxt, BoundCtxt, FnCtxt, FnKind, Visitor};
11+
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
1212
use rustc_ast::walk_list;
1313
use rustc_ast::*;
1414
use rustc_ast_pretty::pprust::{self, State};
@@ -1231,7 +1231,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12311231
self.visit_ident(item.ident);
12321232
self.visit_generics(generics);
12331233
self.with_banned_tilde_const(|this| {
1234-
walk_list!(this, visit_param_bound, bounds, BoundCtxt::SuperTraits)
1234+
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits)
12351235
});
12361236
walk_list!(self, visit_assoc_item, items, AssocCtxt::Trait);
12371237
walk_list!(self, visit_attribute, &item.attrs);
@@ -1459,10 +1459,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14591459
visit::walk_generic_param(self, param);
14601460
}
14611461

1462-
fn visit_param_bound(&mut self, bound: &'a GenericBound, ctxt: BoundCtxt) {
1462+
fn visit_param_bound(&mut self, bound: &'a GenericBound, ctxt: BoundKind) {
14631463
if let GenericBound::Trait(ref poly, modify) = *bound {
14641464
match (ctxt, modify) {
1465-
(BoundCtxt::SuperTraits, TraitBoundModifier::Maybe) => {
1465+
(BoundKind::SuperTraits, TraitBoundModifier::Maybe) => {
14661466
let mut err = self.err_handler().struct_span_err(
14671467
poly.span,
14681468
&format!("`?Trait` is not permitted in supertraits"),
@@ -1471,7 +1471,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14711471
err.note(&format!("traits are `?{}` by default", path_str));
14721472
err.emit();
14731473
}
1474-
(BoundCtxt::TraitObject, TraitBoundModifier::Maybe) => {
1474+
(BoundKind::TraitObject, TraitBoundModifier::Maybe) => {
14751475
let mut err = self.err_handler().struct_span_err(
14761476
poly.span,
14771477
&format!("`?Trait` is not permitted in trait object types"),
@@ -1661,7 +1661,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16611661
walk_list!(self, visit_attribute, &item.attrs);
16621662
self.with_tilde_const_allowed(|this| {
16631663
this.visit_generics(generics);
1664-
walk_list!(this, visit_param_bound, bounds, BoundCtxt::Normal);
1664+
walk_list!(this, visit_param_bound, bounds, BoundKind::Bound);
16651665
});
16661666
walk_list!(self, visit_ty, ty);
16671667
}

compiler/rustc_ast_passes/src/node_count.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'ast> Visitor<'ast> for NodeCounter {
7676
self.count += 1;
7777
walk_trait_ref(self, t)
7878
}
79-
fn visit_param_bound(&mut self, bounds: &GenericBound, _ctxt: BoundCtxt) {
79+
fn visit_param_bound(&mut self, bounds: &GenericBound, _ctxt: BoundKind) {
8080
self.count += 1;
8181
walk_param_bound(self, bounds)
8282
}

compiler/rustc_passes/src/hir_stats.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// completely accurate (some things might be counted twice, others missed).
44

55
use rustc_ast::visit as ast_visit;
6-
use rustc_ast::visit::BoundCtxt;
6+
use rustc_ast::visit::BoundKind;
77
use rustc_ast::{self as ast, AttrId, NodeId};
88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
99
use rustc_hir as hir;
@@ -303,7 +303,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
303303
ast_visit::walk_assoc_item(self, item, ctxt);
304304
}
305305

306-
fn visit_param_bound(&mut self, bounds: &'v ast::GenericBound, _ctxt: BoundCtxt) {
306+
fn visit_param_bound(&mut self, bounds: &'v ast::GenericBound, _ctxt: BoundKind) {
307307
self.record("GenericBound", Id::None, bounds);
308308
ast_visit::walk_param_bound(self, bounds)
309309
}

compiler/rustc_resolve/src/late.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
1212
use crate::{ResolutionError, Resolver, Segment, UseError};
1313

1414
use rustc_ast::ptr::P;
15-
use rustc_ast::visit::{self, AssocCtxt, BoundCtxt, FnCtxt, FnKind, Visitor};
15+
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
1616
use rustc_ast::*;
1717
use rustc_ast_lowering::ResolverAstLowering;
1818
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -835,7 +835,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
835835
this.visit_generic_param_vec(&bound_generic_params, false);
836836
this.visit_ty(bounded_ty);
837837
for bound in bounds {
838-
this.visit_param_bound(bound, BoundCtxt::Normal)
838+
this.visit_param_bound(bound, BoundKind::Bound)
839839
}
840840
},
841841
);
@@ -1026,12 +1026,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10261026
match param.kind {
10271027
GenericParamKind::Lifetime => {
10281028
for bound in &param.bounds {
1029-
this.visit_param_bound(bound, BoundCtxt::Normal);
1029+
this.visit_param_bound(bound, BoundKind::Bound);
10301030
}
10311031
}
10321032
GenericParamKind::Type { ref default } => {
10331033
for bound in &param.bounds {
1034-
this.visit_param_bound(bound, BoundCtxt::Normal);
1034+
this.visit_param_bound(bound, BoundKind::Bound);
10351035
}
10361036

10371037
if let Some(ref ty) = default {
@@ -1496,7 +1496,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
14961496
Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
14971497
|this| {
14981498
this.visit_generics(generics);
1499-
walk_list!(this, visit_param_bound, bounds, BoundCtxt::SuperTraits);
1499+
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits);
15001500

15011501
let walk_assoc_item =
15021502
|this: &mut Self,
@@ -1580,7 +1580,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15801580
Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
15811581
|this| {
15821582
this.visit_generics(generics);
1583-
walk_list!(this, visit_param_bound, bounds, BoundCtxt::Normal);
1583+
walk_list!(this, visit_param_bound, bounds, BoundKind::Bound);
15841584
},
15851585
);
15861586
},

0 commit comments

Comments
 (0)