Skip to content

Commit 0ccc39d

Browse files
Encode cross-crate opaque type origin
1 parent 45089ec commit 0ccc39d

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
@@ -279,7 +279,7 @@ enum ImplTraitContext {
279279
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
280280
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
281281
///
282-
OpaqueTy { origin: hir::OpaqueTyOrigin },
282+
OpaqueTy { origin: hir::OpaqueTyOrigin<LocalDefId> },
283283
/// `impl Trait` is unstably accepted in this position.
284284
FeatureGated(ImplTraitPosition, Symbol),
285285
/// `impl Trait` is not accepted in this position.
@@ -1474,7 +1474,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14741474
fn lower_opaque_impl_trait(
14751475
&mut self,
14761476
span: Span,
1477-
origin: hir::OpaqueTyOrigin,
1477+
origin: hir::OpaqueTyOrigin<LocalDefId>,
14781478
opaque_ty_node_id: NodeId,
14791479
bounds: &GenericBounds,
14801480
itctx: ImplTraitContext,
@@ -1542,7 +1542,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15421542
fn lower_opaque_inner(
15431543
&mut self,
15441544
opaque_ty_node_id: NodeId,
1545-
origin: hir::OpaqueTyOrigin,
1545+
origin: hir::OpaqueTyOrigin<LocalDefId>,
15461546
captured_lifetimes_to_duplicate: FxIndexSet<Lifetime>,
15471547
span: Span,
15481548
opaque_ty_span: Span,

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
@@ -2748,7 +2748,7 @@ pub struct OpaqueTy<'hir> {
27482748
pub def_id: LocalDefId,
27492749
pub generics: &'hir Generics<'hir>,
27502750
pub bounds: GenericBounds<'hir>,
2751-
pub origin: OpaqueTyOrigin,
2751+
pub origin: OpaqueTyOrigin<LocalDefId>,
27522752
/// Return-position impl traits (and async futures) must "reify" any late-bound
27532753
/// lifetimes that are captured from the function signature they originate from.
27542754
///
@@ -2796,33 +2796,35 @@ pub struct PreciseCapturingNonLifetimeArg {
27962796
pub res: Res,
27972797
}
27982798

2799-
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
2799+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2800+
#[derive(HashStable_Generic, Encodable, Decodable)]
28002801
pub enum RpitContext {
28012802
Trait,
28022803
TraitImpl,
28032804
}
28042805

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

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn check_opaque_meets_bounds<'tcx>(
267267
tcx: TyCtxt<'tcx>,
268268
def_id: LocalDefId,
269269
span: Span,
270-
origin: &hir::OpaqueTyOrigin,
270+
origin: &hir::OpaqueTyOrigin<LocalDefId>,
271271
) -> Result<(), ErrorGuaranteed> {
272272
let defining_use_anchor = match *origin {
273273
hir::OpaqueTyOrigin::FnReturn { parent, .. }
@@ -675,7 +675,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
675675
DefKind::OpaqueTy => {
676676
check_opaque_precise_captures(tcx, def_id);
677677

678-
let origin = tcx.opaque_type_origin(def_id);
678+
let origin = tcx.local_opaque_ty_origin(def_id);
679679
if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
680680
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. } = origin
681681
&& 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
};
@@ -1757,9 +1757,18 @@ fn coroutine_for_closure(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DefId {
17571757
def_id.to_def_id()
17581758
}
17591759

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

17651774
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;
@@ -1807,7 +1808,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18071808
match path.res {
18081809
Res::Def(DefKind::OpaqueTy, did) => {
18091810
// Check for desugared `impl Trait`.
1810-
assert!(tcx.is_type_alias_impl_trait(did));
1811+
assert_matches!(tcx.opaque_ty_origin(did), hir::OpaqueTyOrigin::TyAlias { .. });
18111812
let item_segment = path.segments.split_last().unwrap();
18121813
let _ = self
18131814
.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
@@ -317,10 +317,7 @@ provide! { tcx, def_id, other, cdata,
317317
})
318318
.unwrap_or_default()
319319
}
320-
is_type_alias_impl_trait => {
321-
debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
322-
cdata.root.tables.is_type_alias_impl_trait.get(cdata, def_id.index)
323-
}
320+
opaque_ty_origin => { table }
324321
assumed_wf_types_for_rpitit => { table }
325322
collect_return_position_impl_trait_in_trait_tys => {
326323
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
@@ -259,11 +259,10 @@ rustc_queries! {
259259
separate_provide_extern
260260
}
261261

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

269268
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
@@ -2109,11 +2109,9 @@ impl<'tcx> TyCtxt<'tcx> {
21092109
}
21102110

21112111
/// Returns the origin of the opaque type `def_id`.
2112-
#[track_caller]
2113-
pub fn opaque_type_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin {
2114-
let origin = self.hir().expect_opaque_ty(def_id).origin;
2115-
trace!("opaque_type_origin({def_id:?}) => {origin:?}");
2116-
origin
2112+
#[instrument(skip(self), level = "trace", ret)]
2113+
pub fn local_opaque_ty_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin<LocalDefId> {
2114+
self.hir().expect_opaque_ty(def_id).origin
21172115
}
21182116
}
21192117

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

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)