Skip to content

Commit 9fda623

Browse files
Encode cross-crate opaque type origin
1 parent e7c0d27 commit 9fda623

File tree

19 files changed

+53
-45
lines changed

19 files changed

+53
-45
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ enum ImplTraitContext {
288288
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
289289
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
290290
///
291-
OpaqueTy { origin: hir::OpaqueTyOrigin },
291+
OpaqueTy { origin: hir::OpaqueTyOrigin<LocalDefId> },
292292
/// `impl Trait` is unstably accepted in this position.
293293
FeatureGated(ImplTraitPosition, Symbol),
294294
/// `impl Trait` is not accepted in this position.
@@ -1483,7 +1483,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14831483
fn lower_opaque_impl_trait(
14841484
&mut self,
14851485
span: Span,
1486-
origin: hir::OpaqueTyOrigin,
1486+
origin: hir::OpaqueTyOrigin<LocalDefId>,
14871487
opaque_ty_node_id: NodeId,
14881488
bounds: &GenericBounds,
14891489
itctx: ImplTraitContext,
@@ -1588,7 +1588,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15881588
fn lower_opaque_inner(
15891589
&mut self,
15901590
opaque_ty_node_id: NodeId,
1591-
origin: hir::OpaqueTyOrigin,
1591+
origin: hir::OpaqueTyOrigin<LocalDefId>,
15921592
captured_lifetimes_to_duplicate: FxIndexSet<Lifetime>,
15931593
span: Span,
15941594
opaque_ty_span: Span,

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

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

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

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,7 @@ pub struct OpaqueTy<'hir> {
27522752
pub def_id: LocalDefId,
27532753
pub generics: &'hir Generics<'hir>,
27542754
pub bounds: GenericBounds<'hir>,
2755-
pub origin: OpaqueTyOrigin,
2755+
pub origin: OpaqueTyOrigin<LocalDefId>,
27562756
/// Return-position impl traits (and async futures) must "reify" any late-bound
27572757
/// lifetimes that are captured from the function signature they originate from.
27582758
///
@@ -2800,33 +2800,35 @@ pub struct PreciseCapturingNonLifetimeArg {
28002800
pub res: Res,
28012801
}
28022802

2803-
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
2803+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2804+
#[derive(HashStable_Generic, Encodable, Decodable)]
28042805
pub enum RpitContext {
28052806
Trait,
28062807
TraitImpl,
28072808
}
28082809

28092810
/// From whence the opaque type came.
2810-
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
2811-
pub enum OpaqueTyOrigin {
2811+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2812+
#[derive(HashStable_Generic, Encodable, Decodable)]
2813+
pub enum OpaqueTyOrigin<D> {
28122814
/// `-> impl Trait`
28132815
FnReturn {
28142816
/// The defining function.
2815-
parent: LocalDefId,
2817+
parent: D,
28162818
// Whether this is an RPITIT (return position impl trait in trait)
28172819
in_trait_or_impl: Option<RpitContext>,
28182820
},
28192821
/// `async fn`
28202822
AsyncFn {
28212823
/// The defining function.
2822-
parent: LocalDefId,
2824+
parent: D,
28232825
// Whether this is an AFIT (async fn in trait)
28242826
in_trait_or_impl: Option<RpitContext>,
28252827
},
28262828
/// type aliases: `type Foo = impl Trait;`
28272829
TyAlias {
28282830
/// The type alias or associated type parent of the TAIT/ATPIT
2829-
parent: LocalDefId,
2831+
parent: D,
28302832
/// associated types in impl blocks for traits.
28312833
in_assoc_ty: bool,
28322834
},

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn check_opaque_meets_bounds<'tcx>(
278278
tcx: TyCtxt<'tcx>,
279279
def_id: LocalDefId,
280280
span: Span,
281-
origin: &hir::OpaqueTyOrigin,
281+
origin: &hir::OpaqueTyOrigin<LocalDefId>,
282282
) -> Result<(), ErrorGuaranteed> {
283283
let defining_use_anchor = match *origin {
284284
hir::OpaqueTyOrigin::FnReturn { parent, .. }
@@ -686,7 +686,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
686686
DefKind::OpaqueTy => {
687687
check_opaque_precise_captures(tcx, def_id);
688688

689-
let origin = tcx.opaque_type_origin(def_id);
689+
let origin = tcx.local_opaque_ty_origin(def_id);
690690
if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
691691
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. } = origin
692692
&& 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
@@ -84,7 +84,7 @@ pub fn provide(providers: &mut Providers) {
8484
impl_trait_header,
8585
coroutine_kind,
8686
coroutine_for_closure,
87-
is_type_alias_impl_trait,
87+
opaque_ty_origin,
8888
rendered_precise_capturing_args,
8989
..*providers
9090
};
@@ -1755,9 +1755,18 @@ fn coroutine_for_closure(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DefId {
17551755
def_id.to_def_id()
17561756
}
17571757

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

17631772
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;
@@ -1796,7 +1797,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
17961797
match path.res {
17971798
Res::Def(DefKind::OpaqueTy, did) => {
17981799
// Check for desugared `impl Trait`.
1799-
assert!(tcx.is_type_alias_impl_trait(did));
1800+
assert_matches!(tcx.opaque_ty_origin(did), hir::OpaqueTyOrigin::TyAlias { .. });
18001801
let item_segment = path.segments.split_last().unwrap();
18011802
let _ = self
18021803
.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
@@ -603,7 +603,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
603603
_ => return None,
604604
};
605605
let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id, .. } =
606-
self.tcx.opaque_type_origin(def_id)
606+
self.tcx.local_opaque_ty_origin(def_id)
607607
else {
608608
return None;
609609
};

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

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

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,7 @@ provide! { tcx, def_id, other, cdata,
315315
})
316316
.unwrap_or_default()
317317
}
318-
is_type_alias_impl_trait => {
319-
debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
320-
cdata.root.tables.is_type_alias_impl_trait.get(cdata, def_id.index)
321-
}
318+
opaque_ty_origin => { table }
322319
assumed_wf_types_for_rpitit => { table }
323320
collect_return_position_impl_trait_in_trait_tys => {
324321
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)
@@ -1527,9 +1527,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15271527
if let DefKind::OpaqueTy = def_kind {
15281528
self.encode_explicit_item_bounds(def_id);
15291529
self.encode_explicit_item_super_predicates(def_id);
1530-
self.tables
1531-
.is_type_alias_impl_trait
1532-
.set(def_id.index, self.tcx.is_type_alias_impl_trait(def_id));
1530+
record!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id));
15331531
self.encode_precise_capturing_args(def_id);
15341532
}
15351533
if tcx.impl_method_has_trait_impl_trait_tys(def_id)

0 commit comments

Comments
 (0)