- /// Second round of fallback: Unconstrained type variables created
- /// from the instantiation of an opaque type fall back to the
- /// opaque type itself. This is a somewhat incomplete attempt to
- /// manage "identity passthrough" for `impl Trait` types.
- ///
- /// For example, in this code:
- ///
- ///```
- /// type MyType = impl Copy;
- /// fn defining_use() -> MyType { true }
- /// fn other_use() -> MyType { defining_use() }
- /// ```
- ///
- /// `defining_use` will constrain the instantiated inference
- /// variable to `bool`, while `other_use` will constrain
- /// the instantiated inference variable to `MyType`.
- ///
- /// When we process opaque types during writeback, we
- /// will handle cases like `other_use`, and not count
- /// them as defining usages
- ///
- /// However, we also need to handle cases like this:
- ///
- /// ```rust
- /// pub type Foo = impl Copy;
- /// fn produce() -> Option<Foo> {
- /// None
- /// }
- /// ```
- ///
- /// In the above snippet, the inference variable created by
- /// instantiating `Option<Foo>` will be completely unconstrained.
- /// We treat this as a non-defining use by making the inference
- /// variable fall back to the opaque type itself.
- fn fallback_opaque_type_vars(&self, ty: Ty<'tcx>) -> bool {
- let span = self
- .infcx
- .type_var_origin(ty)
- .map(|origin| origin.span)
- .unwrap_or(rustc_span::DUMMY_SP);
- let oty = self.inner.borrow().opaque_types_vars.get(&ty).copied();
- if let Some(opaque_ty) = oty {
- debug!(
- "fallback_opaque_type_vars(ty={:?}): falling back to opaque type {:?}",
- ty, opaque_ty
- );
- self.demand_eqtype(span, ty, opaque_ty);
- true
- } else {
- return false;
- }
- }
-