Skip to content

Commit e093b82

Browse files
Encode cross-crate opaque type origin
1 parent 4add5e4 commit e093b82

File tree

19 files changed

+53
-43
lines changed

19 files changed

+53
-43
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ enum ImplTraitContext {
272272
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
273273
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
274274
///
275-
OpaqueTy { origin: hir::OpaqueTyOrigin },
275+
OpaqueTy { origin: hir::OpaqueTyOrigin<LocalDefId> },
276276
/// `impl Trait` is unstably accepted in this position.
277277
FeatureGated(ImplTraitPosition, Symbol),
278278
/// `impl Trait` is not accepted in this position.
@@ -1416,7 +1416,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14161416
fn lower_opaque_impl_trait(
14171417
&mut self,
14181418
span: Span,
1419-
origin: hir::OpaqueTyOrigin,
1419+
origin: hir::OpaqueTyOrigin<LocalDefId>,
14201420
opaque_ty_node_id: NodeId,
14211421
bounds: &GenericBounds,
14221422
itctx: ImplTraitContext,
@@ -1458,7 +1458,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14581458
fn lower_opaque_inner(
14591459
&mut self,
14601460
opaque_ty_node_id: NodeId,
1461-
origin: hir::OpaqueTyOrigin,
1461+
origin: hir::OpaqueTyOrigin<LocalDefId>,
14621462
opaque_ty_span: Span,
14631463
lower_item_bounds: impl FnOnce(&mut Self) -> &'hir [hir::GenericBound<'hir>],
14641464
) -> hir::TyKind<'hir> {

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl<'tcx> LazyOpaqueTyEnv<'tcx> {
502502
}
503503

504504
let &Self { tcx, def_id, .. } = self;
505-
let origin = tcx.opaque_type_origin(def_id);
505+
let origin = tcx.local_opaque_ty_origin(def_id);
506506
let parent = match origin {
507507
hir::OpaqueTyOrigin::FnReturn { parent, .. }
508508
| hir::OpaqueTyOrigin::AsyncFn { parent, .. }

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,7 @@ pub struct OpaqueTy<'hir> {
27462746
pub hir_id: HirId,
27472747
pub def_id: LocalDefId,
27482748
pub bounds: GenericBounds<'hir>,
2749-
pub origin: OpaqueTyOrigin,
2749+
pub origin: OpaqueTyOrigin<LocalDefId>,
27502750
pub span: Span,
27512751
}
27522752

@@ -2784,33 +2784,35 @@ pub struct PreciseCapturingNonLifetimeArg {
27842784
pub res: Res,
27852785
}
27862786

2787-
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
2787+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2788+
#[derive(HashStable_Generic, Encodable, Decodable)]
27882789
pub enum RpitContext {
27892790
Trait,
27902791
TraitImpl,
27912792
}
27922793

27932794
/// From whence the opaque type came.
2794-
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
2795-
pub enum OpaqueTyOrigin {
2795+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2796+
#[derive(HashStable_Generic, Encodable, Decodable)]
2797+
pub enum OpaqueTyOrigin<D> {
27962798
/// `-> impl Trait`
27972799
FnReturn {
27982800
/// The defining function.
2799-
parent: LocalDefId,
2801+
parent: D,
28002802
// Whether this is an RPITIT (return position impl trait in trait)
28012803
in_trait_or_impl: Option<RpitContext>,
28022804
},
28032805
/// `async fn`
28042806
AsyncFn {
28052807
/// The defining function.
2806-
parent: LocalDefId,
2808+
parent: D,
28072809
// Whether this is an AFIT (async fn in trait)
28082810
in_trait_or_impl: Option<RpitContext>,
28092811
},
28102812
/// type aliases: `type Foo = impl Trait;`
28112813
TyAlias {
28122814
/// The type alias or associated type parent of the TAIT/ATPIT
2813-
parent: LocalDefId,
2815+
parent: D,
28142816
/// associated types in impl blocks for traits.
28152817
in_assoc_ty: bool,
28162818
},

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn check_opaque_meets_bounds<'tcx>(
268268
tcx: TyCtxt<'tcx>,
269269
def_id: LocalDefId,
270270
span: Span,
271-
origin: &hir::OpaqueTyOrigin,
271+
origin: &hir::OpaqueTyOrigin<LocalDefId>,
272272
) -> Result<(), ErrorGuaranteed> {
273273
let defining_use_anchor = match *origin {
274274
hir::OpaqueTyOrigin::FnReturn { parent, .. }
@@ -677,7 +677,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
677677
DefKind::OpaqueTy => {
678678
check_opaque_precise_captures(tcx, def_id);
679679

680-
let origin = tcx.opaque_type_origin(def_id);
680+
let origin = tcx.local_opaque_ty_origin(def_id);
681681
if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
682682
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. } = origin
683683
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn provide(providers: &mut Providers) {
8686
impl_trait_header,
8787
coroutine_kind,
8888
coroutine_for_closure,
89-
is_type_alias_impl_trait,
89+
opaque_ty_origin,
9090
rendered_precise_capturing_args,
9191
..*providers
9292
};
@@ -1759,9 +1759,18 @@ fn coroutine_for_closure(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DefId {
17591759
def_id.to_def_id()
17601760
}
17611761

1762-
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
1763-
let opaque = tcx.hir().expect_opaque_ty(def_id);
1764-
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias { .. })
1762+
fn opaque_ty_origin<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> hir::OpaqueTyOrigin<DefId> {
1763+
match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
1764+
hir::OpaqueTyOrigin::FnReturn { parent, in_trait_or_impl } => {
1765+
hir::OpaqueTyOrigin::FnReturn { parent: parent.to_def_id(), in_trait_or_impl }
1766+
}
1767+
hir::OpaqueTyOrigin::AsyncFn { parent, in_trait_or_impl } => {
1768+
hir::OpaqueTyOrigin::AsyncFn { parent: parent.to_def_id(), in_trait_or_impl }
1769+
}
1770+
hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty } => {
1771+
hir::OpaqueTyOrigin::TyAlias { parent: parent.to_def_id(), in_assoc_ty }
1772+
}
1773+
}
17651774
}
17661775

17671776
fn rendered_precise_capturing_args<'tcx>(

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod errors;
2020
pub mod generics;
2121
mod lint;
2222

23+
use std::assert_matches::assert_matches;
2324
use std::slice;
2425

2526
use rustc_ast::TraitObjectSyntax;
@@ -1811,7 +1812,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18111812
match path.res {
18121813
Res::Def(DefKind::OpaqueTy, did) => {
18131814
// Check for desugared `impl Trait`.
1814-
assert!(tcx.is_type_alias_impl_trait(did));
1815+
assert_matches!(tcx.opaque_ty_origin(did), hir::OpaqueTyOrigin::TyAlias { .. });
18151816
let item_segment = path.segments.split_last().unwrap();
18161817
let _ = self
18171818
.prohibit_generic_args(item_segment.1.iter(), GenericsArgsErrExtend::OpaqueTy);

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
601601
_ => return None,
602602
};
603603
let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id, .. } =
604-
self.tcx.opaque_type_origin(def_id)
604+
self.tcx.local_opaque_ty_origin(def_id)
605605
else {
606606
return None;
607607
};

compiler/rustc_infer/src/infer/opaque_types/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ impl<'tcx> InferCtxt<'tcx> {
155155
// however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`, where
156156
// it is of no concern, so we only check for TAITs.
157157
if self.can_define_opaque_ty(b_def_id)
158-
&& self.tcx.is_type_alias_impl_trait(b_def_id)
158+
&& matches!(
159+
self.tcx.opaque_ty_origin(b_def_id),
160+
hir::OpaqueTyOrigin::TyAlias { .. }
161+
)
159162
{
160163
self.dcx().emit_err(OpaqueHiddenTypeDiag {
161164
span,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,7 @@ provide! { tcx, def_id, other, cdata,
316316
})
317317
.unwrap_or_default()
318318
}
319-
is_type_alias_impl_trait => {
320-
debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
321-
cdata.root.tables.is_type_alias_impl_trait.get(cdata, def_id.index)
322-
}
319+
opaque_ty_origin => { table }
323320
assumed_wf_types_for_rpitit => { table }
324321
collect_return_position_impl_trait_in_trait_tys => {
325322
Ok(cdata

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11881188
| DefKind::SyntheticCoroutineBody => true,
11891189

11901190
DefKind::OpaqueTy => {
1191-
let origin = tcx.opaque_type_origin(def_id);
1191+
let origin = tcx.local_opaque_ty_origin(def_id);
11921192
if let hir::OpaqueTyOrigin::FnReturn { parent, .. }
11931193
| hir::OpaqueTyOrigin::AsyncFn { parent, .. } = origin
11941194
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(parent)
@@ -1530,9 +1530,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15301530
if let DefKind::OpaqueTy = def_kind {
15311531
self.encode_explicit_item_bounds(def_id);
15321532
self.encode_explicit_item_super_predicates(def_id);
1533-
self.tables
1534-
.is_type_alias_impl_trait
1535-
.set(def_id.index, self.tcx.is_type_alias_impl_trait(def_id));
1533+
record!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id));
15361534
self.encode_precise_capturing_args(def_id);
15371535
}
15381536
if tcx.impl_method_has_trait_impl_trait_tys(def_id)

0 commit comments

Comments
 (0)