Skip to content

Commit 2278365

Browse files
committed
recompute opaque type origin
1 parent dcc9028 commit 2278365

File tree

4 files changed

+22
-38
lines changed

4 files changed

+22
-38
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
6161
pub(crate) fn infer_opaque_types(
6262
&self,
6363
infcx: &InferCtxt<'tcx>,
64-
opaque_ty_decls: FxIndexMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
64+
opaque_ty_decls: FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
6565
) -> FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>> {
6666
let mut result: FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>> = FxIndexMap::default();
6767

@@ -72,7 +72,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
7272
.collect();
7373
debug!(?member_constraints);
7474

75-
for (opaque_type_key, (concrete_type, origin)) in opaque_ty_decls {
75+
for (opaque_type_key, concrete_type) in opaque_ty_decls {
7676
let substs = opaque_type_key.substs;
7777
debug!(?concrete_type, ?substs);
7878

@@ -143,7 +143,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
143143
let ty = infcx.infer_opaque_definition_from_instantiation(
144144
opaque_type_key,
145145
universal_concrete_type,
146-
origin,
147146
);
148147
// Sometimes two opaque types are the same only after we remap the generic parameters
149148
// back to the opaque type definition. E.g. we may have `OpaqueType<X, Y>` mapped to `(X, Y)`
@@ -215,7 +214,6 @@ pub trait InferCtxtExt<'tcx> {
215214
&self,
216215
opaque_type_key: OpaqueTypeKey<'tcx>,
217216
instantiated_ty: OpaqueHiddenType<'tcx>,
218-
origin: OpaqueTyOrigin,
219217
) -> Ty<'tcx>;
220218
}
221219

@@ -248,7 +246,6 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
248246
&self,
249247
opaque_type_key: OpaqueTypeKey<'tcx>,
250248
instantiated_ty: OpaqueHiddenType<'tcx>,
251-
origin: OpaqueTyOrigin,
252249
) -> Ty<'tcx> {
253250
if let Some(e) = self.tainted_by_errors() {
254251
return self.tcx.ty_error(e);
@@ -258,18 +255,16 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
258255
.remap_generic_params_to_declaration_params(opaque_type_key, self.tcx, false)
259256
.ty;
260257

261-
if let Err(guar) = check_opaque_type_parameter_valid(
262-
self.tcx,
263-
opaque_type_key,
264-
origin,
265-
instantiated_ty.span,
266-
) {
258+
if let Err(guar) =
259+
check_opaque_type_parameter_valid(self.tcx, opaque_type_key, instantiated_ty.span)
260+
{
267261
return self.tcx.ty_error(guar);
268262
}
269263

270264
// Only check this for TAIT. RPIT already supports `tests/ui/impl-trait/nested-return-type2.rs`
271265
// on stable and we'd break that.
272-
let OpaqueTyOrigin::TyAlias { .. } = origin else {
266+
let opaque_ty_hir = self.tcx.hir().expect_item(opaque_type_key.def_id);
267+
let OpaqueTyOrigin::TyAlias { .. } = opaque_ty_hir.expect_opaque_ty().origin else {
273268
return definition_ty;
274269
};
275270
let def_id = opaque_type_key.def_id;
@@ -347,10 +342,10 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
347342
fn check_opaque_type_parameter_valid(
348343
tcx: TyCtxt<'_>,
349344
opaque_type_key: OpaqueTypeKey<'_>,
350-
origin: OpaqueTyOrigin,
351345
span: Span,
352346
) -> Result<(), ErrorGuaranteed> {
353-
match origin {
347+
let opaque_ty_hir = tcx.hir().expect_item(opaque_type_key.def_id);
348+
match opaque_ty_hir.expect_opaque_ty().origin {
354349
// No need to check return position impl trait (RPIT)
355350
// because for type and const parameters they are correct
356351
// by construction: we convert

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{fmt, iter, mem};
77

88
use either::Either;
99

10-
use hir::OpaqueTyOrigin;
1110
use rustc_data_structures::frozen::Frozen;
1211
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
1312
use rustc_errors::ErrorGuaranteed;
@@ -241,7 +240,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
241240
hidden_type.ty = infcx.tcx.ty_error(reported);
242241
}
243242

244-
(opaque_type_key, (hidden_type, decl.origin))
243+
(opaque_type_key, hidden_type)
245244
})
246245
.collect();
247246

@@ -878,8 +877,7 @@ struct BorrowCheckContext<'a, 'tcx> {
878877
pub(crate) struct MirTypeckResults<'tcx> {
879878
pub(crate) constraints: MirTypeckRegionConstraints<'tcx>,
880879
pub(crate) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
881-
pub(crate) opaque_type_values:
882-
FxIndexMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
880+
pub(crate) opaque_type_values: FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
883881
}
884882

885883
/// A collection of region constraints that must be satisfied for the

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ pub struct OpaqueTypeDecl<'tcx> {
3333
/// There can be multiple, but they are all `lub`ed together at the end
3434
/// to obtain the canonical hidden type.
3535
pub hidden_type: OpaqueHiddenType<'tcx>,
36-
37-
/// The origin of the opaque type.
38-
pub origin: hir::OpaqueTyOrigin,
3936
}
4037

4138
impl<'tcx> InferCtxt<'tcx> {
@@ -108,7 +105,7 @@ impl<'tcx> InferCtxt<'tcx> {
108105
let process = |a: Ty<'tcx>, b: Ty<'tcx>, a_is_expected| match *a.kind() {
109106
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) if def_id.is_local() => {
110107
let def_id = def_id.expect_local();
111-
let origin = match self.defining_use_anchor {
108+
match self.defining_use_anchor {
112109
DefiningAnchor::Bind(_) => {
113110
// Check that this is `impl Trait` type is
114111
// declared by `parent_def_id` -- i.e., one whose
@@ -144,9 +141,11 @@ impl<'tcx> InferCtxt<'tcx> {
144141
// let x = || foo(); // returns the Opaque assoc with `foo`
145142
// }
146143
// ```
147-
self.opaque_type_origin(def_id)?
144+
if self.opaque_type_origin(def_id).is_none() {
145+
return None;
146+
}
148147
}
149-
DefiningAnchor::Bubble => self.opaque_type_origin_unchecked(def_id),
148+
DefiningAnchor::Bubble => {}
150149
DefiningAnchor::Error => return None,
151150
};
152151
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() {
@@ -170,7 +169,6 @@ impl<'tcx> InferCtxt<'tcx> {
170169
cause.clone(),
171170
param_env,
172171
b,
173-
origin,
174172
a_is_expected,
175173
))
176174
}
@@ -524,7 +522,6 @@ impl<'tcx> InferCtxt<'tcx> {
524522
cause: ObligationCause<'tcx>,
525523
param_env: ty::ParamEnv<'tcx>,
526524
hidden_ty: Ty<'tcx>,
527-
origin: hir::OpaqueTyOrigin,
528525
a_is_expected: bool,
529526
) -> InferResult<'tcx, ()> {
530527
// Ideally, we'd get the span where *this specific `ty` came
@@ -544,11 +541,11 @@ impl<'tcx> InferCtxt<'tcx> {
544541
ty::PredicateKind::Ambiguous,
545542
)]
546543
} else {
547-
let prev = self.inner.borrow_mut().opaque_types().register(
548-
opaque_type_key,
549-
OpaqueHiddenType { ty: hidden_ty, span },
550-
origin,
551-
);
544+
let prev = self
545+
.inner
546+
.borrow_mut()
547+
.opaque_types()
548+
.register(opaque_type_key, OpaqueHiddenType { ty: hidden_ty, span });
552549
if let Some(prev) = prev {
553550
self.at(&cause, param_env)
554551
.eq_exp(DefineOpaqueTypes::Yes, a_is_expected, prev, hidden_ty)?
@@ -579,15 +576,11 @@ impl<'tcx> InferCtxt<'tcx> {
579576
hidden_ty: Ty<'tcx>,
580577
) -> InferResult<'tcx, ()> {
581578
assert!(self.next_trait_solver());
582-
let origin = self
583-
.opaque_type_origin(opaque_type_key.def_id)
584-
.expect("should be called for defining usages only");
585579
self.register_hidden_type(
586580
opaque_type_key,
587581
ObligationCause::dummy(),
588582
param_env,
589583
hidden_ty,
590-
origin,
591584
true,
592585
)
593586
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_data_structures::undo_log::UndoLogs;
2-
use rustc_hir::OpaqueTyOrigin;
32
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty};
43
use rustc_span::DUMMY_SP;
54

@@ -60,14 +59,13 @@ impl<'a, 'tcx> OpaqueTypeTable<'a, 'tcx> {
6059
&mut self,
6160
key: OpaqueTypeKey<'tcx>,
6261
hidden_type: OpaqueHiddenType<'tcx>,
63-
origin: OpaqueTyOrigin,
6462
) -> Option<Ty<'tcx>> {
6563
if let Some(decl) = self.storage.opaque_types.get_mut(&key) {
6664
let prev = std::mem::replace(&mut decl.hidden_type, hidden_type);
6765
self.undo_log.push(UndoLog::OpaqueTypes(key, Some(prev)));
6866
return Some(prev.ty);
6967
}
70-
let decl = OpaqueTypeDecl { hidden_type, origin };
68+
let decl = OpaqueTypeDecl { hidden_type };
7169
self.storage.opaque_types.insert(key, decl);
7270
self.undo_log.push(UndoLog::OpaqueTypes(key, None));
7371
None

0 commit comments

Comments
 (0)