@@ -5,7 +5,7 @@ import syntax::print::pprust::{expr_to_str};
5
5
// Extracts the bound regions from bound_tys and then replaces those same
6
6
// regions in `sty` with fresh region variables, returning the resulting type.
7
7
// Does not descend into fn types. This is used when deciding whether an impl
8
- // applies at a given call site. See also universally_quantify_before_call().
8
+ // applies at a given call site.
9
9
fn universally_quantify_from_sty ( fcx : @fn_ctxt ,
10
10
span : span ,
11
11
bound_tys : [ ty:: t ] ,
@@ -31,6 +31,11 @@ fn universally_quantify_from_sty(fcx: @fn_ctxt,
31
31
}
32
32
}
33
33
34
+ // Takes `isr`, a mapping from in-scope region names ("isr"s) to their
35
+ // corresponding regions (possibly produced by a call to
36
+ // collect_bound_regions_in_tys; and `ty`, a type. Returns an updated
37
+ // version of `ty`, in which bound regions in `ty` have been replaced
38
+ // with the corresponding bindings in `isr`.
34
39
fn replace_bound_regions (
35
40
tcx : ty:: ctxt ,
36
41
span : span ,
@@ -140,23 +145,29 @@ fn region_of(fcx: @fn_ctxt, expr: @ast::expr) -> ty::region {
140
145
}
141
146
}
142
147
148
+ // Takes `isr`, a (possibly empty) mapping from in-scope region names ("isr"s)
149
+ // to their corresponding regions; `tys`, a list of types, and `to_r`, a
150
+ // closure that takes a bound_region and returns a region. Returns an updated
151
+ // version of `isr`, extended with the in-scope region names from all of the
152
+ // bound regions appearing in the types in the `tys` list (if they're not in
153
+ // `isr` already), with each of those in-scope region names mapped to a region
154
+ // that's the result of applying `to_r` to itself.
155
+
156
+ // "collect" is something of a misnomer -- we're not merely collecting
157
+ // a list of the bound regions, but also doing the work of applying
158
+ // `to_r` to them!
143
159
fn collect_bound_regions_in_tys (
144
160
tcx : ty:: ctxt ,
145
161
isr : isr_alist ,
146
162
tys : [ ty:: t ] ,
147
163
to_r : fn ( ty:: bound_region ) -> ty:: region ) -> isr_alist {
148
164
149
- tys. foldl ( isr) { |isr, t|
150
- collect_bound_regions_in_ty ( tcx, isr, t, to_r)
151
- }
152
- }
153
-
154
- fn collect_bound_regions_in_ty (
155
- tcx : ty:: ctxt ,
156
- isr : isr_alist ,
157
- ty : ty:: t ,
158
- to_r : fn ( ty:: bound_region ) -> ty:: region ) -> isr_alist {
159
-
165
+ // Takes `isr` (described above), `to_r` (described above), and `r`, a
166
+ // region. If `r` is anything other than a bound region, or if it's a
167
+ // bound region that already appears in `isr`, then we return `isr`
168
+ // unchanged. If `r` is a bound region that doesn't already appear in
169
+ // `isr`, we return an updated isr_alist that now contains a mapping from
170
+ // `r` to the result of calling `to_r` on it.
160
171
fn append_isr ( isr : isr_alist ,
161
172
to_r : fn ( ty:: bound_region ) -> ty:: region ,
162
173
r : ty:: region ) -> isr_alist {
@@ -174,16 +185,30 @@ fn collect_bound_regions_in_ty(
174
185
}
175
186
}
176
187
177
- let mut isr = isr;
188
+ // For each region in `t`, apply the `append_isr` function to that
189
+ // region, accumulating and returning the results in an isr_alist.
190
+ fn fold_over_regions_in_type (
191
+ tcx : ty:: ctxt ,
192
+ isr : isr_alist ,
193
+ ty : ty:: t ,
194
+ to_r : fn ( ty:: bound_region ) -> ty:: region ) -> isr_alist {
195
+
196
+ let mut isr = isr;
197
+
198
+ // Using fold_regions is inefficient, because it constructs new types,
199
+ // but it avoids code duplication in terms of locating all the regions
200
+ // within the various kinds of types. This had already caused me
201
+ // several bugs so I decided to switch over.
202
+ ty:: fold_regions ( tcx, ty) { |r, in_fn|
203
+ if !in_fn { isr = append_isr ( isr, to_r, r) ; }
204
+ r
205
+ } ;
178
206
179
- // Using fold_regions is inefficient, because it constructs new types, but
180
- // it avoids code duplication in terms of locating all the regions within
181
- // the various kinds of types. This had already caused me several bugs
182
- // so I decided to switch over.
183
- ty:: fold_regions ( tcx, ty) { |r, in_fn|
184
- if !in_fn { isr = append_isr ( isr, to_r, r) ; }
185
- r
186
- } ;
207
+ ret isr;
208
+ }
187
209
188
- ret isr;
210
+ // For each type `t` in `tys`...
211
+ tys. foldl ( isr) { |isr, t|
212
+ fold_over_regions_in_type ( tcx, isr, t, to_r)
213
+ }
189
214
}
0 commit comments