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

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

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

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

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

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

+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;
@@ -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

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

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

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

+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)
@@ -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)

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
@@ -469,6 +468,7 @@ define_tables! {
469468
doc_link_resolutions: Table<DefIndex, LazyValue<DocLinkResMap>>,
470469
doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,
471470
assumed_wf_types_for_rpitit: Table<DefIndex, LazyArray<(Ty<'static>, Span)>>,
471+
opaque_ty_origin: Table<DefIndex, LazyValue<hir::OpaqueTyOrigin<DefId>>>,
472472
}
473473

474474
#[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
@@ -260,11 +260,10 @@ rustc_queries! {
260260
separate_provide_extern
261261
}
262262

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

270269
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
@@ -2103,11 +2103,9 @@ impl<'tcx> TyCtxt<'tcx> {
21032103
}
21042104

21052105
/// Returns the origin of the opaque type `def_id`.
2106-
#[track_caller]
2107-
pub fn opaque_type_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin {
2108-
let origin = self.hir().expect_opaque_ty(def_id).origin;
2109-
trace!("opaque_type_origin({def_id:?}) => {origin:?}");
2110-
origin
2106+
#[instrument(skip(self), level = "trace", ret)]
2107+
pub fn local_opaque_ty_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin<LocalDefId> {
2108+
self.hir().expect_opaque_ty(def_id).origin
21112109
}
21122110
}
21132111

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
@@ -2648,7 +2648,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
26482648
obligation: &PredicateObligation<'tcx>,
26492649
def_id: DefId,
26502650
) -> ErrorGuaranteed {
2651-
let name = match self.tcx.opaque_type_origin(def_id.expect_local()) {
2651+
let name = match self.tcx.local_opaque_ty_origin(def_id.expect_local()) {
26522652
hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } => {
26532653
"opaque type".to_string()
26542654
}

compiler/rustc_ty_utils/src/assoc.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ fn associated_type_for_impl_trait_in_trait(
246246
) -> LocalDefId {
247247
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
248248
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. }) =
249-
tcx.opaque_type_origin(opaque_ty_def_id)
249+
tcx.local_opaque_ty_origin(opaque_ty_def_id)
250250
else {
251251
bug!("expected opaque for {opaque_ty_def_id:?}");
252252
};
@@ -284,8 +284,6 @@ fn associated_type_for_impl_trait_in_trait(
284284
// Copy defaultness of the containing function.
285285
trait_assoc_ty.defaultness(tcx.defaultness(fn_def_id));
286286

287-
trait_assoc_ty.is_type_alias_impl_trait(false);
288-
289287
// There are no inferred outlives for the synthesized associated type.
290288
trait_assoc_ty.inferred_outlives_of(&[]);
291289

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)