@@ -8,11 +8,11 @@ use rustc_infer::infer::outlives::env::RegionBoundPairs;
88use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, OpaqueTypeStorageEntries};
99use rustc_infer::traits::ObligationCause;
1010use rustc_macros::extension;
11- use rustc_middle::mir::{Body, ConstraintCategory, DefinitionSiteHiddenTypes };
11+ use rustc_middle::mir::{Body, ConstraintCategory};
1212use rustc_middle::ty::{
13- self, DefiningScopeKind, EarlyBinder , FallibleTypeFolder, GenericArg, GenericArgsRef ,
14- OpaqueHiddenType , OpaqueTypeKey, Region, RegionVid, Ty, TyCtxt, TypeFoldable ,
15- TypeSuperFoldable, TypeVisitableExt, fold_regions,
13+ self, DefiningScopeKind, DefinitionSiteHiddenType , FallibleTypeFolder, GenericArg,
14+ GenericArgsRef , OpaqueTypeKey, ProvisionalHiddenType, Region, RegionVid, Ty, TyCtxt,
15+ TypeFoldable, TypeSuperFoldable, TypeVisitableExt, fold_regions,
1616};
1717use rustc_mir_dataflow::points::DenseLocationMap;
1818use rustc_span::Span;
@@ -48,7 +48,7 @@ pub(crate) enum DeferredOpaqueTypeError<'tcx> {
4848 /// The opaque type.
4949 opaque_type_key: OpaqueTypeKey<'tcx>,
5050 /// The hidden type containing the member region.
51- hidden_type: OpaqueHiddenType <'tcx>,
51+ hidden_type: ProvisionalHiddenType <'tcx>,
5252 /// The unexpected region.
5353 member_region: Region<'tcx>,
5454 },
@@ -67,7 +67,7 @@ pub(crate) fn clone_and_resolve_opaque_types<'tcx>(
6767 infcx: &BorrowckInferCtxt<'tcx>,
6868 universal_region_relations: &Frozen<UniversalRegionRelations<'tcx>>,
6969 constraints: &mut MirTypeckRegionConstraints<'tcx>,
70- ) -> (OpaqueTypeStorageEntries, Vec<(OpaqueTypeKey<'tcx>, OpaqueHiddenType <'tcx>)>) {
70+ ) -> (OpaqueTypeStorageEntries, Vec<(OpaqueTypeKey<'tcx>, ProvisionalHiddenType <'tcx>)>) {
7171 let opaque_types = infcx.clone_opaque_types();
7272 let opaque_types_storage_num_entries = infcx.inner.borrow_mut().opaque_types().num_entries();
7373 let opaque_types = opaque_types
@@ -131,27 +131,26 @@ fn nll_var_to_universal_region<'tcx>(
131131/// and errors if we end up with distinct hidden types.
132132fn 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
@@ -162,7 +161,7 @@ struct DefiningUse<'tcx> {
162161 /// to interact with code outside of `rustc_borrowck`.
163162 opaque_type_key: OpaqueTypeKey<'tcx>,
164163 arg_regions: Vec<RegionVid>,
165- hidden_type: OpaqueHiddenType <'tcx>,
164+ hidden_type: ProvisionalHiddenType <'tcx>,
166165}
167166
168167/// This computes the actual hidden types of the opaque types and maps them to their
@@ -181,8 +180,8 @@ 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>,
185- opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType <'tcx>)],
183+ hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType< 'tcx> >,
184+ opaque_types: &[(OpaqueTypeKey<'tcx>, ProvisionalHiddenType <'tcx>)],
186185) -> Vec<DeferredOpaqueTypeError<'tcx>> {
187186 let mut errors = Vec::new();
188187 // When computing the hidden type we need to track member constraints.
@@ -216,8 +215,8 @@ pub(crate) fn compute_definition_site_hidden_types<'tcx>(
216215#[instrument(level = "debug", skip_all, ret)]
217216fn collect_defining_uses<'tcx>(
218217 rcx: &mut RegionCtxt<'_, 'tcx>,
219- hidden_types: &mut DefinitionSiteHiddenTypes< 'tcx>,
220- opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType <'tcx>)],
218+ hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType< 'tcx> >,
219+ opaque_types: &[(OpaqueTypeKey<'tcx>, ProvisionalHiddenType <'tcx>)],
221220 errors: &mut Vec<DeferredOpaqueTypeError<'tcx>>,
222221) -> Vec<DefiningUse<'tcx>> {
223222 let infcx = rcx.infcx;
@@ -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))]
277276fn 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) {
@@ -303,29 +302,28 @@ fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
303302 hidden_type.span,
304303 "opaque type with non-universal region args",
305304 );
306- ty::OpaqueHiddenType ::new_error(tcx, guar)
305+ ty::ProvisionalHiddenType ::new_error(tcx, guar)
307306 }
308307 };
309308
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>,
499- opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType <'tcx>)],
491+ hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType< 'tcx> >,
492+ opaque_types: &[(OpaqueTypeKey<'tcx>, ProvisionalHiddenType <'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
@@ -676,24 +679,21 @@ impl<'tcx> InferCtxt<'tcx> {
676679 fn infer_opaque_definition_from_instantiation(
677680 &self,
678681 opaque_type_key: OpaqueTypeKey<'tcx>,
679- instantiated_ty: OpaqueHiddenType <'tcx>,
680- ) -> Result<Ty <'tcx>, NonDefiningUseReason<'tcx>> {
682+ instantiated_ty: ProvisionalHiddenType <'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}
0 commit comments