|
| 1 | +use rustc::hir::def_id::DefId; |
| 2 | +use rustc::infer::InferCtxt; |
| 3 | +use rustc::ty; |
| 4 | +use rustc_data_structures::fx::FxHashMap; |
| 5 | +use rustc_span::Span; |
| 6 | + |
| 7 | +use super::RegionInferenceContext; |
| 8 | + |
| 9 | +impl<'tcx> RegionInferenceContext<'tcx> { |
| 10 | + /// Resolve any opaque types that were encountered while borrow checking |
| 11 | + /// this item. This is then used to get the type in the `type_of` query. |
| 12 | + pub(in crate::borrow_check) fn infer_opaque_types( |
| 13 | + &self, |
| 14 | + infcx: &InferCtxt<'_, 'tcx>, |
| 15 | + opaque_ty_decls: FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>>, |
| 16 | + span: Span, |
| 17 | + ) -> FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>> { |
| 18 | + opaque_ty_decls |
| 19 | + .into_iter() |
| 20 | + .map(|(opaque_def_id, ty::ResolvedOpaqueTy { concrete_type, substs })| { |
| 21 | + debug!( |
| 22 | + "infer_opaque_types(concrete_type = {:?}, substs = {:?})", |
| 23 | + concrete_type, substs |
| 24 | + ); |
| 25 | + |
| 26 | + // Map back to "concrete" regions so that errors in |
| 27 | + // `infer_opaque_definition_from_instantiation` can show |
| 28 | + // sensible region names. |
| 29 | + let universal_concrete_type = |
| 30 | + infcx.tcx.fold_regions(&concrete_type, &mut false, |region, _| match region { |
| 31 | + &ty::ReVar(vid) => { |
| 32 | + let universal_bound = self.universal_upper_bound(vid); |
| 33 | + self.definitions[universal_bound] |
| 34 | + .external_name |
| 35 | + .filter(|_| self.eval_equal(universal_bound, vid)) |
| 36 | + .unwrap_or(infcx.tcx.lifetimes.re_empty) |
| 37 | + } |
| 38 | + concrete => concrete, |
| 39 | + }); |
| 40 | + let universal_substs = |
| 41 | + infcx.tcx.fold_regions(&substs, &mut false, |region, _| match region { |
| 42 | + ty::ReVar(vid) => { |
| 43 | + self.definitions[*vid].external_name.unwrap_or_else(|| { |
| 44 | + infcx.tcx.sess.delay_span_bug( |
| 45 | + span, |
| 46 | + "opaque type with non-universal region substs", |
| 47 | + ); |
| 48 | + infcx.tcx.lifetimes.re_static |
| 49 | + }) |
| 50 | + } |
| 51 | + concrete => concrete, |
| 52 | + }); |
| 53 | + |
| 54 | + debug!( |
| 55 | + "infer_opaque_types(universal_concrete_type = {:?}, universal_substs = {:?})", |
| 56 | + universal_concrete_type, universal_substs |
| 57 | + ); |
| 58 | + |
| 59 | + let remapped_type = infcx.infer_opaque_definition_from_instantiation( |
| 60 | + opaque_def_id, |
| 61 | + universal_substs, |
| 62 | + universal_concrete_type, |
| 63 | + span, |
| 64 | + ); |
| 65 | + ( |
| 66 | + opaque_def_id, |
| 67 | + ty::ResolvedOpaqueTy { concrete_type: remapped_type, substs: universal_substs }, |
| 68 | + ) |
| 69 | + }) |
| 70 | + .collect() |
| 71 | + } |
| 72 | +} |
0 commit comments