Skip to content

Commit 34c56c4

Browse files
committed
Rename HIR TypeBinding to AssocItemConstraint and related cleanup
1 parent 0a59f11 commit 34c56c4

File tree

108 files changed

+879
-819
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+879
-819
lines changed

compiler/rustc_ast/src/ast.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl PathSegment {
167167
}
168168
}
169169

170-
/// The arguments of a path segment.
170+
/// The generic arguments and associated item constraints of a path segment.
171171
///
172172
/// E.g., `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`.
173173
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -221,14 +221,13 @@ pub struct AngleBracketedArgs {
221221
pub args: ThinVec<AngleBracketedArg>,
222222
}
223223

224-
/// Either an argument for a parameter e.g., `'a`, `Vec<u8>`, `0`,
225-
/// or a constraint on an associated item, e.g., `Item = String` or `Item: Bound`.
224+
/// Either an argument for a generic parameter or a constraint on an associated item.
226225
#[derive(Clone, Encodable, Decodable, Debug)]
227226
pub enum AngleBracketedArg {
228-
/// Argument for a generic parameter.
227+
/// A generic argument for a generic parameter.
229228
Arg(GenericArg),
230-
/// Constraint for an associated item.
231-
Constraint(AssocConstraint),
229+
/// A constraint on an associated item.
230+
Constraint(AssocItemConstraint),
232231
}
233232

234233
impl AngleBracketedArg {
@@ -418,7 +417,7 @@ impl Default for WhereClause {
418417
/// A single predicate in a where-clause.
419418
#[derive(Clone, Encodable, Decodable, Debug)]
420419
pub enum WherePredicate {
421-
/// A type binding (e.g., `for<'c> Foo: Send + Clone + 'c`).
420+
/// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
422421
BoundPredicate(WhereBoundPredicate),
423422
/// A lifetime predicate (e.g., `'a: 'b + 'c`).
424423
RegionPredicate(WhereRegionPredicate),
@@ -2034,18 +2033,25 @@ impl UintTy {
20342033
}
20352034
}
20362035

2037-
/// A constraint on an associated type (e.g., `A = Bar` in `Foo<A = Bar>` or
2038-
/// `A: TraitA + TraitB` in `Foo<A: TraitA + TraitB>`).
2039-
#[derive(Clone, Encodable, Decodable, Debug)]
2040-
pub struct AssocConstraint {
2036+
/// A constraint on an associated item.
2037+
///
2038+
/// ### Examples
2039+
///
2040+
/// * the `A = Ty` and `B = Ty` in `Trait<A = Ty, B = Ty>`
2041+
/// * the `G<Ty> = Ty` in `Trait<G<Ty> = Ty>`
2042+
/// * the `A: Bound` in `Trait<A: Bound>`
2043+
/// * the `RetTy` in `Trait(ArgTy, ArgTy) -> RetTy`
2044+
/// * the `C = { Ct }` in `Trait<C = { Ct }>` (feature `associated_const_equality`)
2045+
/// * the `f(): Bound` in `Trait<f(): Bound>` (feature `return_type_notation`)
2046+
#[derive(Clone, Encodable, Decodable, Debug)]
2047+
pub struct AssocItemConstraint {
20412048
pub id: NodeId,
20422049
pub ident: Ident,
20432050
pub gen_args: Option<GenericArgs>,
2044-
pub kind: AssocConstraintKind,
2051+
pub kind: AssocItemConstraintKind,
20452052
pub span: Span,
20462053
}
20472054

2048-
/// The kinds of an `AssocConstraint`.
20492055
#[derive(Clone, Encodable, Decodable, Debug)]
20502056
pub enum Term {
20512057
Ty(P<Ty>),
@@ -2064,12 +2070,17 @@ impl From<AnonConst> for Term {
20642070
}
20652071
}
20662072

2067-
/// The kinds of an `AssocConstraint`.
2073+
/// The kind of [associated item constraint][AssocItemConstraint].
20682074
#[derive(Clone, Encodable, Decodable, Debug)]
2069-
pub enum AssocConstraintKind {
2070-
/// E.g., `A = Bar`, `A = 3` in `Foo<A = Bar>` where A is an associated type.
2075+
pub enum AssocItemConstraintKind {
2076+
/// An equality constraint for an associated item (e.g., `AssocTy = Ty` in `Trait<AssocTy = Ty>`).
2077+
///
2078+
/// Also known as an *associated item binding* (we *bind* an associated item to a term).
2079+
///
2080+
/// Furthermore, associated type equality constraints can also be referred to as *associated type
2081+
/// bindings*. Similarly with associated const equality constraints and *associated const bindings*.
20712082
Equality { term: Term },
2072-
/// E.g. `A: TraitA + TraitB` in `Foo<A: TraitA + TraitB>`.
2083+
/// A bound on an associated type (e.g., `AssocTy: Bound` in `Trait<AssocTy: Bound>`).
20732084
Bound { bounds: GenericBounds },
20742085
}
20752086

compiler/rustc_ast/src/mut_visit.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ pub trait MutVisitor: Sized {
175175
noop_visit_lifetime(l, self);
176176
}
177177

178-
fn visit_constraint(&mut self, t: &mut AssocConstraint) {
179-
noop_visit_constraint(t, self);
178+
fn visit_assoc_item_constraint(&mut self, c: &mut AssocItemConstraint) {
179+
noop_visit_assoc_item_constraint(c, self);
180180
}
181181

182182
fn visit_foreign_mod(&mut self, nm: &mut ForeignMod) {
@@ -463,8 +463,8 @@ pub fn noop_flat_map_arm<T: MutVisitor>(mut arm: Arm, vis: &mut T) -> SmallVec<[
463463
smallvec![arm]
464464
}
465465

466-
fn noop_visit_constraint<T: MutVisitor>(
467-
AssocConstraint { id, ident, gen_args, kind, span }: &mut AssocConstraint,
466+
fn noop_visit_assoc_item_constraint<T: MutVisitor>(
467+
AssocItemConstraint { id, ident, gen_args, kind, span }: &mut AssocItemConstraint,
468468
vis: &mut T,
469469
) {
470470
vis.visit_id(id);
@@ -473,11 +473,11 @@ fn noop_visit_constraint<T: MutVisitor>(
473473
vis.visit_generic_args(gen_args);
474474
}
475475
match kind {
476-
AssocConstraintKind::Equality { term } => match term {
476+
AssocItemConstraintKind::Equality { term } => match term {
477477
Term::Ty(ty) => vis.visit_ty(ty),
478478
Term::Const(c) => vis.visit_anon_const(c),
479479
},
480-
AssocConstraintKind::Bound { bounds } => visit_bounds(bounds, vis),
480+
AssocItemConstraintKind::Bound { bounds } => visit_bounds(bounds, vis),
481481
}
482482
vis.visit_span(span);
483483
}
@@ -607,7 +607,7 @@ fn noop_visit_angle_bracketed_parameter_data<T: MutVisitor>(
607607
let AngleBracketedArgs { args, span } = data;
608608
visit_thin_vec(args, |arg| match arg {
609609
AngleBracketedArg::Arg(arg) => vis.visit_generic_arg(arg),
610-
AngleBracketedArg::Constraint(constraint) => vis.visit_constraint(constraint),
610+
AngleBracketedArg::Constraint(constraint) => vis.visit_assoc_item_constraint(constraint),
611611
});
612612
vis.visit_span(span);
613613
}

compiler/rustc_ast/src/visit.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,11 @@ pub trait Visitor<'ast>: Sized {
246246
fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) -> Self::Result {
247247
walk_generic_arg(self, generic_arg)
248248
}
249-
fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) -> Self::Result {
250-
walk_assoc_constraint(self, constraint)
249+
fn visit_assoc_item_constraint(
250+
&mut self,
251+
constraint: &'ast AssocItemConstraint,
252+
) -> Self::Result {
253+
walk_assoc_item_constraint(self, constraint)
251254
}
252255
fn visit_attribute(&mut self, attr: &'ast Attribute) -> Self::Result {
253256
walk_attribute(self, attr)
@@ -558,7 +561,7 @@ where
558561
match arg {
559562
AngleBracketedArg::Arg(a) => try_visit!(visitor.visit_generic_arg(a)),
560563
AngleBracketedArg::Constraint(c) => {
561-
try_visit!(visitor.visit_assoc_constraint(c))
564+
try_visit!(visitor.visit_assoc_item_constraint(c))
562565
}
563566
}
564567
}
@@ -582,18 +585,18 @@ where
582585
}
583586
}
584587

585-
pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(
588+
pub fn walk_assoc_item_constraint<'a, V: Visitor<'a>>(
586589
visitor: &mut V,
587-
constraint: &'a AssocConstraint,
590+
constraint: &'a AssocItemConstraint,
588591
) -> V::Result {
589592
try_visit!(visitor.visit_ident(constraint.ident));
590593
visit_opt!(visitor, visit_generic_args, &constraint.gen_args);
591594
match &constraint.kind {
592-
AssocConstraintKind::Equality { term } => match term {
595+
AssocItemConstraintKind::Equality { term } => match term {
593596
Term::Ty(ty) => try_visit!(visitor.visit_ty(ty)),
594597
Term::Const(c) => try_visit!(visitor.visit_anon_const(c)),
595598
},
596-
AssocConstraintKind::Bound { bounds } => {
599+
AssocItemConstraintKind::Bound { bounds } => {
597600
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
598601
}
599602
}

compiler/rustc_ast_lowering/src/index.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
333333
});
334334
}
335335

336-
fn visit_assoc_type_binding(&mut self, type_binding: &'hir TypeBinding<'hir>) {
337-
self.insert(type_binding.span, type_binding.hir_id, Node::TypeBinding(type_binding));
338-
self.with_parent(type_binding.hir_id, |this| {
339-
intravisit::walk_assoc_type_binding(this, type_binding)
336+
fn visit_assoc_item_constraint(&mut self, constraint: &'hir AssocItemConstraint<'hir>) {
337+
self.insert(constraint.span, constraint.hir_id, Node::AssocItemConstraint(constraint));
338+
self.with_parent(constraint.hir_id, |this| {
339+
intravisit::walk_assoc_item_constraint(this, constraint)
340340
})
341341
}
342342

compiler/rustc_ast_lowering/src/lib.rs

+21-30
Original file line numberDiff line numberDiff line change
@@ -961,24 +961,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
961961
DelimArgs { dspan: args.dspan, delim: args.delim, tokens: args.tokens.flattened() }
962962
}
963963

964-
/// Given an associated type constraint like one of these:
965-
///
966-
/// ```ignore (illustrative)
967-
/// T: Iterator<Item: Debug>
968-
/// ^^^^^^^^^^^
969-
/// T: Iterator<Item = Debug>
970-
/// ^^^^^^^^^^^^
971-
/// ```
972-
///
973-
/// returns a `hir::TypeBinding` representing `Item`.
974-
#[instrument(level = "debug", skip(self))]
975-
fn lower_assoc_ty_constraint(
964+
/// Lower an associated item constraint.
965+
#[instrument(level = "debug", skip_all)]
966+
fn lower_assoc_item_constraint(
976967
&mut self,
977-
constraint: &AssocConstraint,
968+
constraint: &AssocItemConstraint,
978969
itctx: ImplTraitContext,
979-
) -> hir::TypeBinding<'hir> {
980-
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
981-
// lower generic arguments of identifier in constraint
970+
) -> hir::AssocItemConstraint<'hir> {
971+
debug!(?constraint, ?itctx);
972+
// Lower the generic arguments for the associated item.
982973
let gen_args = if let Some(gen_args) = &constraint.gen_args {
983974
let gen_args_ctor = match gen_args {
984975
GenericArgs::AngleBracketed(data) => {
@@ -994,7 +985,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
994985
};
995986
GenericArgsCtor {
996987
args: Default::default(),
997-
bindings: &[],
988+
constraints: &[],
998989
parenthesized,
999990
span: data.inputs_span,
1000991
}
@@ -1024,7 +1015,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10241015
err.emit();
10251016
GenericArgsCtor {
10261017
args: Default::default(),
1027-
bindings: &[],
1018+
constraints: &[],
10281019
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
10291020
span: data.span,
10301021
}
@@ -1046,14 +1037,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10461037
self.arena.alloc(hir::GenericArgs::none())
10471038
};
10481039
let kind = match &constraint.kind {
1049-
AssocConstraintKind::Equality { term } => {
1040+
AssocItemConstraintKind::Equality { term } => {
10501041
let term = match term {
10511042
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
10521043
Term::Const(c) => self.lower_anon_const(c).into(),
10531044
};
1054-
hir::TypeBindingKind::Equality { term }
1045+
hir::AssocItemConstraintKind::Equality { term }
10551046
}
1056-
AssocConstraintKind::Bound { bounds } => {
1047+
AssocItemConstraintKind::Bound { bounds } => {
10571048
// Disallow ATB in dyn types
10581049
if self.is_in_dyn_type {
10591050
let suggestion = match itctx {
@@ -1077,18 +1068,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10771068
});
10781069
let err_ty =
10791070
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
1080-
hir::TypeBindingKind::Equality { term: err_ty.into() }
1071+
hir::AssocItemConstraintKind::Equality { term: err_ty.into() }
10811072
} else {
1082-
// Desugar `AssocTy: Bounds` into a type binding where the
1073+
// Desugar `AssocTy: Bounds` into an assoc type binding where the
10831074
// later desugars into a trait predicate.
10841075
let bounds = self.lower_param_bounds(bounds, itctx);
10851076

1086-
hir::TypeBindingKind::Constraint { bounds }
1077+
hir::AssocItemConstraintKind::Bound { bounds }
10871078
}
10881079
}
10891080
};
10901081

1091-
hir::TypeBinding {
1082+
hir::AssocItemConstraint {
10921083
hir_id: self.lower_node_id(constraint.id),
10931084
ident: self.lower_ident(constraint.ident),
10941085
gen_args,
@@ -2008,7 +1999,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20081999

20092000
let bound_args = self.arena.alloc(hir::GenericArgs {
20102001
args: &[],
2011-
bindings: arena_vec![self; self.assoc_ty_binding(assoc_ty_name, opaque_ty_span, output_ty)],
2002+
constraints: arena_vec![self; self.assoc_ty_binding(assoc_ty_name, opaque_ty_span, output_ty)],
20122003
parenthesized: hir::GenericArgsParentheses::No,
20132004
span_ext: DUMMY_SP,
20142005
});
@@ -2581,10 +2572,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25812572
}
25822573
}
25832574

2584-
/// Helper struct for delayed construction of GenericArgs.
2575+
/// Helper struct for the delayed construction of [`hir::GenericArgs`].
25852576
struct GenericArgsCtor<'hir> {
25862577
args: SmallVec<[hir::GenericArg<'hir>; 4]>,
2587-
bindings: &'hir [hir::TypeBinding<'hir>],
2578+
constraints: &'hir [hir::AssocItemConstraint<'hir>],
25882579
parenthesized: hir::GenericArgsParentheses,
25892580
span: Span,
25902581
}
@@ -2664,14 +2655,14 @@ impl<'hir> GenericArgsCtor<'hir> {
26642655

26652656
fn is_empty(&self) -> bool {
26662657
self.args.is_empty()
2667-
&& self.bindings.is_empty()
2658+
&& self.constraints.is_empty()
26682659
&& self.parenthesized == hir::GenericArgsParentheses::No
26692660
}
26702661

26712662
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
26722663
let ga = hir::GenericArgs {
26732664
args: this.arena.alloc_from_iter(self.args),
2674-
bindings: self.bindings,
2665+
constraints: self.constraints,
26752666
parenthesized: self.parenthesized,
26762667
span_ext: this.lower_span(self.span),
26772668
};

0 commit comments

Comments
 (0)