Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer opaque type regions in borrow checking
We want type checking for function bodies to ignore/erase regions. As such, we need to infer the regions in opaque types in borrow check instead.
- Loading branch information
1 parent
75ac0cc
commit d9b9f00
Showing
4 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ use crate::borrow_check::{ | |
|
||
mod dump_mir; | ||
mod graphviz; | ||
mod opaque_types; | ||
|
||
pub mod values; | ||
|
||
|
72 changes: 72 additions & 0 deletions
72
src/librustc_mir/borrow_check/region_infer/opaque_types.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use rustc::hir::def_id::DefId; | ||
use rustc::infer::InferCtxt; | ||
use rustc::ty; | ||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_span::Span; | ||
|
||
use super::RegionInferenceContext; | ||
|
||
impl<'tcx> RegionInferenceContext<'tcx> { | ||
/// Resolve any opaque types that were encountered while borrow checking | ||
/// this item. This is then used to get the type in the `type_of` query. | ||
pub(in crate::borrow_check) fn infer_opaque_types( | ||
&self, | ||
infcx: &InferCtxt<'_, 'tcx>, | ||
opaque_ty_decls: FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>>, | ||
span: Span, | ||
) -> FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>> { | ||
opaque_ty_decls | ||
.into_iter() | ||
.map(|(opaque_def_id, ty::ResolvedOpaqueTy { concrete_type, substs })| { | ||
debug!( | ||
"infer_opaque_types(concrete_type = {:?}, substs = {:?})", | ||
concrete_type, substs | ||
); | ||
|
||
// Map back to "concrete" regions so that errors in | ||
// `infer_opaque_definition_from_instantiation` can show | ||
// sensible region names. | ||
let universal_concrete_type = | ||
infcx.tcx.fold_regions(&concrete_type, &mut false, |region, _| match region { | ||
&ty::ReVar(vid) => { | ||
let universal_bound = self.universal_upper_bound(vid); | ||
self.definitions[universal_bound] | ||
.external_name | ||
.filter(|_| self.eval_equal(universal_bound, vid)) | ||
.unwrap_or(infcx.tcx.lifetimes.re_empty) | ||
} | ||
concrete => concrete, | ||
}); | ||
let universal_substs = | ||
infcx.tcx.fold_regions(&substs, &mut false, |region, _| match region { | ||
ty::ReVar(vid) => { | ||
self.definitions[*vid].external_name.unwrap_or_else(|| { | ||
infcx.tcx.sess.delay_span_bug( | ||
span, | ||
"opaque type with non-universal region substs", | ||
); | ||
infcx.tcx.lifetimes.re_static | ||
}) | ||
} | ||
concrete => concrete, | ||
}); | ||
|
||
debug!( | ||
"infer_opaque_types(universal_concrete_type = {:?}, universal_substs = {:?})", | ||
universal_concrete_type, universal_substs | ||
); | ||
|
||
let remapped_type = infcx.infer_opaque_definition_from_instantiation( | ||
opaque_def_id, | ||
universal_substs, | ||
universal_concrete_type, | ||
span, | ||
); | ||
( | ||
opaque_def_id, | ||
ty::ResolvedOpaqueTy { concrete_type: remapped_type, substs: universal_substs }, | ||
) | ||
}) | ||
.collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters