From c134d50283bd040a03fd076221a40c5211c1717e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 15 Oct 2024 17:48:53 +0200 Subject: [PATCH] Reveal the underlying types of `impl Trait` opaque types --- engine/lib/import_thir.ml | 2 +- frontend/exporter/src/types/copied.rs | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/engine/lib/import_thir.ml b/engine/lib/import_thir.ml index eb44d8c51..27870ae89 100644 --- a/engine/lib/import_thir.ml +++ b/engine/lib/import_thir.ml @@ -1019,7 +1019,7 @@ end) : EXPR = struct let impl = c_impl_expr span impl_expr in let item = Concrete_ident.of_def_id (AssociatedItem Type) def_id in TAssociatedType { impl; item } - | Alias { kind = Opaque; def_id; _ } -> + | Alias { kind = Opaque _; def_id; _ } -> TOpaque (Concrete_ident.of_def_id Type def_id) | Alias { kind = Inherent; _ } -> assertion_failure [ span ] "Ty::Alias with AliasTyKind::Inherent" diff --git a/frontend/exporter/src/types/copied.rs b/frontend/exporter/src/types/copied.rs index 8054f6fe9..0344024d1 100644 --- a/frontend/exporter/src/types/copied.rs +++ b/frontend/exporter/src/types/copied.rs @@ -1766,7 +1766,10 @@ pub enum AliasKind { /// An associated type in an inherent impl. Inherent, /// An `impl Trait` opaque type. - Opaque, + Opaque { + /// The real type hidden inside this opaque type. + hidden_ty: Ty, + }, /// A type alias that references opaque types. Likely to always be normalized away. Weak, } @@ -1778,11 +1781,11 @@ impl Alias { s: &S, alias_kind: &rustc_type_ir::AliasTyKind, alias_ty: &rustc_middle::ty::AliasTy<'tcx>, - ) -> Self { + ) -> TyKind { + let tcx = s.base().tcx; use rustc_type_ir::AliasTyKind as RustAliasKind; let kind = match alias_kind { RustAliasKind::Projection => { - let tcx = s.base().tcx; let trait_ref = alias_ty.trait_ref(tcx); // In a case like: // ``` @@ -1803,14 +1806,20 @@ impl Alias { } } RustAliasKind::Inherent => AliasKind::Inherent, - RustAliasKind::Opaque => AliasKind::Opaque, + RustAliasKind::Opaque => { + // Reveal the underlying `impl Trait` type. + let ty = tcx.type_of(alias_ty.def_id).instantiate(tcx, alias_ty.args); + AliasKind::Opaque { + hidden_ty: ty.sinto(s), + } + } RustAliasKind::Weak => AliasKind::Weak, }; - Alias { + TyKind::Alias(Alias { kind, args: alias_ty.args.sinto(s), def_id: alias_ty.def_id.sinto(s), - } + }) } } @@ -1903,7 +1912,7 @@ pub enum TyKind { Tuple(Vec), #[custom_arm( rustc_middle::ty::TyKind::Alias(alias_kind, alias_ty) => { - TyKind::Alias(Alias::from(state, alias_kind, alias_ty)) + Alias::from(state, alias_kind, alias_ty) }, )] Alias(Alias),