Skip to content

Commit 3a6ab3f

Browse files
committed
split definition and use site hidden tys
1 parent 4146079 commit 3a6ab3f

File tree

14 files changed

+188
-138
lines changed

14 files changed

+188
-138
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub fn provide(providers: &mut Providers) {
119119
fn mir_borrowck(
120120
tcx: TyCtxt<'_>,
121121
def: LocalDefId,
122-
) -> Result<&DefinitionSiteHiddenTypes<'_>, ErrorGuaranteed> {
122+
) -> Result<&FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'_>>, ErrorGuaranteed> {
123123
assert!(!tcx.is_typeck_child(def.to_def_id()));
124124
let (input_body, _) = tcx.mir_promoted(def);
125125
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));
@@ -130,7 +130,7 @@ fn mir_borrowck(
130130
Err(guar)
131131
} else if input_body.should_skip() {
132132
debug!("Skipping borrowck because of injected body");
133-
let opaque_types = DefinitionSiteHiddenTypes(Default::default());
133+
let opaque_types = Default::default();
134134
Ok(tcx.arena.alloc(opaque_types))
135135
} else {
136136
let mut root_cx = BorrowCheckRootCtxt::new(tcx, def, None);

compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use rustc_infer::infer::outlives::env::RegionBoundPairs;
88
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, OpaqueTypeStorageEntries};
99
use rustc_infer::traits::ObligationCause;
1010
use rustc_macros::extension;
11-
use rustc_middle::mir::{Body, ConstraintCategory, DefinitionSiteHiddenTypes};
11+
use rustc_middle::mir::{Body, ConstraintCategory};
1212
use rustc_middle::ty::{
13-
self, DefiningScopeKind, EarlyBinder, FallibleTypeFolder, GenericArg, GenericArgsRef,
14-
OpaqueHiddenType, OpaqueTypeKey, Region, RegionVid, Ty, TyCtxt, TypeFoldable,
13+
self, DefiningScopeKind, DefinitionSiteHiddenType, FallibleTypeFolder, GenericArg,
14+
GenericArgsRef, OpaqueHiddenType, OpaqueTypeKey, Region, RegionVid, Ty, TyCtxt, TypeFoldable,
1515
TypeSuperFoldable, TypeVisitableExt, fold_regions,
1616
};
1717
use rustc_mir_dataflow::points::DenseLocationMap;
@@ -131,27 +131,26 @@ fn nll_var_to_universal_region<'tcx>(
131131
/// and errors if we end up with distinct hidden types.
132132
fn add_hidden_type<'tcx>(
133133
tcx: TyCtxt<'tcx>,
134-
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
134+
hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
135135
def_id: LocalDefId,
136-
hidden_ty: OpaqueHiddenType<'tcx>,
136+
hidden_ty: ty::DefinitionSiteHiddenType<'tcx>,
137137
) {
138138
// Sometimes two opaque types are the same only after we remap the generic parameters
139139
// back to the opaque type definition. E.g. we may have `OpaqueType<X, Y>` mapped to
140140
// `(X, Y)` and `OpaqueType<Y, X>` mapped to `(Y, X)`, and those are the same, but we
141141
// only know that once we convert the generic parameters to those of the opaque type.
142-
if let Some(prev) = hidden_types.0.get_mut(&def_id) {
143-
if prev.ty != hidden_ty.ty {
144-
let guar = hidden_ty.ty.error_reported().err().unwrap_or_else(|| {
145-
let (Ok(e) | Err(e)) = prev.build_mismatch_error(&hidden_ty, tcx).map(|d| d.emit());
146-
e
147-
});
148-
prev.ty = Ty::new_error(tcx, guar);
142+
if let Some(prev) = hidden_types.get_mut(&def_id) {
143+
if prev.ty == hidden_ty.ty {
144+
// Pick a better span if there is one.
145+
// FIXME(oli-obk): collect multiple spans for better diagnostics down the road.
146+
prev.span = prev.span.substitute_dummy(hidden_ty.span);
147+
} else {
148+
let (Ok(guar) | Err(guar)) =
149+
prev.build_mismatch_error(&hidden_ty, tcx).map(|d| d.emit());
150+
*prev = ty::DefinitionSiteHiddenType::new_error(tcx, guar);
149151
}
150-
// Pick a better span if there is one.
151-
// FIXME(oli-obk): collect multiple spans for better diagnostics down the road.
152-
prev.span = prev.span.substitute_dummy(hidden_ty.span);
153152
} else {
154-
hidden_types.0.insert(def_id, hidden_ty);
153+
hidden_types.insert(def_id, hidden_ty);
155154
}
156155
}
157156

@@ -181,7 +180,7 @@ pub(crate) fn compute_definition_site_hidden_types<'tcx>(
181180
universal_region_relations: &Frozen<UniversalRegionRelations<'tcx>>,
182181
constraints: &MirTypeckRegionConstraints<'tcx>,
183182
location_map: Rc<DenseLocationMap>,
184-
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
183+
hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
185184
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
186185
) -> Vec<DeferredOpaqueTypeError<'tcx>> {
187186
let mut errors = Vec::new();
@@ -216,7 +215,7 @@ pub(crate) fn compute_definition_site_hidden_types<'tcx>(
216215
#[instrument(level = "debug", skip_all, ret)]
217216
fn collect_defining_uses<'tcx>(
218217
rcx: &mut RegionCtxt<'_, 'tcx>,
219-
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
218+
hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
220219
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
221220
errors: &mut Vec<DeferredOpaqueTypeError<'tcx>>,
222221
) -> Vec<DefiningUse<'tcx>> {
@@ -240,7 +239,7 @@ fn collect_defining_uses<'tcx>(
240239
infcx.tcx,
241240
hidden_types,
242241
opaque_type_key.def_id,
243-
OpaqueHiddenType::new_error(infcx.tcx, guar),
242+
DefinitionSiteHiddenType::new_error(infcx.tcx, guar),
244243
),
245244
_ => debug!(?non_nll_opaque_type_key, ?err, "ignoring non-defining use"),
246245
}
@@ -276,7 +275,7 @@ fn collect_defining_uses<'tcx>(
276275
#[instrument(level = "debug", skip(rcx, hidden_types, defining_uses, errors))]
277276
fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
278277
rcx: &RegionCtxt<'_, 'tcx>,
279-
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
278+
hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
280279
defining_uses: &[DefiningUse<'tcx>],
281280
errors: &mut Vec<DeferredOpaqueTypeError<'tcx>>,
282281
) {
@@ -310,22 +309,21 @@ fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
310309
// Now that we mapped the member regions to their final value,
311310
// map the arguments of the opaque type key back to the parameters
312311
// of the opaque type definition.
313-
let ty = infcx
312+
let hidden_type = infcx
314313
.infer_opaque_definition_from_instantiation(opaque_type_key, hidden_type)
315314
.unwrap_or_else(|_| {
316-
Ty::new_error_with_message(
317-
rcx.infcx.tcx,
318-
hidden_type.span,
319-
"deferred invalid opaque type args",
320-
)
315+
let guar = tcx
316+
.dcx()
317+
.span_delayed_bug(hidden_type.span, "deferred invalid opaque type args");
318+
DefinitionSiteHiddenType::new_error(tcx, guar)
321319
});
322320

323321
// Sometimes, when the hidden type is an inference variable, it can happen that
324322
// the hidden type becomes the opaque type itself. In this case, this was an opaque
325323
// usage of the opaque type and we can ignore it. This check is mirrored in typeck's
326324
// writeback.
327325
if !rcx.infcx.tcx.use_typing_mode_borrowck() {
328-
if let ty::Alias(ty::Opaque, alias_ty) = ty.kind()
326+
if let ty::Alias(ty::Opaque, alias_ty) = hidden_type.ty.skip_binder().kind()
329327
&& alias_ty.def_id == opaque_type_key.def_id.to_def_id()
330328
&& alias_ty.args == opaque_type_key.args
331329
{
@@ -357,12 +355,7 @@ fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
357355
},
358356
));
359357
}
360-
add_hidden_type(
361-
tcx,
362-
hidden_types,
363-
opaque_type_key.def_id,
364-
OpaqueHiddenType { span: hidden_type.span, ty },
365-
);
358+
add_hidden_type(tcx, hidden_types, opaque_type_key.def_id, hidden_type);
366359
}
367360
}
368361

@@ -495,14 +488,13 @@ pub(crate) fn apply_definition_site_hidden_types<'tcx>(
495488
region_bound_pairs: &RegionBoundPairs<'tcx>,
496489
known_type_outlives_obligations: &[ty::PolyTypeOutlivesPredicate<'tcx>],
497490
constraints: &mut MirTypeckRegionConstraints<'tcx>,
498-
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
491+
hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
499492
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
500493
) -> Vec<DeferredOpaqueTypeError<'tcx>> {
501494
let tcx = infcx.tcx;
502495
let mut errors = Vec::new();
503496
for &(key, hidden_type) in opaque_types {
504-
let Some(expected) = hidden_types.0.get(&key.def_id).map(|ty| EarlyBinder::bind(*ty))
505-
else {
497+
let Some(expected) = hidden_types.get(&key.def_id) else {
506498
if !tcx.use_typing_mode_borrowck() {
507499
if let ty::Alias(ty::Opaque, alias_ty) = hidden_type.ty.kind()
508500
&& alias_ty.def_id == key.def_id.to_def_id()
@@ -521,20 +513,26 @@ pub(crate) fn apply_definition_site_hidden_types<'tcx>(
521513
hidden_type.span,
522514
"non-defining use in the defining scope with no defining uses",
523515
);
524-
add_hidden_type(tcx, hidden_types, key.def_id, OpaqueHiddenType::new_error(tcx, guar));
516+
add_hidden_type(
517+
tcx,
518+
hidden_types,
519+
key.def_id,
520+
DefinitionSiteHiddenType::new_error(tcx, guar),
521+
);
525522
continue;
526523
};
527524

528525
// We erase all non-member region of the opaque and need to treat these as existentials.
529-
let expected = ty::fold_regions(tcx, expected.instantiate(tcx, key.args), |re, _dbi| {
530-
match re.kind() {
531-
ty::ReErased => infcx.next_nll_region_var(
532-
NllRegionVariableOrigin::Existential { name: None },
533-
|| crate::RegionCtxt::Existential(None),
534-
),
535-
_ => re,
536-
}
537-
});
526+
let expected_ty =
527+
ty::fold_regions(tcx, expected.ty.instantiate(tcx, key.args), |re, _dbi| {
528+
match re.kind() {
529+
ty::ReErased => infcx.next_nll_region_var(
530+
NllRegionVariableOrigin::Existential { name: None },
531+
|| crate::RegionCtxt::Existential(None),
532+
),
533+
_ => re,
534+
}
535+
});
538536

539537
// We now simply equate the expected with the actual hidden type.
540538
let locations = Locations::All(hidden_type.span);
@@ -555,13 +553,18 @@ pub(crate) fn apply_definition_site_hidden_types<'tcx>(
555553
);
556554
// We need to normalize both types in the old solver before equatingt them.
557555
let actual_ty = ocx.normalize(&cause, infcx.param_env, hidden_type.ty);
558-
let expected_ty = ocx.normalize(&cause, infcx.param_env, expected.ty);
556+
let expected_ty = ocx.normalize(&cause, infcx.param_env, expected_ty);
559557
ocx.eq(&cause, infcx.param_env, actual_ty, expected_ty).map_err(|_| NoSolution)
560558
},
561559
"equating opaque types",
562560
),
563561
) {
564-
add_hidden_type(tcx, hidden_types, key.def_id, OpaqueHiddenType::new_error(tcx, guar));
562+
add_hidden_type(
563+
tcx,
564+
hidden_types,
565+
key.def_id,
566+
DefinitionSiteHiddenType::new_error(tcx, guar),
567+
);
565568
}
566569
}
567570
errors
@@ -677,23 +680,20 @@ impl<'tcx> InferCtxt<'tcx> {
677680
&self,
678681
opaque_type_key: OpaqueTypeKey<'tcx>,
679682
instantiated_ty: OpaqueHiddenType<'tcx>,
680-
) -> Result<Ty<'tcx>, NonDefiningUseReason<'tcx>> {
683+
) -> Result<ty::DefinitionSiteHiddenType<'tcx>, NonDefiningUseReason<'tcx>> {
681684
opaque_type_has_defining_use_args(
682685
self,
683686
opaque_type_key,
684687
instantiated_ty.span,
685688
DefiningScopeKind::MirBorrowck,
686689
)?;
687690

688-
let definition_ty = instantiated_ty
689-
.remap_generic_params_to_declaration_params(
690-
opaque_type_key,
691-
self.tcx,
692-
DefiningScopeKind::MirBorrowck,
693-
)
694-
.ty;
695-
696-
definition_ty.error_reported()?;
691+
let definition_ty = instantiated_ty.remap_generic_params_to_declaration_params(
692+
opaque_type_key,
693+
self.tcx,
694+
DefiningScopeKind::MirBorrowck,
695+
);
696+
definition_ty.ty.skip_binder().error_reported()?;
697697
Ok(definition_ty)
698698
}
699699
}

compiler/rustc_borrowck/src/root_cx.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ use crate::region_infer::opaque_types::{
1717
};
1818
use crate::type_check::{Locations, constraint_conversion};
1919
use crate::{
20-
ClosureRegionRequirements, CollectRegionConstraintsResult, DefinitionSiteHiddenTypes,
21-
PropagatedBorrowCheckResults, borrowck_check_region_constraints,
22-
borrowck_collect_region_constraints,
20+
ClosureRegionRequirements, CollectRegionConstraintsResult, PropagatedBorrowCheckResults,
21+
borrowck_check_region_constraints, borrowck_collect_region_constraints,
2322
};
2423

2524
/// The shared context used by both the root as well as all its nested
2625
/// items.
2726
pub(super) struct BorrowCheckRootCtxt<'tcx> {
2827
pub tcx: TyCtxt<'tcx>,
2928
root_def_id: LocalDefId,
30-
hidden_types: DefinitionSiteHiddenTypes<'tcx>,
29+
hidden_types: FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
3130
/// The region constraints computed by [borrowck_collect_region_constraints]. This uses
3231
/// an [FxIndexMap] to guarantee that iterating over it visits nested bodies before
3332
/// their parents.
@@ -72,7 +71,10 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
7271
&self.propagated_borrowck_results[&nested_body_def_id].used_mut_upvars
7372
}
7473

75-
pub(super) fn finalize(self) -> Result<&'tcx DefinitionSiteHiddenTypes<'tcx>, ErrorGuaranteed> {
74+
pub(super) fn finalize(
75+
self,
76+
) -> Result<&'tcx FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>, ErrorGuaranteed>
77+
{
7678
if let Some(guar) = self.tainted_by_errors {
7779
Err(guar)
7880
} else {

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ fn best_definition_site_of_opaque<'tcx>(
430430
.tcx
431431
.mir_borrowck(item_def_id)
432432
.ok()
433-
.and_then(|opaque_types| opaque_types.0.get(&self.opaque_def_id))
433+
.and_then(|opaque_types| opaque_types.get(&self.opaque_def_id))
434434
{
435435
ControlFlow::Break((hidden_ty.span, item_def_id))
436436
} else {

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pub(super) fn type_of_opaque(
345345
def_id: DefId,
346346
) -> Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
347347
if let Some(def_id) = def_id.as_local() {
348-
Ok(ty::EarlyBinder::bind(match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
348+
Ok(match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
349349
hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: false, .. } => {
350350
opaque::find_opaque_ty_constraints_for_tait(
351351
tcx,
@@ -378,7 +378,7 @@ pub(super) fn type_of_opaque(
378378
DefiningScopeKind::MirBorrowck,
379379
)
380380
}
381-
}))
381+
})
382382
} else {
383383
// Foreign opaque type will go through the foreign provider
384384
// and load the type from metadata.
@@ -390,7 +390,7 @@ pub(super) fn type_of_opaque_hir_typeck(
390390
tcx: TyCtxt<'_>,
391391
def_id: LocalDefId,
392392
) -> ty::EarlyBinder<'_, Ty<'_>> {
393-
ty::EarlyBinder::bind(match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
393+
match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
394394
hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: false, .. } => {
395395
opaque::find_opaque_ty_constraints_for_tait(tcx, def_id, DefiningScopeKind::HirTypeck)
396396
}
@@ -419,7 +419,7 @@ pub(super) fn type_of_opaque_hir_typeck(
419419
DefiningScopeKind::HirTypeck,
420420
)
421421
}
422-
})
422+
}
423423
}
424424

425425
fn infer_placeholder_type<'tcx>(

0 commit comments

Comments
 (0)