Remove a field that is computed later anyway

This commit is contained in:
Oli Scherer 2021-07-26 17:09:05 +00:00
parent d99805982b
commit 14021feea9
2 changed files with 30 additions and 45 deletions

View file

@ -42,29 +42,6 @@ pub struct OpaqueTypeDecl<'tcx> {
/// lifetime parameter on `foo`.) /// lifetime parameter on `foo`.)
pub concrete_ty: Ty<'tcx>, pub concrete_ty: Ty<'tcx>,
/// Returns `true` if the `impl Trait` bounds include region bounds.
/// For example, this would be true for:
///
/// fn foo<'a, 'b, 'c>() -> impl Trait<'c> + 'a + 'b
///
/// but false for:
///
/// fn foo<'c>() -> impl Trait<'c>
///
/// unless `Trait` was declared like:
///
/// trait Trait<'c>: 'c
///
/// in which case it would be true.
///
/// This is used during regionck to decide whether we need to
/// impose any additional constraints to ensure that region
/// variables in `concrete_ty` wind up being constrained to
/// something from `substs` (or, at minimum, things that outlive
/// the fn body). (Ultimately, writeback is responsible for this
/// check.)
pub has_required_region_bounds: bool,
/// The origin of the opaque type. /// The origin of the opaque type.
pub origin: hir::OpaqueTyOrigin, pub origin: hir::OpaqueTyOrigin,
} }

View file

@ -330,19 +330,36 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let span = tcx.def_span(def_id); let span = tcx.def_span(def_id);
// If there are required region bounds, we can use them. // Check if the `impl Trait` bounds include region bounds.
if opaque_defn.has_required_region_bounds { // For example, this would be true for:
let bounds = tcx.explicit_item_bounds(def_id); //
debug!("{:#?}", bounds); // fn foo<'a, 'b, 'c>() -> impl Trait<'c> + 'a + 'b
let bounds: Vec<_> = //
bounds.iter().map(|(bound, _)| bound.subst(tcx, opaque_type_key.substs)).collect(); // but false for:
debug!("{:#?}", bounds); //
let opaque_type = tcx.mk_opaque(def_id, opaque_type_key.substs); // fn foo<'c>() -> impl Trait<'c>
//
let required_region_bounds = // unless `Trait` was declared like:
required_region_bounds(tcx, opaque_type, bounds.into_iter()); //
debug_assert!(!required_region_bounds.is_empty()); // trait Trait<'c>: 'c
//
// in which case it would be true.
//
// This is used during regionck to decide whether we need to
// impose any additional constraints to ensure that region
// variables in `concrete_ty` wind up being constrained to
// something from `substs` (or, at minimum, things that outlive
// the fn body). (Ultimately, writeback is responsible for this
// check.)
let bounds = tcx.explicit_item_bounds(def_id);
debug!("{:#?}", bounds);
let bounds: Vec<_> =
bounds.iter().map(|(bound, _)| bound.subst(tcx, opaque_type_key.substs)).collect();
debug!("{:#?}", bounds);
let opaque_type = tcx.mk_opaque(def_id, opaque_type_key.substs);
let required_region_bounds = required_region_bounds(tcx, opaque_type, bounds.into_iter());
if !required_region_bounds.is_empty() {
for required_region in required_region_bounds { for required_region in required_region_bounds {
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor { concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
op: |r| self.sub_regions(infer::CallReturn(span), required_region, r), op: |r| self.sub_regions(infer::CallReturn(span), required_region, r),
@ -979,9 +996,6 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
debug!("instantiate_opaque_types: bounds={:?}", bounds); debug!("instantiate_opaque_types: bounds={:?}", bounds);
let required_region_bounds = required_region_bounds(tcx, ty, bounds.iter().copied());
debug!("instantiate_opaque_types: required_region_bounds={:?}", required_region_bounds);
// Make sure that we are in fact defining the *entire* type // Make sure that we are in fact defining the *entire* type
// (e.g., `type Foo<T: Bound> = impl Bar;` needs to be // (e.g., `type Foo<T: Bound> = impl Bar;` needs to be
// defined by a function like `fn foo<T: Bound>() -> Foo<T>`). // defined by a function like `fn foo<T: Bound>() -> Foo<T>`).
@ -997,13 +1011,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
self.opaque_types.insert( self.opaque_types.insert(
OpaqueTypeKey { def_id, substs }, OpaqueTypeKey { def_id, substs },
OpaqueTypeDecl { OpaqueTypeDecl { opaque_type: ty, definition_span, concrete_ty: ty_var, origin },
opaque_type: ty,
definition_span,
concrete_ty: ty_var,
has_required_region_bounds: !required_region_bounds.is_empty(),
origin,
},
); );
debug!("instantiate_opaque_types: ty_var={:?}", ty_var); debug!("instantiate_opaque_types: ty_var={:?}", ty_var);