Skip to content

Commit

Permalink
Use substs from opaque type key instead of using it from opaque_decl
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Jun 7, 2021
1 parent 37ab718 commit 5dabd55
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
18 changes: 7 additions & 11 deletions compiler/rustc_mir/src/borrow_check/type_check/mod.rs
Expand Up @@ -1283,28 +1283,25 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
);

for &(opaque_type_key, opaque_decl) in &opaque_type_map {
let opaque_def_id = opaque_type_key.def_id;
let resolved_ty = infcx.resolve_vars_if_possible(opaque_decl.concrete_ty);
let concrete_is_opaque = if let ty::Opaque(def_id, _) = resolved_ty.kind() {
*def_id == opaque_def_id
*def_id == opaque_type_key.def_id
} else {
false
};

let opaque_type_key =
OpaqueTypeKey { def_id: opaque_def_id, substs: opaque_decl.substs };
let concrete_ty = match concrete_opaque_types
.iter()
.find(|(opaque_type_key, _)| opaque_type_key.def_id == opaque_def_id)
.map(|(_, concrete_ty)| concrete_ty)
.find(|(key, _)| key.def_id == opaque_type_key.def_id)
.map(|(_, ty)| ty)
{
None => {
if !concrete_is_opaque {
tcx.sess.delay_span_bug(
body.span,
&format!(
"Non-defining use of {:?} with revealed type",
opaque_def_id,
opaque_type_key.def_id,
),
);
}
Expand All @@ -1313,7 +1310,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Some(concrete_ty) => concrete_ty,
};
debug!("concrete_ty = {:?}", concrete_ty);
let subst_opaque_defn_ty = concrete_ty.subst(tcx, opaque_decl.substs);
let subst_opaque_defn_ty = concrete_ty.subst(tcx, opaque_type_key.substs);
let renumbered_opaque_defn_ty =
renumber::renumber_regions(infcx, subst_opaque_defn_ty);

Expand Down Expand Up @@ -1353,7 +1350,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// gets 'revealed' into
debug!(
"eq_opaque_type_and_type: non-defining use of {:?}",
opaque_def_id,
opaque_type_key.def_id,
);
}
}
Expand All @@ -1379,14 +1376,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// instantiated it with).
if let Some(opaque_type_map) = opaque_type_map {
for (opaque_type_key, opaque_decl) in opaque_type_map {
let opaque_def_id = opaque_type_key.def_id;
self.fully_perform_op(
locations,
ConstraintCategory::OpaqueType,
CustomTypeOp::new(
|_cx| {
infcx.constrain_opaque_type(
opaque_def_id,
opaque_type_key.def_id,
&opaque_decl,
GenerateMemberConstraints::IfNoStaticBound,
universal_region_relations,
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_trait_selection/src/opaque_types.rs
Expand Up @@ -371,9 +371,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
debug!("constrain_opaque_types()");

for &(opaque_type_key, opaque_defn) in opaque_types {
let OpaqueTypeKey { def_id, substs: _ } = opaque_type_key;
self.constrain_opaque_type(
def_id,
opaque_type_key.def_id,
&opaque_defn,
GenerateMemberConstraints::WhenRequired,
free_region_relations,
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_typeck/src/check/check.rs
Expand Up @@ -717,11 +717,10 @@ fn check_opaque_meets_bounds<'tcx>(
);

for (opaque_type_key, opaque_defn) in opaque_type_map {
let def_id = opaque_type_key.def_id;
match infcx
.at(&misc_cause, param_env)
.eq(opaque_defn.concrete_ty, tcx.type_of(def_id).subst(tcx, opaque_defn.substs))
{
match infcx.at(&misc_cause, param_env).eq(
opaque_defn.concrete_ty,
tcx.type_of(opaque_type_key.def_id).subst(tcx, opaque_defn.substs),
) {
Ok(infer_ok) => inh.register_infer_ok_obligations(infer_ok),
Err(ty_err) => tcx.sess.delay_span_bug(
opaque_defn.definition_span,
Expand Down
14 changes: 6 additions & 8 deletions compiler/rustc_typeck/src/check/writeback.rs
Expand Up @@ -15,7 +15,7 @@ use rustc_middle::hir::place::Place as HirPlace;
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast};
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::symbol::sym;
use rustc_span::Span;
use rustc_trait_selection::opaque_types::InferCtxtExt;
Expand Down Expand Up @@ -476,8 +476,8 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {

fn visit_opaque_types(&mut self, span: Span) {
for &(opaque_type_key, opaque_defn) in self.fcx.opaque_types.borrow().iter() {
let OpaqueTypeKey { def_id, substs: _ } = opaque_type_key;
let hir_id = self.tcx().hir().local_def_id_to_hir_id(def_id.expect_local());
let hir_id =
self.tcx().hir().local_def_id_to_hir_id(opaque_type_key.def_id.expect_local());
let instantiated_ty = self.resolve(opaque_defn.concrete_ty, &hir_id);

debug_assert!(!instantiated_ty.has_escaping_bound_vars());
Expand All @@ -494,7 +494,6 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
// fn foo<U>() -> Foo<U> { .. }
// ```
// figures out the concrete type with `U`, but the stored type is with `T`.
let opaque_type_key = OpaqueTypeKey { def_id, substs: opaque_defn.substs };
let definition_ty = self.fcx.infer_opaque_definition_from_instantiation(
opaque_type_key,
instantiated_ty,
Expand All @@ -506,7 +505,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
if let ty::Opaque(defin_ty_def_id, _substs) = *definition_ty.kind() {
if let hir::OpaqueTyOrigin::Misc | hir::OpaqueTyOrigin::TyAlias = opaque_defn.origin
{
if def_id == defin_ty_def_id {
if opaque_type_key.def_id == defin_ty_def_id {
debug!(
"skipping adding concrete definition for opaque type {:?} {:?}",
opaque_defn, defin_ty_def_id
Expand All @@ -516,14 +515,13 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
}
}

if !opaque_defn.substs.needs_infer() {
if !opaque_type_key.substs.needs_infer() {
// We only want to add an entry into `concrete_opaque_types`
// if we actually found a defining usage of this opaque type.
// Otherwise, we do nothing - we'll either find a defining usage
// in some other location, or we'll end up emitting an error due
// to the lack of defining usage
if !skip_add {
let opaque_type_key = OpaqueTypeKey { def_id, substs: opaque_defn.substs };
let old_concrete_ty = self
.typeck_results
.concrete_opaque_types
Expand All @@ -534,7 +532,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
span,
"`visit_opaque_types` tried to write different types for the same \
opaque type: {:?}, {:?}, {:?}, {:?}",
def_id,
opaque_type_key.def_id,
definition_ty,
opaque_defn,
old_concrete_ty,
Expand Down

0 comments on commit 5dabd55

Please sign in to comment.