Skip to content

Commit 0652c94

Browse files
fully_revealed_type_of
1 parent 1f5a006 commit 0652c94

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub fn provide(providers: &mut Providers) {
6262
*providers = Providers {
6363
opt_const_param_of: type_of::opt_const_param_of,
6464
type_of: type_of::type_of,
65+
fully_revealed_type_of: type_of::fully_revealed_type_of,
6566
item_bounds: item_bounds::item_bounds,
6667
explicit_item_bounds: item_bounds::explicit_item_bounds,
6768
generics_of: generics_of::generics_of,

compiler/rustc_hir_analysis/src/collect/type_of.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_hir::{HirId, Node};
77
use rustc_middle::hir::nested_filter;
88
use rustc_middle::ty::subst::InternalSubsts;
99
use rustc_middle::ty::util::IntTypeExt;
10-
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt, TypeFolder, TypeSuperFoldable, TypeVisitable};
10+
use rustc_middle::ty::{
11+
self, DefIdTree, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable,
12+
};
1113
use rustc_span::symbol::Ident;
1214
use rustc_span::{Span, DUMMY_SP};
1315

@@ -538,6 +540,35 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
538540
}
539541
}
540542

543+
pub fn fully_revealed_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
544+
let ty = tcx.type_of(def_id);
545+
if ty.has_opaque_types() { ty.fold_with(&mut DeeperTypeFolder { tcx }) } else { ty }
546+
}
547+
548+
struct DeeperTypeFolder<'tcx> {
549+
tcx: TyCtxt<'tcx>,
550+
}
551+
552+
impl<'tcx> TypeFolder<'tcx> for DeeperTypeFolder<'tcx> {
553+
fn tcx(&self) -> TyCtxt<'tcx> {
554+
self.tcx
555+
}
556+
557+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
558+
if !ty.has_opaque_types() {
559+
return ty;
560+
}
561+
562+
let ty = ty.super_fold_with(self);
563+
564+
if let ty::Opaque(def_id, substs) = *ty.kind() {
565+
self.tcx.bound_fully_revealed_type_of(def_id).subst(self.tcx, substs)
566+
} else {
567+
ty
568+
}
569+
}
570+
}
571+
541572
#[instrument(skip(tcx), level = "debug")]
542573
/// Checks "defining uses" of opaque `impl Trait` types to ensure that they meet the restrictions
543574
/// laid for "higher-order pattern unification".

compiler/rustc_middle/src/query/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ rustc_queries! {
164164
separate_provide_extern
165165
}
166166

167+
query fully_revealed_type_of(key: DefId) -> Ty<'tcx> {
168+
desc { |tcx|
169+
"fully {action} `{path}`",
170+
action = {
171+
use rustc_hir::def::DefKind;
172+
match tcx.def_kind(key) {
173+
DefKind::TyAlias => "expanding type alias",
174+
DefKind::TraitAlias => "expanding trait alias",
175+
_ => "computing type of",
176+
}
177+
},
178+
path = tcx.def_path_str(key),
179+
}
180+
}
181+
167182
query collect_trait_impl_trait_tys(key: DefId)
168183
-> Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed>
169184
{

compiler/rustc_middle/src/ty/util.rs

+4
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,10 @@ impl<'tcx> TyCtxt<'tcx> {
650650
ty::EarlyBinder(self.type_of(def_id))
651651
}
652652

653+
pub fn bound_fully_revealed_type_of(self, def_id: DefId) -> ty::EarlyBinder<Ty<'tcx>> {
654+
ty::EarlyBinder(self.fully_revealed_type_of(def_id))
655+
}
656+
653657
pub fn bound_trait_impl_trait_tys(
654658
self,
655659
def_id: DefId,

compiler/rustc_trait_selection/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
514514
}
515515

516516
let substs = substs.fold_with(self);
517-
let generic_ty = self.tcx().bound_type_of(def_id);
517+
let generic_ty = self.tcx().bound_fully_revealed_type_of(def_id);
518518
let concrete_ty = generic_ty.subst(self.tcx(), substs);
519519
self.depth += 1;
520520
let folded_ty = self.fold_ty(concrete_ty);

compiler/rustc_trait_selection/src/traits/query/normalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
217217
self.infcx.err_ctxt().report_overflow_error(&obligation, true);
218218
}
219219

220-
let generic_ty = self.tcx().bound_type_of(def_id);
220+
let generic_ty = self.tcx().bound_fully_revealed_type_of(def_id);
221221
let concrete_ty = generic_ty.subst(self.tcx(), substs);
222222
self.anon_depth += 1;
223223
if concrete_ty == ty {

0 commit comments

Comments
 (0)