Skip to content

Commit 0f4a440

Browse files
authored
Rollup merge of #125635 - fmease:mv-type-binding-assoc-item-constraint, r=compiler-errors
Rename HIR `TypeBinding` to `AssocItemConstraint` and related cleanup Rename `hir::TypeBinding` and `ast::AssocConstraint` to `AssocItemConstraint` and update all items and locals using the old terminology. Motivation: The terminology *type binding* is extremely outdated. "Type bindings" not only include constraints on associated *types* but also on associated *constants* (feature `associated_const_equality`) and on RPITITs of associated *functions* (feature `return_type_notation`). Hence the word *item* in the new name. Furthermore, the word *binding* commonly refers to a mapping from a binder/identifier to a "value" for some definition of "value". Its use in "type binding" made sense when equality constraints (e.g., `AssocTy = Ty`) were the only kind of associated item constraint. Nowadays however, we also have *associated type bounds* (e.g., `AssocTy: Bound`) for which the term *binding* doesn't make sense. --- Old terminology (HIR, rustdoc): ``` `TypeBinding`: (associated) type binding ├── `Constraint`: associated type bound └── `Equality`: (associated) equality constraint (?) ├── `Ty`: (associated) type binding └── `Const`: associated const equality (constraint) ``` Old terminology (AST, abbrev.): ``` `AssocConstraint` ├── `Bound` └── `Equality` ├── `Ty` └── `Const` ``` New terminology (AST, HIR, rustdoc): ``` `AssocItemConstraint`: associated item constraint ├── `Bound`: associated type bound └── `Equality`: associated item equality constraint OR associated item binding (for short) ├── `Ty`: associated type equality constraint OR associated type binding (for short) └── `Const`: associated const equality constraint OR associated const binding (for short) ``` r? compiler-errors
2 parents e084908 + 84125d8 commit 0f4a440

File tree

5 files changed

+44
-51
lines changed

5 files changed

+44
-51
lines changed

clippy_lints/src/implied_bounds_in_impls.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet;
33
use rustc_errors::{Applicability, SuggestionStyle};
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{
6-
GenericArg, GenericBound, GenericBounds, ItemKind, PredicateOrigin, TraitBoundModifier, TyKind, TypeBinding,
6+
GenericArg, GenericBound, GenericBounds, ItemKind, PredicateOrigin, TraitBoundModifier, TyKind, AssocItemConstraint,
77
WherePredicate,
88
};
99
use rustc_hir_analysis::lower_ty;
@@ -54,9 +54,9 @@ fn emit_lint(
5454
poly_trait: &rustc_hir::PolyTraitRef<'_>,
5555
bounds: GenericBounds<'_>,
5656
index: usize,
57-
// The bindings that were implied, used for suggestion purposes since removing a bound with associated types
58-
// means we might need to then move it to a different bound
59-
implied_bindings: &[TypeBinding<'_>],
57+
// The constraints that were implied, used for suggestion purposes since removing a bound with
58+
// associated types means we might need to then move it to a different bound.
59+
implied_constraints: &[AssocItemConstraint<'_>],
6060
bound: &ImplTraitBound<'_>,
6161
) {
6262
let implied_by = snippet(cx, bound.span, "..");
@@ -83,46 +83,46 @@ fn emit_lint(
8383

8484
let mut sugg = vec![(implied_span_extended, String::new())];
8585

86-
// We also might need to include associated type binding that were specified in the implied bound,
86+
// We also might need to include associated item constraints that were specified in the implied bound,
8787
// but omitted in the implied-by bound:
8888
// `fn f() -> impl Deref<Target = u8> + DerefMut`
8989
// If we're going to suggest removing `Deref<..>`, we'll need to put `<Target = u8>` on `DerefMut`
90-
let omitted_assoc_tys: Vec<_> = implied_bindings
90+
let omitted_constraints: Vec<_> = implied_constraints
9191
.iter()
92-
.filter(|binding| !bound.bindings.iter().any(|b| b.ident == binding.ident))
92+
.filter(|constraint| !bound.constraints.iter().any(|c| c.ident == constraint.ident))
9393
.collect();
9494

95-
if !omitted_assoc_tys.is_empty() {
96-
// `<>` needs to be added if there aren't yet any generic arguments or bindings
97-
let needs_angle_brackets = bound.args.is_empty() && bound.bindings.is_empty();
98-
let insert_span = match (bound.args, bound.bindings) {
99-
([.., arg], [.., binding]) => arg.span().max(binding.span).shrink_to_hi(),
95+
if !omitted_constraints.is_empty() {
96+
// `<>` needs to be added if there aren't yet any generic arguments or constraints
97+
let needs_angle_brackets = bound.args.is_empty() && bound.constraints.is_empty();
98+
let insert_span = match (bound.args, bound.constraints) {
99+
([.., arg], [.., constraint]) => arg.span().max(constraint.span).shrink_to_hi(),
100100
([.., arg], []) => arg.span().shrink_to_hi(),
101-
([], [.., binding]) => binding.span.shrink_to_hi(),
101+
([], [.., constraint]) => constraint.span.shrink_to_hi(),
102102
([], []) => bound.span.shrink_to_hi(),
103103
};
104104

105-
let mut associated_tys_sugg = if needs_angle_brackets {
105+
let mut constraints_sugg = if needs_angle_brackets {
106106
"<".to_owned()
107107
} else {
108-
// If angle brackets aren't needed (i.e., there are already generic arguments or bindings),
108+
// If angle brackets aren't needed (i.e., there are already generic arguments or constraints),
109109
// we need to add a comma:
110110
// `impl A<B, C >`
111111
// ^ if we insert `Assoc=i32` without a comma here, that'd be invalid syntax:
112112
// `impl A<B, C Assoc=i32>`
113113
", ".to_owned()
114114
};
115115

116-
for (index, binding) in omitted_assoc_tys.into_iter().enumerate() {
116+
for (index, constraint) in omitted_constraints.into_iter().enumerate() {
117117
if index > 0 {
118-
associated_tys_sugg += ", ";
118+
constraints_sugg += ", ";
119119
}
120-
associated_tys_sugg += &snippet(cx, binding.span, "..");
120+
constraints_sugg += &snippet(cx, constraint.span, "..");
121121
}
122122
if needs_angle_brackets {
123-
associated_tys_sugg += ">";
123+
constraints_sugg += ">";
124124
}
125-
sugg.push((insert_span, associated_tys_sugg));
125+
sugg.push((insert_span, constraints_sugg));
126126
}
127127

128128
diag.multipart_suggestion_with_style(
@@ -229,8 +229,8 @@ struct ImplTraitBound<'tcx> {
229229
trait_def_id: DefId,
230230
/// The generic arguments on the `impl Trait` bound
231231
args: &'tcx [GenericArg<'tcx>],
232-
/// The associated types on this bound
233-
bindings: &'tcx [TypeBinding<'tcx>],
232+
/// The associated item constraints of this bound
233+
constraints: &'tcx [AssocItemConstraint<'tcx>],
234234
}
235235

236236
/// Given an `impl Trait` type, gets all the supertraits from each bound ("implied bounds").
@@ -253,7 +253,7 @@ fn collect_supertrait_bounds<'tcx>(cx: &LateContext<'tcx>, bounds: GenericBounds
253253
Some(ImplTraitBound {
254254
predicates,
255255
args: path.args.map_or([].as_slice(), |p| p.args),
256-
bindings: path.args.map_or([].as_slice(), |p| p.bindings),
256+
constraints: path.args.map_or([].as_slice(), |p| p.constraints),
257257
trait_def_id,
258258
span: bound.span(),
259259
})
@@ -310,20 +310,20 @@ fn check<'tcx>(cx: &LateContext<'tcx>, bounds: GenericBounds<'tcx>) {
310310
if let GenericBound::Trait(poly_trait, TraitBoundModifier::None) = bound
311311
&& let [.., path] = poly_trait.trait_ref.path.segments
312312
&& let implied_args = path.args.map_or([].as_slice(), |a| a.args)
313-
&& let implied_bindings = path.args.map_or([].as_slice(), |a| a.bindings)
313+
&& let implied_constraints = path.args.map_or([].as_slice(), |a| a.constraints)
314314
&& let Some(def_id) = poly_trait.trait_ref.path.res.opt_def_id()
315315
&& let Some(bound) = find_bound_in_supertraits(cx, def_id, implied_args, &supertraits)
316316
// If the implied bound has a type binding that also exists in the implied-by trait,
317317
// then we shouldn't lint. See #11880 for an example.
318318
&& let assocs = cx.tcx.associated_items(bound.trait_def_id)
319-
&& !implied_bindings.iter().any(|binding| {
319+
&& !implied_constraints.iter().any(|constraint| {
320320
assocs
321-
.filter_by_name_unhygienic(binding.ident.name)
321+
.filter_by_name_unhygienic(constraint.ident.name)
322322
.next()
323323
.is_some_and(|assoc| assoc.kind == ty::AssocKind::Type)
324324
})
325325
{
326-
emit_lint(cx, poly_trait, bounds, index, implied_bindings, bound);
326+
emit_lint(cx, poly_trait, bounds, index, implied_constraints, bound);
327327
}
328328
}
329329
}

clippy_lints/src/len_zero.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::def_id::{DefId, DefIdSet};
99
use rustc_hir::{
1010
AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, GenericArg, GenericBound, ImplItem, ImplItemKind,
1111
ImplicitSelfKind, Item, ItemKind, Mutability, Node, OpaqueTyOrigin, PatKind, PathSegment, PrimTy, QPath,
12-
TraitItemRef, TyKind, TypeBindingKind,
12+
TraitItemRef, TyKind,
1313
};
1414
use rustc_lint::{LateContext, LateLintPass};
1515
use rustc_middle::ty::{self, AssocKind, FnSig, Ty};
@@ -307,17 +307,12 @@ fn extract_future_output<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<&
307307
&& let [GenericBound::Trait(trait_ref, _)] = &opaque.bounds
308308
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
309309
&& let Some(generic_args) = segment.args
310-
&& generic_args.bindings.len() == 1
311-
&& let TypeBindingKind::Equality {
312-
term:
313-
rustc_hir::Term::Ty(rustc_hir::Ty {
314-
kind: TyKind::Path(QPath::Resolved(_, path)),
315-
..
316-
}),
317-
} = &generic_args.bindings[0].kind
318-
&& path.segments.len() == 1
310+
&& let [constraint] = generic_args.constraints
311+
&& let Some(ty) = constraint.ty()
312+
&& let TyKind::Path(QPath::Resolved(_, path)) = ty.kind
313+
&& let [segment] = path.segments
319314
{
320-
return Some(&path.segments[0]);
315+
return Some(segment);
321316
}
322317

323318
None

clippy_lints/src/manual_async_fn.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use rustc_errors::Applicability;
44
use rustc_hir::intravisit::FnKind;
55
use rustc_hir::{
66
Block, Body, Closure, ClosureKind, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, FnDecl,
7-
FnRetTy, GenericArg, GenericBound, ImplItem, Item, ItemKind, LifetimeName, Node, Term, TraitRef, Ty, TyKind,
8-
TypeBindingKind,
7+
FnRetTy, GenericArg, GenericBound, ImplItem, Item, ItemKind, LifetimeName, Node, TraitRef, Ty, TyKind,
98
};
109
use rustc_lint::{LateContext, LateLintPass};
1110
use rustc_session::declare_lint_pass;
@@ -138,10 +137,9 @@ fn future_trait_ref<'tcx>(
138137
fn future_output_ty<'tcx>(trait_ref: &'tcx TraitRef<'tcx>) -> Option<&'tcx Ty<'tcx>> {
139138
if let Some(segment) = trait_ref.path.segments.last()
140139
&& let Some(args) = segment.args
141-
&& args.bindings.len() == 1
142-
&& let binding = &args.bindings[0]
143-
&& binding.ident.name == sym::Output
144-
&& let TypeBindingKind::Equality { term: Term::Ty(output) } = binding.kind
140+
&& let [constraint] = args.constraints
141+
&& constraint.ident.name == sym::Output
142+
&& let Some(output) = constraint.ty()
145143
{
146144
return Some(output);
147145
}

clippy_utils/src/ast_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
108108
pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool {
109109
match (l, r) {
110110
(AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r),
111-
(AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_constraint(l, r),
111+
(AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_item_constraint(l, r),
112112
_ => false,
113113
}
114114
}
@@ -802,8 +802,8 @@ fn eq_term(l: &Term, r: &Term) -> bool {
802802
}
803803
}
804804

805-
pub fn eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool {
806-
use AssocConstraintKind::*;
805+
pub fn eq_assoc_item_constraint(l: &AssocItemConstraint, r: &AssocItemConstraint) -> bool {
806+
use AssocItemConstraintKind::*;
807807
eq_id(l.ident, r.ident)
808808
&& match (&l.kind, &r.kind) {
809809
(Equality { term: l }, Equality { term: r }) => eq_term(l, r),

clippy_utils/src/hir_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::MatchSource::TryDesugar;
99
use rustc_hir::{
1010
ArrayLen, BinOpKind, BindingMode, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy, GenericArg,
1111
GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime, LifetimeName, Pat, PatField, PatKind, Path,
12-
PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
12+
PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, AssocItemConstraint,
1313
};
1414
use rustc_lexer::{tokenize, TokenKind};
1515
use rustc_lint::LateContext;
@@ -486,7 +486,7 @@ impl HirEqInterExpr<'_, '_, '_> {
486486
fn eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool {
487487
if left.parenthesized == right.parenthesized {
488488
over(left.args, right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
489-
&& over(left.bindings, right.bindings, |l, r| self.eq_type_binding(l, r))
489+
&& over(left.constraints, right.constraints, |l, r| self.eq_assoc_type_binding(l, r))
490490
} else {
491491
false
492492
}
@@ -518,8 +518,8 @@ impl HirEqInterExpr<'_, '_, '_> {
518518
}
519519
}
520520

521-
fn eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -> bool {
522-
left.ident.name == right.ident.name && self.eq_ty(left.ty(), right.ty())
521+
fn eq_assoc_type_binding(&mut self, left: &AssocItemConstraint<'_>, right: &AssocItemConstraint<'_>) -> bool {
522+
left.ident.name == right.ident.name && self.eq_ty(left.ty().expect("expected assoc type binding"), right.ty().expect("expected assoc type binding"))
523523
}
524524

525525
fn check_ctxt(&mut self, left: SyntaxContext, right: SyntaxContext) -> bool {

0 commit comments

Comments
 (0)