Skip to content

Commit bf8fff7

Browse files
committed
Auto merge of rust-lang#125797 - matthiaskrgr:rollup-v2jmg7i, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#125635 (Rename HIR `TypeBinding` to `AssocItemConstraint` and related cleanup) - rust-lang#125774 (Avoid unwrap diag.code directly in note_and_explain_type_err) - rust-lang#125786 (Fold item bounds before proving them in `check_type_bounds` in new solver) - rust-lang#125790 (Don't recompute `tail` in `lower_stmts`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 366da30 + c11e057 commit bf8fff7

File tree

147 files changed

+1494
-966
lines changed

Some content is hidden

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

147 files changed

+1494
-966
lines changed

Diff for: 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

Diff for: 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
}

Diff for: 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
}

Diff for: compiler/rustc_ast_lowering/src/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7676
StmtKind::Empty => {}
7777
StmtKind::MacCall(..) => panic!("shouldn't exist here"),
7878
}
79-
ast_stmts = &ast_stmts[1..];
79+
ast_stmts = tail;
8080
}
8181
(self.arena.alloc_from_iter(stmts), expr)
8282
}

Diff for: compiler/rustc_ast_lowering/src/index.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
325325
});
326326
}
327327

328-
fn visit_assoc_type_binding(&mut self, type_binding: &'hir TypeBinding<'hir>) {
329-
self.insert(type_binding.span, type_binding.hir_id, Node::TypeBinding(type_binding));
330-
self.with_parent(type_binding.hir_id, |this| {
331-
intravisit::walk_assoc_type_binding(this, type_binding)
328+
fn visit_assoc_item_constraint(&mut self, constraint: &'hir AssocItemConstraint<'hir>) {
329+
self.insert(constraint.span, constraint.hir_id, Node::AssocItemConstraint(constraint));
330+
self.with_parent(constraint.hir_id, |this| {
331+
intravisit::walk_assoc_item_constraint(this, constraint)
332332
})
333333
}
334334

Diff for: compiler/rustc_ast_lowering/src/lib.rs

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

970-
/// Given an associated type constraint like one of these:
971-
///
972-
/// ```ignore (illustrative)
973-
/// T: Iterator<Item: Debug>
974-
/// ^^^^^^^^^^^
975-
/// T: Iterator<Item = Debug>
976-
/// ^^^^^^^^^^^^
977-
/// ```
978-
///
979-
/// returns a `hir::TypeBinding` representing `Item`.
980-
#[instrument(level = "debug", skip(self))]
981-
fn lower_assoc_ty_constraint(
970+
/// Lower an associated item constraint.
971+
#[instrument(level = "debug", skip_all)]
972+
fn lower_assoc_item_constraint(
982973
&mut self,
983-
constraint: &AssocConstraint,
974+
constraint: &AssocItemConstraint,
984975
itctx: ImplTraitContext,
985-
) -> hir::TypeBinding<'hir> {
986-
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
987-
// lower generic arguments of identifier in constraint
976+
) -> hir::AssocItemConstraint<'hir> {
977+
debug!(?constraint, ?itctx);
978+
// Lower the generic arguments for the associated item.
988979
let gen_args = if let Some(gen_args) = &constraint.gen_args {
989980
let gen_args_ctor = match gen_args {
990981
GenericArgs::AngleBracketed(data) => {
@@ -1000,7 +991,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1000991
};
1001992
GenericArgsCtor {
1002993
args: Default::default(),
1003-
bindings: &[],
994+
constraints: &[],
1004995
parenthesized,
1005996
span: data.inputs_span,
1006997
}
@@ -1030,7 +1021,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10301021
err.emit();
10311022
GenericArgsCtor {
10321023
args: Default::default(),
1033-
bindings: &[],
1024+
constraints: &[],
10341025
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
10351026
span: data.span,
10361027
}
@@ -1052,14 +1043,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10521043
self.arena.alloc(hir::GenericArgs::none())
10531044
};
10541045
let kind = match &constraint.kind {
1055-
AssocConstraintKind::Equality { term } => {
1046+
AssocItemConstraintKind::Equality { term } => {
10561047
let term = match term {
10571048
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
10581049
Term::Const(c) => self.lower_anon_const(c).into(),
10591050
};
1060-
hir::TypeBindingKind::Equality { term }
1051+
hir::AssocItemConstraintKind::Equality { term }
10611052
}
1062-
AssocConstraintKind::Bound { bounds } => {
1053+
AssocItemConstraintKind::Bound { bounds } => {
10631054
// Disallow ATB in dyn types
10641055
if self.is_in_dyn_type {
10651056
let suggestion = match itctx {
@@ -1083,18 +1074,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10831074
});
10841075
let err_ty =
10851076
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
1086-
hir::TypeBindingKind::Equality { term: err_ty.into() }
1077+
hir::AssocItemConstraintKind::Equality { term: err_ty.into() }
10871078
} else {
1088-
// Desugar `AssocTy: Bounds` into a type binding where the
1079+
// Desugar `AssocTy: Bounds` into an assoc type binding where the
10891080
// later desugars into a trait predicate.
10901081
let bounds = self.lower_param_bounds(bounds, itctx);
10911082

1092-
hir::TypeBindingKind::Constraint { bounds }
1083+
hir::AssocItemConstraintKind::Bound { bounds }
10931084
}
10941085
}
10951086
};
10961087

1097-
hir::TypeBinding {
1088+
hir::AssocItemConstraint {
10981089
hir_id: self.lower_node_id(constraint.id),
10991090
ident: self.lower_ident(constraint.ident),
11001091
gen_args,
@@ -2014,7 +2005,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20142005

20152006
let bound_args = self.arena.alloc(hir::GenericArgs {
20162007
args: &[],
2017-
bindings: arena_vec![self; self.assoc_ty_binding(assoc_ty_name, opaque_ty_span, output_ty)],
2008+
constraints: arena_vec![self; self.assoc_ty_binding(assoc_ty_name, opaque_ty_span, output_ty)],
20182009
parenthesized: hir::GenericArgsParentheses::No,
20192010
span_ext: DUMMY_SP,
20202011
});
@@ -2587,10 +2578,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25872578
}
25882579
}
25892580

2590-
/// Helper struct for delayed construction of GenericArgs.
2581+
/// Helper struct for the delayed construction of [`hir::GenericArgs`].
25912582
struct GenericArgsCtor<'hir> {
25922583
args: SmallVec<[hir::GenericArg<'hir>; 4]>,
2593-
bindings: &'hir [hir::TypeBinding<'hir>],
2584+
constraints: &'hir [hir::AssocItemConstraint<'hir>],
25942585
parenthesized: hir::GenericArgsParentheses,
25952586
span: Span,
25962587
}
@@ -2670,14 +2661,14 @@ impl<'hir> GenericArgsCtor<'hir> {
26702661

26712662
fn is_empty(&self) -> bool {
26722663
self.args.is_empty()
2673-
&& self.bindings.is_empty()
2664+
&& self.constraints.is_empty()
26742665
&& self.parenthesized == hir::GenericArgsParentheses::No
26752666
}
26762667

26772668
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
26782669
let ga = hir::GenericArgs {
26792670
args: this.arena.alloc_from_iter(self.args),
2680-
bindings: self.bindings,
2671+
constraints: self.constraints,
26812672
parenthesized: self.parenthesized,
26822673
span_ext: this.lower_span(self.span),
26832674
};

0 commit comments

Comments
 (0)