Skip to content

Commit 69ef8fb

Browse files
committed
Auto merge of #60765 - matthewjasper:fix-more-escaping-rescopes, r=oli-obk
Fix more escaping ReScopes Closes #58840
2 parents fe5f42c + 9a4f0ab commit 69ef8fb

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

src/librustc/infer/opaque_types/mod.rs

+46-10
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,40 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
284284
debug!("constrain_opaque_type: def_id={:?}", def_id);
285285
debug!("constrain_opaque_type: opaque_defn={:#?}", opaque_defn);
286286

287+
let tcx = self.tcx;
288+
287289
let concrete_ty = self.resolve_type_vars_if_possible(&opaque_defn.concrete_ty);
288290

289291
debug!("constrain_opaque_type: concrete_ty={:?}", concrete_ty);
290292

291-
let abstract_type_generics = self.tcx.generics_of(def_id);
293+
let abstract_type_generics = tcx.generics_of(def_id);
292294

293-
let span = self.tcx.def_span(def_id);
295+
let span = tcx.def_span(def_id);
294296

295-
// If there are required region bounds, we can just skip
296-
// ahead. There will already be a registered region
297-
// obligation related `concrete_ty` to those regions.
297+
// If there are required region bounds, we can use them.
298298
if opaque_defn.has_required_region_bounds {
299+
let predicates_of = tcx.predicates_of(def_id);
300+
debug!(
301+
"constrain_opaque_type: predicates: {:#?}",
302+
predicates_of,
303+
);
304+
let bounds = predicates_of.instantiate(tcx, opaque_defn.substs);
305+
debug!("constrain_opaque_type: bounds={:#?}", bounds);
306+
let opaque_type = tcx.mk_opaque(def_id, opaque_defn.substs);
307+
308+
let required_region_bounds = tcx.required_region_bounds(
309+
opaque_type,
310+
bounds.predicates.clone(),
311+
);
312+
debug_assert!(!required_region_bounds.is_empty());
313+
314+
for region in required_region_bounds {
315+
concrete_ty.visit_with(&mut OpaqueTypeOutlivesVisitor {
316+
infcx: self,
317+
least_region: region,
318+
span,
319+
});
320+
}
299321
return;
300322
}
301323

@@ -371,7 +393,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
371393
}
372394
}
373395

374-
let least_region = least_region.unwrap_or(self.tcx.lifetimes.re_static);
396+
let least_region = least_region.unwrap_or(tcx.lifetimes.re_static);
375397
debug!("constrain_opaque_types: least_region={:?}", least_region);
376398

377399
concrete_ty.visit_with(&mut OpaqueTypeOutlivesVisitor {
@@ -589,10 +611,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for ReverseMapper<'cx, 'gcx, 'tcx>
589611
ty::ReLateBound(..) |
590612

591613
// ignore `'static`, as that can appear anywhere
592-
ty::ReStatic |
593-
594-
// ignore `ReScope`, which may appear in impl Trait in bindings.
595-
ty::ReScope(..) => return r,
614+
ty::ReStatic => return r,
596615

597616
_ => { }
598617
}
@@ -683,6 +702,23 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for ReverseMapper<'cx, 'gcx, 'tcx>
683702
self.tcx.mk_closure(def_id, ty::ClosureSubsts { substs })
684703
}
685704

705+
ty::Generator(def_id, substs, movability) => {
706+
let generics = self.tcx.generics_of(def_id);
707+
let substs = self.tcx.mk_substs(substs.substs.iter().enumerate().map(
708+
|(index, &kind)| {
709+
if index < generics.parent_count {
710+
// Accommodate missing regions in the parent kinds...
711+
self.fold_kind_mapping_missing_regions_to_empty(kind)
712+
} else {
713+
// ...but not elsewhere.
714+
self.fold_kind_normally(kind)
715+
}
716+
},
717+
));
718+
719+
self.tcx.mk_generator(def_id, ty::GeneratorSubsts { substs }, movability)
720+
}
721+
686722
_ => ty.super_fold_with(self),
687723
}
688724
}

src/test/run-pass/impl-trait/lifetimes.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-pass
22

33
#![allow(warnings)]
4+
#![feature(generators)]
45

56
use std::fmt::Debug;
67

@@ -112,6 +113,11 @@ impl<'unnecessary_lifetime> MyVec {
112113
fn iter_doesnt_capture_unnecessary_lifetime<'s>(&'s self) -> impl Iterator<Item = &'s u8> {
113114
self.0.iter().flat_map(|inner_vec| inner_vec.iter())
114115
}
116+
117+
fn generator_doesnt_capture_unnecessary_lifetime<'s: 's>() -> impl Sized {
118+
|| yield
119+
}
115120
}
116121

122+
117123
fn main() {}

src/test/ui/impl-trait/can-return-unconstrained-closure.rs

+4
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ fn make_identity() -> impl Sized {
1616
|x: &'static i32| x
1717
}
1818

19+
fn make_identity_static() -> impl Sized + 'static {
20+
|x: &'static i32| x
21+
}
22+
1923
fn main() {}

src/test/ui/impl-trait/issue-57464-unexpected-regions.rs

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ fn wrapped_closure() -> impl Sized {
1717
A(f)
1818
}
1919

20+
fn wrapped_closure_with_bound() -> impl Sized + 'static {
21+
let f = |x| x;
22+
f(&0);
23+
A(f)
24+
}
25+
2026
fn main() {
2127
let x: Box<dyn Send> = Box::new(wrapped_closure());
28+
let y: Box<dyn Send> = Box::new(wrapped_closure_with_bound());
2229
}

0 commit comments

Comments
 (0)