1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use hir::def_id::DefId;
14 use infer::{self, InferCtxt, InferOk, TypeVariableOrigin};
15 use infer::outlives::free_region_map::FreeRegionRelations;
16 use rustc_data_structures::fx::FxHashMap;
18 use traits::{self, PredicateObligation};
19 use ty::{self, Ty, TyCtxt, GenericParamDefKind};
20 use ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder};
21 use ty::outlives::Component;
22 use ty::subst::{Kind, Substs, UnpackedKind};
23 use util::nodemap::DefIdMap;
25 pub type OpaqueTypeMap<'tcx> = DefIdMap<OpaqueTypeDecl<'tcx>>;
27 /// Information about the opaque, abstract types whose values we
28 /// are inferring in this function (these are the `impl Trait` that
29 /// appear in the return type).
30 #[derive(Copy, Clone, Debug)]
31 pub struct OpaqueTypeDecl<'tcx> {
32 /// The substitutions that we apply to the abstract that that this
33 /// `impl Trait` desugars to. e.g., if:
35 /// fn foo<'a, 'b, T>() -> impl Trait<'a>
37 /// winds up desugared to:
39 /// abstract type Foo<'x, T>: Trait<'x>
40 /// fn foo<'a, 'b, T>() -> Foo<'a, T>
42 /// then `substs` would be `['a, T]`.
43 pub substs: &'tcx Substs<'tcx>,
45 /// The type variable that represents the value of the abstract type
46 /// that we require. In other words, after we compile this function,
47 /// we will be created a constraint like:
51 /// where `?C` is the value of this type variable. =) It may
52 /// naturally refer to the type and lifetime parameters in scope
53 /// in this function, though ultimately it should only reference
54 /// those that are arguments to `Foo` in the constraint above. (In
55 /// other words, `?C` should not include `'b`, even though it's a
56 /// lifetime parameter on `foo`.)
57 pub concrete_ty: Ty<'tcx>,
59 /// True if the `impl Trait` bounds include region bounds.
60 /// For example, this would be true for:
62 /// fn foo<'a, 'b, 'c>() -> impl Trait<'c> + 'a + 'b
66 /// fn foo<'c>() -> impl Trait<'c>
68 /// unless `Trait` was declared like:
70 /// trait Trait<'c>: 'c
72 /// in which case it would be true.
74 /// This is used during regionck to decide whether we need to
75 /// impose any additional constraints to ensure that region
76 /// variables in `concrete_ty` wind up being constrained to
77 /// something from `substs` (or, at minimum, things that outlive
78 /// the fn body). (Ultimately, writeback is responsible for this
80 pub has_required_region_bounds: bool,
83 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
84 /// Replace all opaque types in `value` with fresh inference variables
85 /// and creates appropriate obligations. For example, given the input:
87 /// impl Iterator<Item = impl Debug>
89 /// this method would create two type variables, `?0` and `?1`. It would
90 /// return the type `?0` but also the obligations:
92 /// ?0: Iterator<Item = ?1>
95 /// Moreover, it returns a `OpaqueTypeMap` that would map `?0` to
96 /// info about the `impl Iterator<..>` type and `?1` to info about
97 /// the `impl Debug` type.
101 /// - `parent_def_id` -- the def-id of the function in which the opaque type
103 /// - `body_id` -- the body-id with which the resulting obligations should
105 /// - `param_env` -- the in-scope parameter environment to be used for
107 /// - `value` -- the value within which we are instantiating opaque types
108 pub fn instantiate_opaque_types<T: TypeFoldable<'tcx>>(
110 parent_def_id: DefId,
111 body_id: ast::NodeId,
112 param_env: ty::ParamEnv<'tcx>,
114 ) -> InferOk<'tcx, (T, OpaqueTypeMap<'tcx>)> {
115 debug!("instantiate_opaque_types(value={:?}, parent_def_id={:?}, body_id={:?}, \
117 value, parent_def_id, body_id, param_env,
119 let mut instantiator = Instantiator {
124 opaque_types: Default::default(),
127 let value = instantiator.instantiate_opaque_types_in_map(value);
129 value: (value, instantiator.opaque_types),
130 obligations: instantiator.obligations,
134 /// Given the map `opaque_types` containing the existential `impl
135 /// Trait` types whose underlying, hidden types are being
136 /// inferred, this method adds constraints to the regions
137 /// appearing in those underlying hidden types to ensure that they
138 /// at least do not refer to random scopes within the current
139 /// function. These constraints are not (quite) sufficient to
140 /// guarantee that the regions are actually legal values; that
141 /// final condition is imposed after region inference is done.
145 /// Let's work through an example to explain how it works. Assume
146 /// the current function is as follows:
149 /// fn foo<'a, 'b>(..) -> (impl Bar<'a>, impl Bar<'b>)
152 /// Here, we have two `impl Trait` types whose values are being
153 /// inferred (the `impl Bar<'a>` and the `impl
154 /// Bar<'b>`). Conceptually, this is sugar for a setup where we
155 /// define underlying abstract types (`Foo1`, `Foo2`) and then, in
156 /// the return type of `foo`, we *reference* those definitions:
159 /// abstract type Foo1<'x>: Bar<'x>;
160 /// abstract type Foo2<'x>: Bar<'x>;
161 /// fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
168 /// As indicating in the comments above, each of those references
169 /// is (in the compiler) basically a substitution (`substs`)
170 /// applied to the type of a suitable `def_id` (which identifies
171 /// `Foo1` or `Foo2`).
173 /// Now, at this point in compilation, what we have done is to
174 /// replace each of the references (`Foo1<'a>`, `Foo2<'b>`) with
175 /// fresh inference variables C1 and C2. We wish to use the values
176 /// of these variables to infer the underlying types of `Foo1` and
177 /// `Foo2`. That is, this gives rise to higher-order (pattern) unification
178 /// constraints like:
181 /// for<'a> (Foo1<'a> = C1)
182 /// for<'b> (Foo1<'b> = C2)
185 /// For these equation to be satisfiable, the types `C1` and `C2`
186 /// can only refer to a limited set of regions. For example, `C1`
187 /// can only refer to `'static` and `'a`, and `C2` can only refer
188 /// to `'static` and `'b`. The job of this function is to impose that
191 /// Up to this point, C1 and C2 are basically just random type
192 /// inference variables, and hence they may contain arbitrary
193 /// regions. In fact, it is fairly likely that they do! Consider
194 /// this possible definition of `foo`:
197 /// fn foo<'a, 'b>(x: &'a i32, y: &'b i32) -> (impl Bar<'a>, impl Bar<'b>) {
202 /// Here, the values for the concrete types of the two impl
203 /// traits will include inference variables:
210 /// Ordinarily, the subtyping rules would ensure that these are
211 /// sufficiently large. But since `impl Bar<'a>` isn't a specific
212 /// type per se, we don't get such constraints by default. This
213 /// is where this function comes into play. It adds extra
214 /// constraints to ensure that all the regions which appear in the
215 /// inferred type are regions that could validly appear.
217 /// This is actually a bit of a tricky constraint in general. We
218 /// want to say that each variable (e.g., `'0`) can only take on
219 /// values that were supplied as arguments to the abstract type
220 /// (e.g., `'a` for `Foo1<'a>`) or `'static`, which is always in
221 /// scope. We don't have a constraint quite of this kind in the current
226 /// We make use of the constraint that we *do* have in the `<=`
227 /// relation. To do that, we find the "minimum" of all the
228 /// arguments that appear in the substs: that is, some region
229 /// which is less than all the others. In the case of `Foo1<'a>`,
230 /// that would be `'a` (it's the only choice, after all). Then we
231 /// apply that as a least bound to the variables (e.g., `'a <=
234 /// In some cases, there is no minimum. Consider this example:
237 /// fn baz<'a, 'b>() -> impl Trait<'a, 'b> { ... }
240 /// Here we would report an error, because `'a` and `'b` have no
241 /// relation to one another.
243 /// # The `free_region_relations` parameter
245 /// The `free_region_relations` argument is used to find the
246 /// "minimum" of the regions supplied to a given abstract type.
247 /// It must be a relation that can answer whether `'a <= 'b`,
248 /// where `'a` and `'b` are regions that appear in the "substs"
249 /// for the abstract type references (the `<'a>` in `Foo1<'a>`).
251 /// Note that we do not impose the constraints based on the
252 /// generic regions from the `Foo1` definition (e.g., `'x`). This
253 /// is because the constraints we are imposing here is basically
254 /// the concern of the one generating the constraining type C1,
255 /// which is the current function. It also means that we can
256 /// take "implied bounds" into account in some cases:
259 /// trait SomeTrait<'a, 'b> { }
260 /// fn foo<'a, 'b>(_: &'a &'b u32) -> impl SomeTrait<'a, 'b> { .. }
263 /// Here, the fact that `'b: 'a` is known only because of the
264 /// implied bounds from the `&'a &'b u32` parameter, and is not
265 /// "inherent" to the abstract type definition.
269 /// - `opaque_types` -- the map produced by `instantiate_opaque_types`
270 /// - `free_region_relations` -- something that can be used to relate
271 /// the free regions (`'a`) that appear in the impl trait.
272 pub fn constrain_opaque_types<FRR: FreeRegionRelations<'tcx>>(
274 opaque_types: &OpaqueTypeMap<'tcx>,
275 free_region_relations: &FRR,
277 debug!("constrain_opaque_types()");
279 for (&def_id, opaque_defn) in opaque_types {
280 self.constrain_opaque_type(def_id, opaque_defn, free_region_relations);
284 pub fn constrain_opaque_type<FRR: FreeRegionRelations<'tcx>>(
287 opaque_defn: &OpaqueTypeDecl<'tcx>,
288 free_region_relations: &FRR,
290 debug!("constrain_opaque_type()");
291 debug!("constrain_opaque_type: def_id={:?}", def_id);
292 debug!("constrain_opaque_type: opaque_defn={:#?}", opaque_defn);
294 let concrete_ty = self.resolve_type_vars_if_possible(&opaque_defn.concrete_ty);
296 debug!("constrain_opaque_type: concrete_ty={:?}", concrete_ty);
298 let abstract_type_generics = self.tcx.generics_of(def_id);
300 let span = self.tcx.def_span(def_id);
302 // If there are required region bounds, we can just skip
303 // ahead. There will already be a registered region
304 // obligation related `concrete_ty` to those regions.
305 if opaque_defn.has_required_region_bounds {
309 // There were no `required_region_bounds`,
310 // so we have to search for a `least_region`.
311 // Go through all the regions used as arguments to the
312 // abstract type. These are the parameters to the abstract
313 // type; so in our example above, `substs` would contain
314 // `['a]` for the first impl trait and `'b` for the
316 let mut least_region = None;
317 for param in &abstract_type_generics.params {
319 GenericParamDefKind::Lifetime => {}
322 // Get the value supplied for this region from the substs.
323 let subst_arg = opaque_defn.substs.region_at(param.index as usize);
325 // Compute the least upper bound of it with the other regions.
326 debug!("constrain_opaque_types: least_region={:?}", least_region);
327 debug!("constrain_opaque_types: subst_arg={:?}", subst_arg);
329 None => least_region = Some(subst_arg),
331 if free_region_relations.sub_free_regions(lr, subst_arg) {
332 // keep the current least region
333 } else if free_region_relations.sub_free_regions(subst_arg, lr) {
334 // switch to `subst_arg`
335 least_region = Some(subst_arg);
337 // There are two regions (`lr` and
338 // `subst_arg`) which are not relatable. We can't
339 // find a best choice.
342 .struct_span_err(span, "ambiguous lifetime bound in `impl Trait`")
345 format!("neither `{}` nor `{}` outlives the other", lr, subst_arg),
349 least_region = Some(self.tcx.mk_region(ty::ReEmpty));
356 let least_region = least_region.unwrap_or(self.tcx.types.re_static);
357 debug!("constrain_opaque_types: least_region={:?}", least_region);
359 // Require that the type `concrete_ty` outlives
360 // `least_region`, modulo any type parameters that appear
361 // in the type, which we ignore. This is because impl
362 // trait values are assumed to capture all the in-scope
363 // type parameters. This little loop here just invokes
364 // `outlives` repeatedly, draining all the nested
365 // obligations that result.
366 let mut types = vec![concrete_ty];
367 let bound_region = |r| self.sub_regions(infer::CallReturn(span), least_region, r);
368 while let Some(ty) = types.pop() {
369 let mut components = smallvec![];
370 self.tcx.push_outlives_components(ty, &mut components);
371 while let Some(component) = components.pop() {
373 Component::Region(r) => {
377 Component::Param(_) => {
378 // ignore type parameters like `T`, they are captured
379 // implicitly by the `impl Trait`
382 Component::UnresolvedInferenceVariable(_) => {
383 // we should get an error that more type
384 // annotations are needed in this case
387 .delay_span_bug(span, "unresolved inf var in opaque");
390 Component::Projection(ty::ProjectionTy {
394 for r in substs.regions() {
397 types.extend(substs.types());
400 Component::EscapingProjection(more_components) => {
401 components.extend(more_components);
408 /// Given the fully resolved, instantiated type for an opaque
409 /// type, i.e., the value of an inference variable like C1 or C2
410 /// (*), computes the "definition type" for an abstract type
411 /// definition -- that is, the inferred value of `Foo1<'x>` or
412 /// `Foo2<'x>` that we would conceptually use in its definition:
414 /// abstract type Foo1<'x>: Bar<'x> = AAA; <-- this type AAA
415 /// abstract type Foo2<'x>: Bar<'x> = BBB; <-- or this type BBB
416 /// fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
418 /// Note that these values are defined in terms of a distinct set of
419 /// generic parameters (`'x` instead of `'a`) from C1 or C2. The main
420 /// purpose of this function is to do that translation.
422 /// (*) C1 and C2 were introduced in the comments on
423 /// `constrain_opaque_types`. Read that comment for more context.
427 /// - `def_id`, the `impl Trait` type
428 /// - `opaque_defn`, the opaque definition created in `instantiate_opaque_types`
429 /// - `instantiated_ty`, the inferred type C1 -- fully resolved, lifted version of
430 /// `opaque_defn.concrete_ty`
431 pub fn infer_opaque_definition_from_instantiation(
434 opaque_defn: &OpaqueTypeDecl<'tcx>,
435 instantiated_ty: Ty<'gcx>,
438 "infer_opaque_definition_from_instantiation(def_id={:?}, instantiated_ty={:?})",
439 def_id, instantiated_ty
442 let gcx = self.tcx.global_tcx();
444 // Use substs to build up a reverse map from regions to their
445 // identity mappings. This is necessary because of `impl
446 // Trait` lifetimes are computed by replacing existing
447 // lifetimes with 'static and remapping only those used in the
448 // `impl Trait` return type, resulting in the parameters
450 let id_substs = Substs::identity_for_item(gcx, def_id);
451 let map: FxHashMap<Kind<'tcx>, Kind<'gcx>> = opaque_defn
455 .map(|(index, subst)| (*subst, id_substs[index]))
458 // Convert the type from the function into a type valid outside
459 // the function, by replacing invalid regions with 'static,
460 // after producing an error for each of them.
462 instantiated_ty.fold_with(&mut ReverseMapper::new(
464 self.is_tainted_by_errors(),
470 "infer_opaque_definition_from_instantiation: definition_ty={:?}",
474 // We can unwrap here because our reverse mapper always
475 // produces things with 'gcx lifetime, though the type folder
477 let definition_ty = gcx.lift(&definition_ty).unwrap();
483 struct ReverseMapper<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
484 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
486 /// If errors have already been reported in this fn, we suppress
487 /// our own errors because they are sometimes derivative.
488 tainted_by_errors: bool,
490 opaque_type_def_id: DefId,
491 map: FxHashMap<Kind<'tcx>, Kind<'gcx>>,
492 map_missing_regions_to_empty: bool,
494 /// initially `Some`, set to `None` once error has been reported
495 hidden_ty: Option<Ty<'tcx>>,
498 impl<'cx, 'gcx, 'tcx> ReverseMapper<'cx, 'gcx, 'tcx> {
500 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
501 tainted_by_errors: bool,
502 opaque_type_def_id: DefId,
503 map: FxHashMap<Kind<'tcx>, Kind<'gcx>>,
511 map_missing_regions_to_empty: false,
512 hidden_ty: Some(hidden_ty),
516 fn fold_kind_mapping_missing_regions_to_empty(&mut self, kind: Kind<'tcx>) -> Kind<'tcx> {
517 assert!(!self.map_missing_regions_to_empty);
518 self.map_missing_regions_to_empty = true;
519 let kind = kind.fold_with(self);
520 self.map_missing_regions_to_empty = false;
524 fn fold_kind_normally(&mut self, kind: Kind<'tcx>) -> Kind<'tcx> {
525 assert!(!self.map_missing_regions_to_empty);
530 impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for ReverseMapper<'cx, 'gcx, 'tcx> {
531 fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
535 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
537 // ignore bound regions that appear in the type (e.g., this
538 // would ignore `'r` in a type like `for<'r> fn(&'r u32)`.
539 ty::ReLateBound(..) |
541 // ignore `'static`, as that can appear anywhere
544 // ignore `ReScope`, as that can appear anywhere
545 // See `src/test/run-pass/issue-49556.rs` for example.
546 ty::ReScope(..) => return r,
551 match self.map.get(&r.into()).map(|k| k.unpack()) {
552 Some(UnpackedKind::Lifetime(r1)) => r1,
553 Some(u) => panic!("region mapped to unexpected kind: {:?}", u),
555 if !self.map_missing_regions_to_empty && !self.tainted_by_errors {
556 if let Some(hidden_ty) = self.hidden_ty.take() {
557 let span = self.tcx.def_span(self.opaque_type_def_id);
558 let mut err = struct_span_err!(
562 "hidden type for `impl Trait` captures lifetime that \
563 does not appear in bounds",
566 // Assuming regionck succeeded, then we must
567 // be capturing *some* region from the fn
568 // header, and hence it must be free, so it's
569 // ok to invoke this fn (which doesn't accept
570 // all regions, and would ICE if an
571 // inappropriate region is given). We check
572 // `is_tainted_by_errors` by errors above, so
573 // we don't get in here unless regionck
574 // succeeded. (Note also that if regionck
575 // failed, then the regions we are attempting
576 // to map here may well be giving errors
577 // *because* the constraints were not
579 self.tcx.note_and_explain_free_region(
581 &format!("hidden type `{}` captures ", hidden_ty),
589 self.tcx.types.re_empty
594 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
596 ty::Closure(def_id, substs) => {
597 // I am a horrible monster and I pray for death. When
598 // we encounter a closure here, it is always a closure
599 // from within the function that we are currently
600 // type-checking -- one that is now being encapsulated
601 // in an existential abstract type. Ideally, we would
602 // go through the types/lifetimes that it references
603 // and treat them just like we would any other type,
604 // which means we would error out if we find any
605 // reference to a type/region that is not in the
608 // **However,** in the case of closures, there is a
609 // somewhat subtle (read: hacky) consideration. The
610 // problem is that our closure types currently include
611 // all the lifetime parameters declared on the
612 // enclosing function, even if they are unused by the
613 // closure itself. We can't readily filter them out,
614 // so here we replace those values with `'empty`. This
615 // can't really make a difference to the rest of the
616 // compiler; those regions are ignored for the
617 // outlives relation, and hence don't affect trait
618 // selection or auto traits, and they are erased
621 let generics = self.tcx.generics_of(def_id);
622 let substs = self.tcx.mk_substs(substs.substs.iter().enumerate().map(
624 if index < generics.parent_count {
625 // Accommodate missing regions in the parent kinds...
626 self.fold_kind_mapping_missing_regions_to_empty(kind)
628 // ...but not elsewhere.
629 self.fold_kind_normally(kind)
634 self.tcx.mk_closure(def_id, ty::ClosureSubsts { substs })
637 _ => ty.super_fold_with(self),
642 struct Instantiator<'a, 'gcx: 'tcx, 'tcx: 'a> {
643 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
644 parent_def_id: DefId,
645 body_id: ast::NodeId,
646 param_env: ty::ParamEnv<'tcx>,
647 opaque_types: OpaqueTypeMap<'tcx>,
648 obligations: Vec<PredicateObligation<'tcx>>,
651 impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
652 fn instantiate_opaque_types_in_map<T: TypeFoldable<'tcx>>(&mut self, value: &T) -> T {
653 debug!("instantiate_opaque_types_in_map(value={:?})", value);
654 let tcx = self.infcx.tcx;
655 value.fold_with(&mut BottomUpFolder {
659 if let ty::Opaque(def_id, substs) = ty.sty {
660 // Check that this is `impl Trait` type is
661 // declared by `parent_def_id` -- i.e., one whose
662 // value we are inferring. At present, this is
663 // always true during the first phase of
664 // type-check, but not always true later on during
665 // NLL. Once we support named abstract types more fully,
666 // this same scenario will be able to arise during all phases.
668 // Here is an example using `abstract type` that indicates
669 // the distinction we are checking for:
673 // pub abstract type Foo: Iterator;
674 // pub fn make_foo() -> Foo { .. }
678 // fn foo() -> a::Foo { a::make_foo() }
682 // Here, the return type of `foo` references a
683 // `Opaque` indeed, but not one whose value is
684 // presently being inferred. You can get into a
685 // similar situation with closure return types
689 // fn foo() -> impl Iterator { .. }
691 // let x = || foo(); // returns the Opaque assoc with `foo`
694 if let Some(opaque_node_id) = tcx.hir().as_local_node_id(def_id) {
695 let parent_def_id = self.parent_def_id;
696 let def_scope_default = || {
697 let opaque_parent_node_id = tcx.hir().get_parent(opaque_node_id);
698 parent_def_id == tcx.hir().local_def_id(opaque_parent_node_id)
700 let in_definition_scope = match tcx.hir().find(opaque_node_id) {
701 Some(Node::Item(item)) => match item.node {
703 hir::ItemKind::Existential(hir::ExistTy {
704 impl_trait_fn: Some(parent),
706 }) => parent == self.parent_def_id,
707 // named existential types
708 hir::ItemKind::Existential(hir::ExistTy {
711 }) => may_define_existential_type(
716 _ => def_scope_default(),
718 Some(Node::ImplItem(item)) => match item.node {
719 hir::ImplItemKind::Existential(_) => may_define_existential_type(
724 _ => def_scope_default(),
727 "expected (impl) item, found {}",
728 tcx.hir().node_to_string(opaque_node_id),
731 if in_definition_scope {
732 return self.fold_opaque_ty(ty, def_id, substs);
736 "instantiate_opaque_types_in_map: \
737 encountered opaque outside its definition scope \
753 substs: &'tcx Substs<'tcx>,
755 let infcx = self.infcx;
759 "instantiate_opaque_types: Opaque(def_id={:?}, substs={:?})",
763 // Use the same type variable if the exact same Opaque appears more
764 // than once in the return type (e.g. if it's passed to a type alias).
765 if let Some(opaque_defn) = self.opaque_types.get(&def_id) {
766 return opaque_defn.concrete_ty;
768 let span = tcx.def_span(def_id);
769 let ty_var = infcx.next_ty_var(TypeVariableOrigin::TypeInference(span));
771 let predicates_of = tcx.predicates_of(def_id);
773 "instantiate_opaque_types: predicates: {:#?}",
776 let bounds = predicates_of.instantiate(tcx, substs);
777 debug!("instantiate_opaque_types: bounds={:?}", bounds);
779 let required_region_bounds = tcx.required_region_bounds(ty, bounds.predicates.clone());
781 "instantiate_opaque_types: required_region_bounds={:?}",
782 required_region_bounds
785 // make sure that we are in fact defining the *entire* type
786 // e.g. `existential type Foo<T: Bound>: Bar;` needs to be
787 // defined by a function like `fn foo<T: Bound>() -> Foo<T>`.
789 "instantiate_opaque_types: param_env: {:#?}",
793 "instantiate_opaque_types: generics: {:#?}",
794 tcx.generics_of(def_id),
797 self.opaque_types.insert(
802 has_required_region_bounds: !required_region_bounds.is_empty(),
805 debug!("instantiate_opaque_types: ty_var={:?}", ty_var);
807 self.obligations.reserve(bounds.predicates.len());
808 for predicate in bounds.predicates {
809 // Change the predicate to refer to the type variable,
810 // which will be the concrete type instead of the opaque type.
811 // This also instantiates nested instances of `impl Trait`.
812 let predicate = self.instantiate_opaque_types_in_map(&predicate);
814 let cause = traits::ObligationCause::new(span, self.body_id, traits::SizedReturnType);
816 // Require that the predicate holds for the concrete type.
817 debug!("instantiate_opaque_types: predicate={:?}", predicate);
819 .push(traits::Obligation::new(cause, self.param_env, predicate));
826 /// Whether `opaque_node_id` is a sibling or a child of a sibling of `def_id`
831 /// pub existential type Baz;
833 /// fn f1() -> Baz { .. }
836 /// fn f2() -> bar::Baz { .. }
840 /// Here, `def_id` will be the `DefId` of the existential type `Baz`.
841 /// `opaque_node_id` is the `NodeId` of the reference to Baz --
842 /// so either the return type of f1 or f2.
843 /// We will return true if the reference is within the same module as the existential type
844 /// So true for f1, false for f2.
845 pub fn may_define_existential_type(
846 tcx: TyCtxt<'_, '_, '_>,
848 opaque_node_id: ast::NodeId,
850 let mut node_id = tcx
852 .as_local_node_id(def_id)
854 // named existential types can be defined by any siblings or
855 // children of siblings
856 let mod_id = tcx.hir().get_parent(opaque_node_id);
857 // so we walk up the node tree until we hit the root or the parent
858 // of the opaque type
859 while node_id != mod_id && node_id != ast::CRATE_NODE_ID {
860 node_id = tcx.hir().get_parent(node_id);
862 // syntactically we are allowed to define the concrete type