Skip to content

Commit 4129197

Browse files
Encode cross-crate opaque type origin
1 parent e1e3cac commit 4129197

File tree

19 files changed

+49
-43
lines changed

19 files changed

+49
-43
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.
@@ -1500,7 +1500,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15001500
fn lower_opaque_impl_trait(
15011501
&mut self,
15021502
span: Span,
1503-
origin: hir::OpaqueTyOrigin,
1503+
origin: hir::OpaqueTyOrigin<LocalDefId>,
15041504
opaque_ty_node_id: NodeId,
15051505
bounds: &GenericBounds,
15061506
itctx: ImplTraitContext,
@@ -1596,7 +1596,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15961596
fn lower_opaque_inner(
15971597
&mut self,
15981598
opaque_ty_node_id: NodeId,
1599-
origin: hir::OpaqueTyOrigin,
1599+
origin: hir::OpaqueTyOrigin<LocalDefId>,
16001600
captured_lifetimes_to_duplicate: FxIndexSet<Lifetime>,
16011601
span: Span,
16021602
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
@@ -2751,7 +2751,7 @@ pub struct BareFnTy<'hir> {
27512751
pub struct OpaqueTy<'hir> {
27522752
pub generics: &'hir Generics<'hir>,
27532753
pub bounds: GenericBounds<'hir>,
2754-
pub origin: OpaqueTyOrigin,
2754+
pub origin: OpaqueTyOrigin<LocalDefId>,
27552755
/// Return-position impl traits (and async futures) must "reify" any late-bound
27562756
/// lifetimes that are captured from the function signature they originate from.
27572757
///
@@ -2798,33 +2798,35 @@ pub struct PreciseCapturingNonLifetimeArg {
27982798
pub res: Res,
27992799
}
28002800

2801-
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
2801+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2802+
#[derive(HashStable_Generic, Encodable, Decodable)]
28022803
pub enum RpitContext {
28032804
Trait,
28042805
TraitImpl,
28052806
}
28062807

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

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn check_opaque_meets_bounds<'tcx>(
333333
tcx: TyCtxt<'tcx>,
334334
def_id: LocalDefId,
335335
span: Span,
336-
origin: &hir::OpaqueTyOrigin,
336+
origin: &hir::OpaqueTyOrigin<LocalDefId>,
337337
) -> Result<(), ErrorGuaranteed> {
338338
let defining_use_anchor = match *origin {
339339
hir::OpaqueTyOrigin::FnReturn { parent, .. }
@@ -735,7 +735,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
735735
DefKind::OpaqueTy => {
736736
check_opaque_precise_captures(tcx, def_id);
737737

738-
let origin = tcx.opaque_type_origin(def_id);
738+
let origin = tcx.local_opaque_ty_origin(def_id);
739739
if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
740740
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. } = origin
741741
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)

compiler/rustc_hir_analysis/src/collect.rs

+11-6
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
};
@@ -1851,12 +1851,17 @@ fn coroutine_for_closure(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DefId {
18511851
def_id.to_def_id()
18521852
}
18531853

1854-
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
1855-
match tcx.hir_node_by_def_id(def_id) {
1856-
Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(opaque), .. }) => {
1857-
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias { .. })
1854+
fn opaque_ty_origin<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> hir::OpaqueTyOrigin<DefId> {
1855+
match tcx.hir_node_by_def_id(def_id).expect_item().expect_opaque_ty().origin {
1856+
hir::OpaqueTyOrigin::FnReturn { parent, in_trait_or_impl } => {
1857+
hir::OpaqueTyOrigin::FnReturn { parent: parent.to_def_id(), in_trait_or_impl }
1858+
}
1859+
hir::OpaqueTyOrigin::AsyncFn { parent, in_trait_or_impl } => {
1860+
hir::OpaqueTyOrigin::AsyncFn { parent: parent.to_def_id(), in_trait_or_impl }
1861+
}
1862+
hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty } => {
1863+
hir::OpaqueTyOrigin::TyAlias { parent: parent.to_def_id(), in_assoc_ty }
18581864
}
1859-
_ => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
18601865
}
18611866
}
18621867

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;
@@ -1795,7 +1796,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
17951796
match path.res {
17961797
Res::Def(DefKind::OpaqueTy, did) => {
17971798
// Check for desugared `impl Trait`.
1798-
assert!(tcx.is_type_alias_impl_trait(did));
1799+
assert_matches!(tcx.opaque_ty_origin(did), hir::OpaqueTyOrigin::TyAlias { .. });
17991800
let item_segment = path.segments.split_last().unwrap();
18001801
let _ = self
18011802
.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
@@ -155,7 +155,10 @@ impl<'tcx> InferCtxt<'tcx> {
155155
// It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
156156
// where 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
@@ -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
@@ -1185,7 +1185,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11851185
| DefKind::SyntheticCoroutineBody => true,
11861186

11871187
DefKind::OpaqueTy => {
1188-
let origin = tcx.opaque_type_origin(def_id);
1188+
let origin = tcx.local_opaque_ty_origin(def_id);
11891189
if let hir::OpaqueTyOrigin::FnReturn { parent, .. }
11901190
| hir::OpaqueTyOrigin::AsyncFn { parent, .. } = origin
11911191
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(parent)
@@ -1524,9 +1524,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15241524
if let DefKind::OpaqueTy = def_kind {
15251525
self.encode_explicit_item_bounds(def_id);
15261526
self.encode_explicit_item_super_predicates(def_id);
1527-
self.tables
1528-
.is_type_alias_impl_trait
1529-
.set(def_id.index, self.tcx.is_type_alias_impl_trait(def_id));
1527+
record!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id));
15301528
self.encode_precise_capturing_args(def_id);
15311529
}
15321530
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
@@ -257,11 +257,10 @@ rustc_queries! {
257257
separate_provide_extern
258258
}
259259

260-
query is_type_alias_impl_trait(key: DefId) -> bool
260+
query opaque_ty_origin(key: DefId) -> hir::OpaqueTyOrigin<DefId>
261261
{
262-
desc { "determine whether the opaque is a type-alias impl trait" }
262+
desc { "determine where the opaque originates from" }
263263
separate_provide_extern
264-
feedable
265264
}
266265

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

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,7 @@ impl<'tcx> TyCtxt<'tcx> {
20742074

20752075
/// Returns the origin of the opaque type `def_id`.
20762076
#[instrument(skip(self), level = "trace", ret)]
2077-
pub fn opaque_type_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin {
2077+
pub fn local_opaque_ty_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin<LocalDefId> {
20782078
self.hir().expect_item(def_id).expect_opaque_ty().origin
20792079
}
20802080
}

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
@@ -2639,7 +2639,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
26392639
obligation: &PredicateObligation<'tcx>,
26402640
def_id: DefId,
26412641
) -> ErrorGuaranteed {
2642-
let name = match self.tcx.opaque_type_origin(def_id.expect_local()) {
2642+
let name = match self.tcx.local_opaque_ty_origin(def_id.expect_local()) {
26432643
hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } => {
26442644
"opaque type".to_string()
26452645
}

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 { .. } => {
@@ -381,7 +379,7 @@ fn associated_type_for_impl_trait_in_trait(
381379
) -> LocalDefId {
382380
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
383381
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. }) =
384-
tcx.opaque_type_origin(opaque_ty_def_id)
382+
tcx.local_opaque_ty_origin(opaque_ty_def_id)
385383
else {
386384
bug!("expected opaque for {opaque_ty_def_id:?}");
387385
};
@@ -419,8 +417,6 @@ fn associated_type_for_impl_trait_in_trait(
419417
// Copy defaultness of the containing function.
420418
trait_assoc_ty.defaultness(tcx.defaultness(fn_def_id));
421419

422-
trait_assoc_ty.is_type_alias_impl_trait(false);
423-
424420
// There are no inferred outlives for the synthesized associated type.
425421
trait_assoc_ty.inferred_outlives_of(&[]);
426422

compiler/rustc_ty_utils/src/opaque_types.rs

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

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

0 commit comments

Comments
 (0)