use rustc_infer::infer::InferCtxt;
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts};
-use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt};
+use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt};
use rustc_span::Span;
pub trait InferCtxtExt<'tcx> {
fn infer_opaque_definition_from_instantiation(
&self,
opaque_type_key: OpaqueTypeKey<'tcx>,
- instantiated_ty: Ty<'tcx>,
- span: Span,
+ instantiated_ty: OpaqueHiddenType<'tcx>,
) -> Ty<'tcx>;
}
/// purpose of this function is to do that translation.
///
/// (*) C1 and C2 were introduced in the comments on
- /// `constrain_opaque_type`. Read that comment for more context.
+ /// `register_member_constraints`. Read that comment for more context.
///
/// # Parameters
///
fn infer_opaque_definition_from_instantiation(
&self,
opaque_type_key: OpaqueTypeKey<'tcx>,
- instantiated_ty: Ty<'tcx>,
- span: Span,
+ instantiated_ty: OpaqueHiddenType<'tcx>,
) -> Ty<'tcx> {
+ if self.is_tainted_by_errors() {
+ return self.tcx.ty_error();
+ }
+
let OpaqueTypeKey { def_id, substs } = opaque_type_key;
// Use substs to build up a reverse map from regions to their
// Convert the type from the function into a type valid outside
// the function, by replacing invalid regions with 'static,
// after producing an error for each of them.
- let definition_ty = instantiated_ty.fold_with(&mut ReverseMapper::new(
+ let definition_ty = instantiated_ty.ty.fold_with(&mut ReverseMapper::new(
self.tcx,
- self.is_tainted_by_errors(),
def_id,
map,
- instantiated_ty,
- span,
+ instantiated_ty.ty,
+ instantiated_ty.span,
));
debug!(?definition_ty);
struct ReverseMapper<'tcx> {
tcx: TyCtxt<'tcx>,
- /// If errors have already been reported in this fn, we suppress
- /// our own errors because they are sometimes derivative.
- tainted_by_errors: bool,
-
opaque_type_def_id: DefId,
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
map_missing_regions_to_empty: bool,
impl<'tcx> ReverseMapper<'tcx> {
fn new(
tcx: TyCtxt<'tcx>,
- tainted_by_errors: bool,
opaque_type_def_id: DefId,
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
hidden_ty: Ty<'tcx>,
) -> Self {
Self {
tcx,
- tainted_by_errors,
opaque_type_def_id,
map,
map_missing_regions_to_empty: false,
match *r {
// Ignore bound regions and `'static` regions that appear in the
// type, we only need to remap regions that reference lifetimes
- // from the function declaraion.
+ // from the function declaration.
// This would ignore `'r` in a type like `for<'r> fn(&'r u32)`.
ty::ReLateBound(..) | ty::ReStatic => return r,
match self.map.get(&r.into()).map(|k| k.unpack()) {
Some(GenericArgKind::Lifetime(r1)) => r1,
Some(u) => panic!("region mapped to unexpected kind: {:?}", u),
- None if self.map_missing_regions_to_empty || self.tainted_by_errors => {
- self.tcx.lifetimes.re_root_empty
- }
+ None if self.map_missing_regions_to_empty => self.tcx.lifetimes.re_root_empty,
None if generics.parent.is_some() => {
if let Some(hidden_ty) = self.hidden_ty.take() {
unexpected_hidden_region_diagnostic(