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

+3-3
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

+1-1
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

+9-7
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

+2-2
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

+13-4
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

+2-1
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

+1-1
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

+4-1
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

+1-4
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

+2-4
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)

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ define_tables! {
378378
- defaulted:
379379
intrinsic: Table<DefIndex, Option<LazyValue<ty::IntrinsicDef>>>,
380380
is_macro_rules: Table<DefIndex, bool>,
381-
is_type_alias_impl_trait: Table<DefIndex, bool>,
382381
type_alias_is_lazy: Table<DefIndex, bool>,
383382
attr_flags: Table<DefIndex, AttrFlags>,
384383
// The u64 is the crate-local part of the DefPathHash. All hashes in this crate have the same
@@ -468,6 +467,7 @@ define_tables! {
468467
doc_link_resolutions: Table<DefIndex, LazyValue<DocLinkResMap>>,
469468
doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,
470469
assumed_wf_types_for_rpitit: Table<DefIndex, LazyArray<(Ty<'static>, Span)>>,
470+
opaque_ty_origin: Table<DefIndex, LazyValue<hir::OpaqueTyOrigin<DefId>>>,
471471
}
472472

473473
#[derive(TyEncodable, TyDecodable)]

compiler/rustc_middle/src/query/erase.rs

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ trivial! {
280280
rustc_hir::IsAsync,
281281
rustc_hir::ItemLocalId,
282282
rustc_hir::LangItem,
283+
rustc_hir::OpaqueTyOrigin<rustc_hir::def_id::DefId>,
283284
rustc_hir::OwnerId,
284285
rustc_hir::Upvar,
285286
rustc_index::bit_set::FiniteBitSet<u32>,

compiler/rustc_middle/src/query/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,10 @@ rustc_queries! {
258258
separate_provide_extern
259259
}
260260

261-
query is_type_alias_impl_trait(key: DefId) -> bool
261+
query opaque_ty_origin(key: DefId) -> hir::OpaqueTyOrigin<DefId>
262262
{
263-
desc { "determine whether the opaque is a type-alias impl trait" }
263+
desc { "determine where the opaque originates from" }
264264
separate_provide_extern
265-
feedable
266265
}
267266

268267
query unsizing_params_for_adt(key: DefId) -> &'tcx rustc_index::bit_set::BitSet<u32>

compiler/rustc_middle/src/ty/context.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2074,11 +2074,9 @@ impl<'tcx> TyCtxt<'tcx> {
20742074
}
20752075

20762076
/// Returns the origin of the opaque type `def_id`.
2077-
#[track_caller]
2078-
pub fn opaque_type_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin {
2079-
let origin = self.hir().expect_opaque_ty(def_id).origin;
2080-
trace!("opaque_type_origin({def_id:?}) => {origin:?}");
2081-
origin
2077+
#[instrument(skip(self), level = "trace", ret)]
2078+
pub fn local_opaque_ty_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin<LocalDefId> {
2079+
self.hir().expect_opaque_ty(def_id).origin
20822080
}
20832081
}
20842082

compiler/rustc_middle/src/ty/parameterized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ trivially_parameterized_over_tcx! {
9494
rustc_hir::def_id::DefId,
9595
rustc_hir::def_id::DefIndex,
9696
rustc_hir::definitions::DefKey,
97+
rustc_hir::OpaqueTyOrigin<rustc_hir::def_id::DefId>,
9798
rustc_index::bit_set::BitSet<u32>,
9899
rustc_index::bit_set::FiniteBitSet<u32>,
99100
rustc_session::cstore::ForeignModule,

compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ impl<T> Trait<T> for X {
384384
| DefKind::AssocFn
385385
| DefKind::AssocConst
386386
)
387-
&& tcx.is_type_alias_impl_trait(opaque_ty.def_id)
387+
&& matches!(
388+
tcx.opaque_ty_origin(opaque_ty.def_id),
389+
hir::OpaqueTyOrigin::TyAlias { .. }
390+
)
388391
&& !tcx
389392
.opaque_types_defined_by(body_owner_def_id.expect_local())
390393
.contains(&opaque_ty.def_id.expect_local())

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2659,7 +2659,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
26592659
obligation: &PredicateObligation<'tcx>,
26602660
def_id: DefId,
26612661
) -> ErrorGuaranteed {
2662-
let name = match self.tcx.opaque_type_origin(def_id.expect_local()) {
2662+
let name = match self.tcx.local_opaque_ty_origin(def_id.expect_local()) {
26632663
hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } => {
26642664
"opaque type".to_string()
26652665
}

compiler/rustc_ty_utils/src/assoc.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ fn associated_type_for_effects(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<De
212212
// No default type
213213
trait_assoc_ty.defaultness(hir::Defaultness::Default { has_value: false });
214214

215-
trait_assoc_ty.is_type_alias_impl_trait(false);
216-
217215
(trait_assoc_ty, trait_def_id)
218216
}
219217
DefKind::Impl { .. } => {
@@ -378,7 +376,7 @@ fn associated_type_for_impl_trait_in_trait(
378376
) -> LocalDefId {
379377
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
380378
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. }) =
381-
tcx.opaque_type_origin(opaque_ty_def_id)
379+
tcx.local_opaque_ty_origin(opaque_ty_def_id)
382380
else {
383381
bug!("expected opaque for {opaque_ty_def_id:?}");
384382
};
@@ -416,8 +414,6 @@ fn associated_type_for_impl_trait_in_trait(
416414
// Copy defaultness of the containing function.
417415
trait_assoc_ty.defaultness(tcx.defaultness(fn_def_id));
418416

419-
trait_assoc_ty.is_type_alias_impl_trait(false);
420-
421417
// There are no inferred outlives for the synthesized associated type.
422418
trait_assoc_ty.inferred_outlives_of(&[]);
423419

compiler/rustc_ty_utils/src/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
139139
}
140140

141141
// TAITs outside their defining scopes are ignored.
142-
let origin = self.tcx.opaque_type_origin(alias_ty.def_id.expect_local());
142+
let origin = self.tcx.local_opaque_ty_origin(alias_ty.def_id.expect_local());
143143
trace!(?origin);
144144
match origin {
145145
rustc_hir::OpaqueTyOrigin::FnReturn { .. }

0 commit comments

Comments
 (0)