Skip to content

Commit 298bfd6

Browse files
committed
Update comments and variable names
1 parent 09722f7 commit 298bfd6

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs

+35-43
Original file line numberDiff line numberDiff line change
@@ -99,69 +99,64 @@ pub(super) fn infer_predicates(
9999

100100
fn insert_required_predicates_to_be_wf<'tcx>(
101101
tcx: TyCtxt<'tcx>,
102-
field_ty: Ty<'tcx>,
103-
field_span: Span,
102+
ty: Ty<'tcx>,
103+
span: Span,
104104
global_inferred_outlives: &FxHashMap<DefId, ty::EarlyBinder<RequiredPredicates<'tcx>>>,
105105
required_predicates: &mut RequiredPredicates<'tcx>,
106106
explicit_map: &mut ExplicitPredicatesMap<'tcx>,
107107
) {
108-
for arg in field_ty.walk() {
109-
let ty = match arg.unpack() {
108+
for arg in ty.walk() {
109+
let leaf_ty = match arg.unpack() {
110110
GenericArgKind::Type(ty) => ty,
111111

112112
// No predicates from lifetimes or constants, except potentially
113113
// constants' types, but `walk` will get to them as well.
114114
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => continue,
115115
};
116116

117-
match *ty.kind() {
118-
// The field is of type &'a T which means that we will have
119-
// a predicate requirement of T: 'a (T outlives 'a).
117+
match *leaf_ty.kind() {
118+
// The type is `&'a T` which means that we will have
119+
// a predicate requirement of `T: 'a` (`T` outlives `'a`).
120120
//
121-
// We also want to calculate potential predicates for the T
121+
// We also want to calculate potential predicates for the `T`.
122122
ty::Ref(region, rty, _) => {
123123
debug!("Ref");
124-
insert_outlives_predicate(tcx, rty.into(), region, field_span, required_predicates);
124+
insert_outlives_predicate(tcx, rty.into(), region, span, required_predicates);
125125
}
126126

127-
// For each Adt (struct/enum/union) type `Foo<'a, T>`, we
128-
// can load the current set of inferred and explicit
129-
// predicates from `global_inferred_outlives` and filter the
130-
// ones that are TypeOutlives.
127+
// For each outer type `Outer<'a, T>`, we can load the current set of
128+
// inferred and explicit predicates from `global_inferred_outlives` and
129+
// filter the ones that are `TypeOutlives`.
131130
ty::Adt(def, args) => {
132131
// First check the inferred predicates
133132
//
134-
// Example 1:
133+
// Example:
135134
//
136-
// struct Foo<'a, T> {
137-
// field1: Bar<'a, T>
135+
// struct Outer<'a, T> {
136+
// outer: Inner<'a, T>
138137
// }
139138
//
140-
// struct Bar<'b, U> {
141-
// field2: &'b U
139+
// struct Inner<'b, U> {
140+
// inner: &'b U
142141
// }
143142
//
144-
// Here, when processing the type of `field1`, we would
145-
// request the set of implicit predicates computed for `Bar`
143+
// Here, when processing the type of field `outer`, we would
144+
// request the set of implicit predicates computed for `Inner`
146145
// thus far. This will initially come back empty, but in next
147146
// round we will get `U: 'b`. We then apply the substitution
148147
// `['b => 'a, U => T]` and thus get the requirement that `T:
149-
// 'a` holds for `Foo`.
148+
// 'a` holds for `Outer`.
150149
debug!("Adt");
151-
if let Some(unsubstituted_predicates) = global_inferred_outlives.get(&def.did()) {
152-
for (unsubstituted_predicate, &span) in
153-
unsubstituted_predicates.as_ref().skip_binder()
154-
{
155-
// `unsubstituted_predicate` is `U: 'b` in the
156-
// example above. So apply the substitution to
157-
// get `T: 'a` (or `predicate`):
158-
let predicate = unsubstituted_predicates
159-
.rebind(*unsubstituted_predicate)
160-
.instantiate(tcx, args);
150+
if let Some(predicates) = global_inferred_outlives.get(&def.did()) {
151+
for (predicate, &span) in predicates.as_ref().skip_binder() {
152+
// `predicate` is `U: 'b` in the example above. So apply the
153+
// substitution to get `T: 'a` (or `instantiated_predicate`):
154+
let instantiated_predicate =
155+
predicates.rebind(*predicate).instantiate(tcx, args);
161156
insert_outlives_predicate(
162157
tcx,
163-
predicate.0,
164-
predicate.1,
158+
instantiated_predicate.0,
159+
instantiated_predicate.1,
165160
span,
166161
required_predicates,
167162
);
@@ -170,7 +165,6 @@ fn insert_required_predicates_to_be_wf<'tcx>(
170165

171166
// Check if the type has any explicit predicates that need
172167
// to be added to `required_predicates`
173-
// let _: () = args.region_at(0);
174168
check_explicit_predicates(
175169
tcx,
176170
def.did(),
@@ -184,10 +178,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
184178
ty::Dynamic(obj, ..) => {
185179
// This corresponds to `dyn Trait<..>`. In this case, we should
186180
// use the explicit predicates as well.
187-
188181
debug!("Dynamic");
189-
debug!("field_ty = {}", &field_ty);
190-
debug!("ty in field = {}", &ty);
191182
if let Some(ex_trait_ref) = obj.principal() {
192183
// Here, we are passing the type `usize` as a
193184
// placeholder value with the function
@@ -209,14 +200,15 @@ fn insert_required_predicates_to_be_wf<'tcx>(
209200
}
210201
}
211202

212-
ty::Alias(ty::Projection, obj) => {
213-
// This corresponds to `<T as Foo<'a>>::Bar`. In this case, we should use the
214-
// explicit predicates as well.
203+
ty::Alias(ty::Projection, alias) => {
204+
// This corresponds to a type like `<() as Trait<'a, T>>::Type`.
205+
// We only use the explicit predicates of the trait but
206+
// not the ones of the associated type itself.
215207
debug!("Projection");
216208
check_explicit_predicates(
217209
tcx,
218-
tcx.parent(obj.def_id),
219-
obj.args,
210+
tcx.parent(alias.def_id),
211+
alias.args,
220212
required_predicates,
221213
explicit_map,
222214
None,
@@ -238,7 +230,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
238230
);
239231
}
240232

241-
// FIXME(inherent_associated_types): Handle this case properly.
233+
// FIXME(inherent_associated_types): Use the explicit predicates from the parent impl.
242234
ty::Alias(ty::Inherent, _) => {}
243235

244236
_ => {}

compiler/rustc_hir_analysis/src/outlives/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clau
5151
}
5252

5353
fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> {
54-
// Compute a map from each struct/enum/union S to the **explicit**
55-
// outlives predicates (`T: 'a`, `'a: 'b`) that the user wrote.
54+
// Compute a map from each ADT (struct/enum/union) & lazy type alias to
55+
// the **explicit** outlives predicates (`T: 'a`, `'a: 'b`) that the user wrote.
5656
// Typically there won't be many of these, except in older code where
5757
// they were mandatory. Nonetheless, we have to ensure that every such
5858
// predicate is satisfied, so they form a kind of base set of requirements

0 commit comments

Comments
 (0)