Skip to content

Commit

Permalink
Remove ResolvedOpaqueTy and just use Ty, SubstsRef is already there
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Jun 7, 2021
1 parent 7f8cad2 commit 7294f49
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 69 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/query.rs
Expand Up @@ -211,7 +211,7 @@ pub struct BorrowCheckResult<'tcx> {
/// All the opaque types that are restricted to concrete types
/// by this function. Unlike the value in `TypeckResults`, this has
/// unerased regions.
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, ty::ResolvedOpaqueTy<'tcx>>,
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
pub closure_requirements: Option<ClosureRegionRequirements<'tcx>>,
pub used_mut_upvars: SmallVec<[Field; 8]>,
}
Expand Down
13 changes: 1 addition & 12 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -288,17 +288,6 @@ impl<'a, V> LocalTableInContextMut<'a, V> {
}
}

/// All information necessary to validate and reveal an `impl Trait`.
#[derive(TyEncodable, TyDecodable, Debug, HashStable)]
pub struct ResolvedOpaqueTy<'tcx> {
/// The revealed type as seen by this function.
pub concrete_type: Ty<'tcx>,
/// Generic parameters on the opaque type as passed by this function.
/// For `type Foo<A, B> = impl Bar<A, B>; fn foo<T, U>() -> Foo<T, U> { .. }`
/// this is `[T, U]`, not `[A, B]`.
pub substs: SubstsRef<'tcx>,
}

/// Whenever a value may be live across a generator yield, the type of that value winds up in the
/// `GeneratorInteriorTypeCause` struct. This struct adds additional information about such
/// captured types that can be useful for diagnostics. In particular, it stores the span that
Expand Down Expand Up @@ -426,7 +415,7 @@ pub struct TypeckResults<'tcx> {

/// All the opaque types that are restricted to concrete types
/// by this function.
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, ResolvedOpaqueTy<'tcx>>,
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,

/// Tracks the minimum captures required for a closure;
/// see `MinCaptureInformationMap` for more details.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Expand Up @@ -58,7 +58,7 @@ pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt, Uneval
pub use self::context::{
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorInteriorTypeCause, GlobalCtxt,
Lift, ResolvedOpaqueTy, TyCtxt, TypeckResults, UserType, UserTypeAnnotationIndex,
Lift, TyCtxt, TypeckResults, UserType, UserTypeAnnotationIndex,
};
pub use self::instance::{Instance, InstanceDef};
pub use self::list::List;
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir/src/borrow_check/nll.rs
Expand Up @@ -8,7 +8,7 @@ use rustc_middle::mir::{
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
Promoted,
};
use rustc_middle::ty::{self, OpaqueTypeKey, RegionKind, RegionVid};
use rustc_middle::ty::{self, OpaqueTypeKey, RegionKind, RegionVid, Ty};
use rustc_span::symbol::sym;
use std::env;
use std::fmt::Debug;
Expand Down Expand Up @@ -46,7 +46,7 @@ crate type PoloniusOutput = Output<RustcFacts>;
/// closure requirements to propagate, and any generated errors.
crate struct NllOutput<'tcx> {
pub regioncx: RegionInferenceContext<'tcx>,
pub opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, ty::ResolvedOpaqueTy<'tcx>>,
pub opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
pub polonius_output: Option<Rc<PoloniusOutput>>,
pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
pub nll_errors: RegionErrors<'tcx>,
Expand Down Expand Up @@ -366,7 +366,7 @@ pub(super) fn dump_annotation<'a, 'tcx>(
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
opaque_type_values: &VecMap<OpaqueTypeKey<'tcx>, ty::ResolvedOpaqueTy<'tcx>>,
opaque_type_values: &VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
errors_buffer: &mut Vec<Diagnostic>,
) {
let tcx = infcx.tcx;
Expand Down
14 changes: 6 additions & 8 deletions compiler/rustc_mir/src/borrow_check/region_infer/opaque_types.rs
@@ -1,6 +1,6 @@
use rustc_data_structures::vec_map::VecMap;
use rustc_infer::infer::InferCtxt;
use rustc_middle::ty::{self, OpaqueTypeKey, TyCtxt, TypeFoldable};
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
use rustc_span::Span;
use rustc_trait_selection::opaque_types::InferCtxtExt;

Expand Down Expand Up @@ -50,12 +50,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
pub(in crate::borrow_check) fn infer_opaque_types(
&self,
infcx: &InferCtxt<'_, 'tcx>,
opaque_ty_decls: VecMap<OpaqueTypeKey<'tcx>, ty::ResolvedOpaqueTy<'tcx>>,
opaque_ty_decls: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
span: Span,
) -> VecMap<OpaqueTypeKey<'tcx>, ty::ResolvedOpaqueTy<'tcx>> {
) -> VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>> {
opaque_ty_decls
.into_iter()
.map(|(opaque_type_key, ty::ResolvedOpaqueTy { concrete_type, substs })| {
.map(|(opaque_type_key, concrete_type)| {
let substs = opaque_type_key.substs;
debug!(?concrete_type, ?substs);

let mut subst_regions = vec![self.universal_regions.fr_static];
Expand Down Expand Up @@ -116,10 +117,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
universal_concrete_type,
span,
);
(
opaque_type_key,
ty::ResolvedOpaqueTy { concrete_type: remapped_type, substs: universal_substs },
)
(opaque_type_key, remapped_type)
})
.collect()
}
Expand Down
25 changes: 9 additions & 16 deletions compiler/rustc_mir/src/borrow_check/type_check/mod.rs
Expand Up @@ -819,7 +819,7 @@ struct TypeChecker<'a, 'tcx> {
reported_errors: FxHashSet<(Ty<'tcx>, Span)>,
borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
universal_region_relations: &'a UniversalRegionRelations<'tcx>,
opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, ty::ResolvedOpaqueTy<'tcx>>,
opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
}

struct BorrowCheckContext<'a, 'tcx> {
Expand All @@ -834,7 +834,7 @@ struct BorrowCheckContext<'a, 'tcx> {
crate struct MirTypeckResults<'tcx> {
crate constraints: MirTypeckRegionConstraints<'tcx>,
pub(in crate::borrow_check) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
crate opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, ty::ResolvedOpaqueTy<'tcx>>,
crate opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
}

/// A collection of region constraints that must be satisfied for the
Expand Down Expand Up @@ -1292,10 +1292,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

let opaque_type_key =
OpaqueTypeKey { def_id: opaque_def_id, substs: opaque_decl.substs };
let opaque_defn_ty = match concrete_opaque_types
let concrete_ty = match concrete_opaque_types
.iter()
.find(|(opaque_type_key, _)| opaque_type_key.def_id == opaque_def_id)
.map(|(_, resolved_opaque_ty)| resolved_opaque_ty)
.map(|(_, concrete_ty)| concrete_ty)
{
None => {
if !concrete_is_opaque {
Expand All @@ -1309,17 +1309,16 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}
continue;
}
Some(opaque_defn_ty) => opaque_defn_ty,
Some(concrete_ty) => concrete_ty,
};
debug!("opaque_defn_ty = {:?}", opaque_defn_ty);
let subst_opaque_defn_ty =
opaque_defn_ty.concrete_type.subst(tcx, opaque_decl.substs);
debug!("concrete_ty = {:?}", concrete_ty);
let subst_opaque_defn_ty = concrete_ty.subst(tcx, opaque_decl.substs);
let renumbered_opaque_defn_ty =
renumber::renumber_regions(infcx, subst_opaque_defn_ty);

debug!(
"eq_opaque_type_and_type: concrete_ty={:?}={:?} opaque_defn_ty={:?}",
opaque_decl.concrete_ty, resolved_ty, renumbered_opaque_defn_ty,
concrete_ty, resolved_ty, renumbered_opaque_defn_ty,
);

if !concrete_is_opaque {
Expand All @@ -1330,13 +1329,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
.at(&ObligationCause::dummy(), param_env)
.eq(opaque_decl.concrete_ty, renumbered_opaque_defn_ty)?,
);
opaque_type_values.insert(
opaque_type_key,
ty::ResolvedOpaqueTy {
concrete_type: renumbered_opaque_defn_ty,
substs: opaque_decl.substs,
},
);
opaque_type_values.insert(opaque_type_key, renumbered_opaque_defn_ty);
} else {
// We're using an opaque `impl Trait` type without
// 'revealing' it. For example, code like this:
Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_typeck/src/check/writeback.rs
Expand Up @@ -522,24 +522,21 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
// in some other location, or we'll end up emitting an error due
// to the lack of defining usage
if !skip_add {
let new = ty::ResolvedOpaqueTy {
concrete_type: definition_ty,
substs: opaque_defn.substs,
};

let opaque_type_key = OpaqueTypeKey { def_id, substs: opaque_defn.substs };
let old =
self.typeck_results.concrete_opaque_types.insert(opaque_type_key, new);
if let Some(old) = old {
if old.concrete_type != definition_ty || old.substs != opaque_defn.substs {
let old_concrete_ty = self
.typeck_results
.concrete_opaque_types
.insert(opaque_type_key, definition_ty);
if let Some(old_concrete_ty) = old_concrete_ty {
if old_concrete_ty != definition_ty {
span_bug!(
span,
"`visit_opaque_types` tried to write different types for the same \
opaque type: {:?}, {:?}, {:?}, {:?}",
def_id,
definition_ty,
opaque_defn,
old,
old_concrete_ty,
);
}
}
Expand Down
30 changes: 12 additions & 18 deletions compiler/rustc_typeck/src/collect/type_of.rs
Expand Up @@ -10,9 +10,7 @@ use rustc_hir::{HirId, Node};
use rustc_middle::hir::map::Map;
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{
self, DefIdTree, OpaqueTypeKey, ResolvedOpaqueTy, Ty, TyCtxt, TypeFoldable,
};
use rustc_middle::ty::{self, DefIdTree, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
use rustc_span::symbol::Ident;
use rustc_span::{Span, DUMMY_SP};

Expand Down Expand Up @@ -353,7 +351,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
&tcx.mir_borrowck(owner.expect_local()).concrete_opaque_types,
def_id.to_def_id(),
)
.map(|opaque| opaque.concrete_type)
.map(|&(_, concrete_ty)| concrete_ty)
.unwrap_or_else(|| {
tcx.sess.delay_span_bug(
DUMMY_SP,
Expand Down Expand Up @@ -531,14 +529,13 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
return;
}
// Use borrowck to get the type with unerased regions.
let ty = find_concrete_ty_from_def_id(
&self.tcx.mir_borrowck(def_id).concrete_opaque_types,
self.def_id,
);
if let Some(ty::ResolvedOpaqueTy { concrete_type, substs }) = ty {
let concrete_opaque_types = &self.tcx.mir_borrowck(def_id).concrete_opaque_types;
if let Some((opaque_type_key, concrete_type)) =
find_concrete_ty_from_def_id(concrete_opaque_types, self.def_id)
{
debug!(
"find_opaque_ty_constraints: found constraint for `{:?}` at `{:?}`: {:?}",
self.def_id, def_id, ty,
self.def_id, def_id, concrete_type,
);

// FIXME(oli-obk): trace the actual span from inference to improve errors.
Expand All @@ -549,7 +546,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
// using `delay_span_bug`, just in case `wfcheck` slips up.
let opaque_generics = self.tcx.generics_of(self.def_id);
let mut used_params: FxHashSet<_> = FxHashSet::default();
for (i, arg) in substs.iter().enumerate() {
for (i, arg) in opaque_type_key.substs.iter().enumerate() {
let arg_is_param = match arg.unpack() {
GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)),
GenericArgKind::Lifetime(lt) => {
Expand Down Expand Up @@ -710,7 +707,7 @@ fn let_position_impl_trait_type(tcx: TyCtxt<'_>, opaque_ty_id: LocalDefId) -> Ty
let owner_typeck_results = tcx.typeck(scope_def_id);
let concrete_ty =
find_concrete_ty_from_def_id(&owner_typeck_results.concrete_opaque_types, opaque_ty_def_id)
.map(|opaque| opaque.concrete_type)
.map(|&(_, concrete_ty)| concrete_ty)
.unwrap_or_else(|| {
tcx.sess.delay_span_bug(
DUMMY_SP,
Expand Down Expand Up @@ -808,11 +805,8 @@ fn check_feature_inherent_assoc_ty(tcx: TyCtxt<'_>, span: Span) {
}

fn find_concrete_ty_from_def_id<'tcx>(
concrete_opaque_types: &'tcx VecMap<OpaqueTypeKey<'tcx>, ResolvedOpaqueTy<'tcx>>,
concrete_opaque_types: &'tcx VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
def_id: DefId,
) -> Option<&'tcx ResolvedOpaqueTy<'tcx>> {
concrete_opaque_types
.iter()
.find(|(opaque_type_key, _)| opaque_type_key.def_id == def_id)
.map(|(_, resolved_opaque_ty)| resolved_opaque_ty)
) -> Option<&'tcx (OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
concrete_opaque_types.iter().find(|(opaque_type_key, _)| opaque_type_key.def_id == def_id)
}

0 comments on commit 7294f49

Please sign in to comment.