Skip to content

Commit 7ded11a

Browse files
committed
Auto merge of #29463 - jseyfried:master, r=nikomatsakis
Remove implicit binder from `FnSpace` in `VecPerParamSpace` (fixes #20526) This removes the implicit binder from `FnSpace` in `VecPerParamSpace` so that `Binder<T>` is the only region binder (as described in issue #20526), and refactors away `enter_region_binder` and `exit_region_binder` from `TypeFolder`.
2 parents 7fd331e + 588e0f9 commit 7ded11a

File tree

3 files changed

+23
-68
lines changed

3 files changed

+23
-68
lines changed

src/librustc/ty/fold.rs

+12-26
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,9 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
116116
pub trait TypeFolder<'tcx> : Sized {
117117
fn tcx<'a>(&'a self) -> &'a TyCtxt<'tcx>;
118118

119-
/// Invoked by the `super_*` routines when we enter a region
120-
/// binding level (for example, when entering a function
121-
/// signature). This is used by clients that want to track the
122-
/// Debruijn index nesting level.
123-
fn enter_region_binder(&mut self) { }
124-
125-
/// Invoked by the `super_*` routines when we exit a region
126-
/// binding level. This is used by clients that want to
127-
/// track the Debruijn index nesting level.
128-
fn exit_region_binder(&mut self) { }
129-
130119
fn fold_binder<T>(&mut self, t: &Binder<T>) -> Binder<T>
131120
where T : TypeFoldable<'tcx>
132121
{
133-
// FIXME(#20526) this should replace `enter_region_binder`/`exit_region_binder`.
134122
t.super_fold_with(self)
135123
}
136124

@@ -197,8 +185,9 @@ pub trait TypeFolder<'tcx> : Sized {
197185
}
198186

199187
pub trait TypeVisitor<'tcx> : Sized {
200-
fn enter_region_binder(&mut self) { }
201-
fn exit_region_binder(&mut self) { }
188+
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
189+
t.super_visit_with(self)
190+
}
202191

203192
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
204193
t.super_visit_with(self)
@@ -296,12 +285,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx>
296285
{
297286
fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
298287

299-
fn enter_region_binder(&mut self) {
288+
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
300289
self.current_depth += 1;
301-
}
302-
303-
fn exit_region_binder(&mut self) {
290+
let t = t.super_fold_with(self);
304291
self.current_depth -= 1;
292+
t
305293
}
306294

307295
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
@@ -438,12 +426,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx>
438426
{
439427
fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
440428

441-
fn enter_region_binder(&mut self) {
429+
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
442430
self.current_depth += 1;
443-
}
444-
445-
fn exit_region_binder(&mut self) {
431+
let t = t.super_fold_with(self);
446432
self.current_depth -= 1;
433+
t
447434
}
448435

449436
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
@@ -596,12 +583,11 @@ struct HasEscapingRegionsVisitor {
596583
}
597584

598585
impl<'tcx> TypeVisitor<'tcx> for HasEscapingRegionsVisitor {
599-
fn enter_region_binder(&mut self) {
586+
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
600587
self.depth += 1;
601-
}
602-
603-
fn exit_region_binder(&mut self) {
588+
let result = t.super_visit_with(self);
604589
self.depth -= 1;
590+
result
605591
}
606592

607593
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {

src/librustc/ty/structural_impls.rs

+8-38
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,19 @@ impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
190190

191191
impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
192192
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
193-
folder.enter_region_binder();
194-
let result = ty::Binder(self.0.fold_with(folder));
195-
folder.exit_region_binder();
196-
result
193+
ty::Binder(self.0.fold_with(folder))
197194
}
198195

199196
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
200197
folder.fold_binder(self)
201198
}
202199

203200
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
204-
visitor.enter_region_binder();
205-
if self.0.visit_with(visitor) { return true }
206-
visitor.exit_region_binder();
207-
false
201+
self.0.visit_with(visitor)
202+
}
203+
204+
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
205+
visitor.visit_binder(self)
208206
}
209207
}
210208

@@ -220,39 +218,11 @@ impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for P<[T]> {
220218

221219
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for VecPerParamSpace<T> {
222220
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
223-
224-
// Things in the Fn space take place under an additional level
225-
// of region binding relative to the other spaces. This is
226-
// because those entries are attached to a method, and methods
227-
// always introduce a level of region binding.
228-
229-
let result = self.map_enumerated(|(space, index, elem)| {
230-
if space == subst::FnSpace && index == 0 {
231-
// enter new level when/if we reach the first thing in fn space
232-
folder.enter_region_binder();
233-
}
234-
elem.fold_with(folder)
235-
});
236-
if result.len(subst::FnSpace) > 0 {
237-
// if there was anything in fn space, exit the region binding level
238-
folder.exit_region_binder();
239-
}
240-
result
221+
self.map(|elem| elem.fold_with(folder))
241222
}
242223

243224
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
244-
let mut entered_region_binder = false;
245-
let result = self.iter_enumerated().any(|(space, index, t)| {
246-
if space == subst::FnSpace && index == 0 {
247-
visitor.enter_region_binder();
248-
entered_region_binder = true;
249-
}
250-
t.visit_with(visitor)
251-
});
252-
if entered_region_binder {
253-
visitor.exit_region_binder();
254-
}
255-
result
225+
self.iter().any(|elem| elem.visit_with(visitor))
256226
}
257227
}
258228

src/librustc/ty/subst.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,11 @@ struct SubstFolder<'a, 'tcx: 'a> {
582582
impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
583583
fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
584584

585-
fn enter_region_binder(&mut self) {
585+
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
586586
self.region_binders_passed += 1;
587-
}
588-
589-
fn exit_region_binder(&mut self) {
587+
let t = t.super_fold_with(self);
590588
self.region_binders_passed -= 1;
589+
t
591590
}
592591

593592
fn fold_region(&mut self, r: ty::Region) -> ty::Region {

0 commit comments

Comments
 (0)