rustc_hir_analysis/collect/
item_bounds.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_hir as hir;
use rustc_infer::traits::util;
use rustc_middle::ty::fold::shift_vars;
use rustc_middle::ty::{
    self, GenericArgs, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
};
use rustc_middle::{bug, span_bug};
use rustc_span::Span;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_type_ir::Upcast;
use tracing::{debug, instrument};

use super::ItemCtxt;
use super::predicates_of::assert_only_contains_predicates_from;
use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter};

/// For associated types we include both bounds written on the type
/// (`type X: Trait`) and predicates from the trait: `where Self::X: Trait`.
///
/// Note that this filtering is done with the items identity args to
/// simplify checking that these bounds are met in impls. This means that
/// a bound such as `for<'b> <Self as X<'b>>::U: Clone` can't be used, as in
/// `hr-associated-type-bound-1.rs`.
fn associated_type_bounds<'tcx>(
    tcx: TyCtxt<'tcx>,
    assoc_item_def_id: LocalDefId,
    hir_bounds: &'tcx [hir::GenericBound<'tcx>],
    span: Span,
    filter: PredicateFilter,
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
    let item_ty = Ty::new_projection_from_args(
        tcx,
        assoc_item_def_id.to_def_id(),
        GenericArgs::identity_for_item(tcx, assoc_item_def_id),
    );

    let icx = ItemCtxt::new(tcx, assoc_item_def_id);
    let mut bounds = icx.lowerer().lower_mono_bounds(item_ty, hir_bounds, filter);
    // Associated types are implicitly sized unless a `?Sized` bound is found
    icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span);

    let trait_def_id = tcx.local_parent(assoc_item_def_id);
    let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id);

    let item_trait_ref = ty::TraitRef::identity(tcx, tcx.parent(assoc_item_def_id.to_def_id()));
    let bounds_from_parent =
        trait_predicates.predicates.iter().copied().filter_map(|(clause, span)| {
            remap_gat_vars_and_recurse_into_nested_projections(
                tcx,
                filter,
                item_trait_ref,
                assoc_item_def_id,
                span,
                clause,
            )
        });

    let all_bounds = tcx.arena.alloc_from_iter(bounds.clauses(tcx).chain(bounds_from_parent));
    debug!(
        "associated_type_bounds({}) = {:?}",
        tcx.def_path_str(assoc_item_def_id.to_def_id()),
        all_bounds
    );

    assert_only_contains_predicates_from(filter, all_bounds, item_ty);

    all_bounds
}

/// The code below is quite involved, so let me explain.
///
/// We loop here, because we also want to collect vars for nested associated items as
/// well. For example, given a clause like `Self::A::B`, we want to add that to the
/// item bounds for `A`, so that we may use that bound in the case that `Self::A::B` is
/// rigid.
///
/// Secondly, regarding bound vars, when we see a where clause that mentions a GAT
/// like `for<'a, ...> Self::Assoc<'a, ...>: Bound<'b, ...>`, we want to turn that into
/// an item bound on the GAT, where all of the GAT args are substituted with the GAT's
/// param regions, and then keep all of the other late-bound vars in the bound around.
/// We need to "compress" the binder so that it doesn't mention any of those vars that
/// were mapped to params.
fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
    tcx: TyCtxt<'tcx>,
    filter: PredicateFilter,
    item_trait_ref: ty::TraitRef<'tcx>,
    assoc_item_def_id: LocalDefId,
    span: Span,
    clause: ty::Clause<'tcx>,
) -> Option<(ty::Clause<'tcx>, Span)> {
    let mut clause_ty = match clause.kind().skip_binder() {
        ty::ClauseKind::Trait(tr) => tr.self_ty(),
        ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty(),
        ty::ClauseKind::TypeOutlives(outlives) => outlives.0,
        _ => return None,
    };

    let gat_vars = loop {
        if let ty::Alias(ty::Projection, alias_ty) = *clause_ty.kind() {
            if alias_ty.trait_ref(tcx) == item_trait_ref
                && alias_ty.def_id == assoc_item_def_id.to_def_id()
            {
                // We have found the GAT in question...
                // Return the vars, since we may need to remap them.
                break &alias_ty.args[item_trait_ref.args.len()..];
            } else {
                // Only collect *self* type bounds if the filter is for self.
                match filter {
                    PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
                        return None;
                    }
                    PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
                }

                clause_ty = alias_ty.self_ty();
                continue;
            }
        }

        return None;
    };

    // Special-case: No GAT vars, no mapping needed.
    if gat_vars.is_empty() {
        return Some((clause, span));
    }

    // First, check that all of the GAT args are substituted with a unique late-bound arg.
    // If we find a duplicate, then it can't be mapped to the definition's params.
    let mut mapping = FxIndexMap::default();
    let generics = tcx.generics_of(assoc_item_def_id);
    for (param, var) in std::iter::zip(&generics.own_params, gat_vars) {
        let existing = match var.unpack() {
            ty::GenericArgKind::Lifetime(re) => {
                if let ty::RegionKind::ReBound(ty::INNERMOST, bv) = re.kind() {
                    mapping.insert(bv.var, tcx.mk_param_from_def(param))
                } else {
                    return None;
                }
            }
            ty::GenericArgKind::Type(ty) => {
                if let ty::Bound(ty::INNERMOST, bv) = *ty.kind() {
                    mapping.insert(bv.var, tcx.mk_param_from_def(param))
                } else {
                    return None;
                }
            }
            ty::GenericArgKind::Const(ct) => {
                if let ty::ConstKind::Bound(ty::INNERMOST, bv) = ct.kind() {
                    mapping.insert(bv, tcx.mk_param_from_def(param))
                } else {
                    return None;
                }
            }
        };

        if existing.is_some() {
            return None;
        }
    }

    // Finally, map all of the args in the GAT to the params we expect, and compress
    // the remaining late-bound vars so that they count up from var 0.
    let mut folder =
        MapAndCompressBoundVars { tcx, binder: ty::INNERMOST, still_bound_vars: vec![], mapping };
    let pred = clause.kind().skip_binder().fold_with(&mut folder);

    Some((
        ty::Binder::bind_with_vars(pred, tcx.mk_bound_variable_kinds(&folder.still_bound_vars))
            .upcast(tcx),
        span,
    ))
}

/// Given some where clause like `for<'b, 'c> <Self as Trait<'a_identity>>::Gat<'b>: Bound<'c>`,
/// the mapping will map `'b` back to the GAT's `'b_identity`. Then we need to compress the
/// remaining bound var `'c` to index 0.
///
/// This folder gives us: `for<'c> <Self as Trait<'a_identity>>::Gat<'b_identity>: Bound<'c>`,
/// which is sufficient for an item bound for `Gat`, since all of the GAT's args are identity.
struct MapAndCompressBoundVars<'tcx> {
    tcx: TyCtxt<'tcx>,
    /// How deep are we? Makes sure we don't touch the vars of nested binders.
    binder: ty::DebruijnIndex,
    /// List of bound vars that remain unsubstituted because they were not
    /// mentioned in the GAT's args.
    still_bound_vars: Vec<ty::BoundVariableKind>,
    /// Subtle invariant: If the `GenericArg` is bound, then it should be
    /// stored with the debruijn index of `INNERMOST` so it can be shifted
    /// correctly during substitution.
    mapping: FxIndexMap<ty::BoundVar, ty::GenericArg<'tcx>>,
}

impl<'tcx> TypeFolder<TyCtxt<'tcx>> for MapAndCompressBoundVars<'tcx> {
    fn cx(&self) -> TyCtxt<'tcx> {
        self.tcx
    }

    fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
    where
        ty::Binder<'tcx, T>: TypeSuperFoldable<TyCtxt<'tcx>>,
    {
        self.binder.shift_in(1);
        let out = t.super_fold_with(self);
        self.binder.shift_out(1);
        out
    }

    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
        if !ty.has_bound_vars() {
            return ty;
        }

        if let ty::Bound(binder, old_bound) = *ty.kind()
            && self.binder == binder
        {
            let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
                mapped.expect_ty()
            } else {
                // If we didn't find a mapped generic, then make a new one.
                // Allocate a new var idx, and insert a new bound ty.
                let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
                self.still_bound_vars.push(ty::BoundVariableKind::Ty(old_bound.kind));
                let mapped = Ty::new_bound(self.tcx, ty::INNERMOST, ty::BoundTy {
                    var,
                    kind: old_bound.kind,
                });
                self.mapping.insert(old_bound.var, mapped.into());
                mapped
            };

            shift_vars(self.tcx, mapped, self.binder.as_u32())
        } else {
            ty.super_fold_with(self)
        }
    }

    fn fold_region(&mut self, re: ty::Region<'tcx>) -> ty::Region<'tcx> {
        if let ty::ReBound(binder, old_bound) = re.kind()
            && self.binder == binder
        {
            let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
                mapped.expect_region()
            } else {
                let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
                self.still_bound_vars.push(ty::BoundVariableKind::Region(old_bound.kind));
                let mapped = ty::Region::new_bound(self.tcx, ty::INNERMOST, ty::BoundRegion {
                    var,
                    kind: old_bound.kind,
                });
                self.mapping.insert(old_bound.var, mapped.into());
                mapped
            };

            shift_vars(self.tcx, mapped, self.binder.as_u32())
        } else {
            re
        }
    }

    fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
        if !ct.has_bound_vars() {
            return ct;
        }

        if let ty::ConstKind::Bound(binder, old_var) = ct.kind()
            && self.binder == binder
        {
            let mapped = if let Some(mapped) = self.mapping.get(&old_var) {
                mapped.expect_const()
            } else {
                let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
                self.still_bound_vars.push(ty::BoundVariableKind::Const);
                let mapped = ty::Const::new_bound(self.tcx, ty::INNERMOST, var);
                self.mapping.insert(old_var, mapped.into());
                mapped
            };

            shift_vars(self.tcx, mapped, self.binder.as_u32())
        } else {
            ct.super_fold_with(self)
        }
    }

    fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
        if !p.has_bound_vars() { p } else { p.super_fold_with(self) }
    }
}

/// Opaque types don't inherit bounds from their parent: for return position
/// impl trait it isn't possible to write a suitable predicate on the
/// containing function and for type-alias impl trait we don't have a backwards
/// compatibility issue.
#[instrument(level = "trace", skip(tcx, item_ty))]
fn opaque_type_bounds<'tcx>(
    tcx: TyCtxt<'tcx>,
    opaque_def_id: LocalDefId,
    hir_bounds: &'tcx [hir::GenericBound<'tcx>],
    item_ty: Ty<'tcx>,
    span: Span,
    filter: PredicateFilter,
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
    ty::print::with_reduced_queries!({
        let icx = ItemCtxt::new(tcx, opaque_def_id);
        let mut bounds = icx.lowerer().lower_mono_bounds(item_ty, hir_bounds, filter);
        // Opaque types are implicitly sized unless a `?Sized` bound is found
        icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span);
        debug!(?bounds);

        tcx.arena.alloc_from_iter(bounds.clauses(tcx))
    })
}

pub(super) fn explicit_item_bounds(
    tcx: TyCtxt<'_>,
    def_id: LocalDefId,
) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
    explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::All)
}

pub(super) fn explicit_item_super_predicates(
    tcx: TyCtxt<'_>,
    def_id: LocalDefId,
) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
    explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::SelfOnly)
}

pub(super) fn explicit_item_bounds_with_filter(
    tcx: TyCtxt<'_>,
    def_id: LocalDefId,
    filter: PredicateFilter,
) -> ty::EarlyBinder<'_, &'_ [(ty::Clause<'_>, Span)]> {
    match tcx.opt_rpitit_info(def_id.to_def_id()) {
        // RPITIT's bounds are the same as opaque type bounds, but with
        // a projection self type.
        Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
            let opaque_ty = tcx.hir_node_by_def_id(opaque_def_id.expect_local()).expect_opaque_ty();
            let item_ty = Ty::new_projection_from_args(
                tcx,
                def_id.to_def_id(),
                ty::GenericArgs::identity_for_item(tcx, def_id),
            );
            let bounds = opaque_type_bounds(
                tcx,
                opaque_def_id.expect_local(),
                opaque_ty.bounds,
                item_ty,
                opaque_ty.span,
                filter,
            );
            assert_only_contains_predicates_from(filter, bounds, item_ty);
            return ty::EarlyBinder::bind(bounds);
        }
        Some(ty::ImplTraitInTraitData::Impl { .. }) => span_bug!(
            tcx.def_span(def_id),
            "item bounds for RPITIT in impl to be fed on def-id creation"
        ),
        None => {}
    }

    let bounds = match tcx.hir_node_by_def_id(def_id) {
        _ if tcx.is_effects_desugared_assoc_ty(def_id.to_def_id()) => {
            associated_type_bounds(tcx, def_id, &[], tcx.def_span(def_id), filter)
        }
        hir::Node::TraitItem(hir::TraitItem {
            kind: hir::TraitItemKind::Type(bounds, _),
            span,
            ..
        }) => associated_type_bounds(tcx, def_id, bounds, *span, filter),
        hir::Node::OpaqueTy(hir::OpaqueTy { bounds, origin, span, .. }) => match origin {
            // Since RPITITs are lowered as projections in `<dyn HirTyLowerer>::lower_ty`,
            // when we're asking for the item bounds of the *opaques* in a trait's default
            // method signature, we need to map these projections back to opaques.
            rustc_hir::OpaqueTyOrigin::FnReturn {
                parent,
                in_trait_or_impl: Some(hir::RpitContext::Trait),
            }
            | rustc_hir::OpaqueTyOrigin::AsyncFn {
                parent,
                in_trait_or_impl: Some(hir::RpitContext::Trait),
            } => {
                let args = GenericArgs::identity_for_item(tcx, def_id);
                let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
                let bounds = &*tcx.arena.alloc_slice(
                    &opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter)
                        .to_vec()
                        .fold_with(&mut AssocTyToOpaque { tcx, fn_def_id: parent.to_def_id() }),
                );
                assert_only_contains_predicates_from(filter, bounds, item_ty);
                bounds
            }
            rustc_hir::OpaqueTyOrigin::FnReturn {
                parent: _,
                in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
            }
            | rustc_hir::OpaqueTyOrigin::AsyncFn {
                parent: _,
                in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
            }
            | rustc_hir::OpaqueTyOrigin::TyAlias { parent: _, .. } => {
                let args = GenericArgs::identity_for_item(tcx, def_id);
                let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
                let bounds = opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter);
                assert_only_contains_predicates_from(filter, bounds, item_ty);
                bounds
            }
        },
        hir::Node::Item(hir::Item { kind: hir::ItemKind::TyAlias(..), .. }) => &[],
        node => bug!("item_bounds called on {def_id:?} => {node:?}"),
    };

    ty::EarlyBinder::bind(bounds)
}

pub(super) fn item_bounds(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
    tcx.explicit_item_bounds(def_id).map_bound(|bounds| {
        tcx.mk_clauses_from_iter(util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)))
    })
}

pub(super) fn item_super_predicates(
    tcx: TyCtxt<'_>,
    def_id: DefId,
) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
    tcx.explicit_item_super_predicates(def_id).map_bound(|bounds| {
        tcx.mk_clauses_from_iter(
            util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)).filter_only_self(),
        )
    })
}

/// This exists as an optimization to compute only the item bounds of the item
/// that are not `Self` bounds.
pub(super) fn item_non_self_assumptions(
    tcx: TyCtxt<'_>,
    def_id: DefId,
) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
    let all_bounds: FxIndexSet<_> = tcx.item_bounds(def_id).skip_binder().iter().collect();
    let own_bounds: FxIndexSet<_> =
        tcx.item_super_predicates(def_id).skip_binder().iter().collect();
    if all_bounds.len() == own_bounds.len() {
        ty::EarlyBinder::bind(ty::ListWithCachedTypeInfo::empty())
    } else {
        ty::EarlyBinder::bind(tcx.mk_clauses_from_iter(all_bounds.difference(&own_bounds).copied()))
    }
}

/// This exists as an optimization to compute only the supertraits of this impl's
/// trait that are outlives bounds.
pub(super) fn impl_super_outlives(
    tcx: TyCtxt<'_>,
    def_id: DefId,
) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
    tcx.impl_trait_header(def_id).expect("expected an impl of trait").trait_ref.map_bound(
        |trait_ref| {
            let clause: ty::Clause<'_> = trait_ref.upcast(tcx);
            tcx.mk_clauses_from_iter(util::elaborate(tcx, [clause]).filter(|clause| {
                matches!(
                    clause.kind().skip_binder(),
                    ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_)
                )
            }))
        },
    )
}

struct AssocTyToOpaque<'tcx> {
    tcx: TyCtxt<'tcx>,
    fn_def_id: DefId,
}

impl<'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTyToOpaque<'tcx> {
    fn cx(&self) -> TyCtxt<'tcx> {
        self.tcx
    }

    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
        if let ty::Alias(ty::Projection, projection_ty) = ty.kind()
            && let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, .. }) =
                self.tcx.opt_rpitit_info(projection_ty.def_id)
            && fn_def_id == self.fn_def_id
        {
            self.tcx.type_of(projection_ty.def_id).instantiate(self.tcx, projection_ty.args)
        } else {
            ty.super_fold_with(self)
        }
    }
}