Skip to content

Commit 8d92a6d

Browse files
committed
rarw
1 parent 446566a commit 8d92a6d

File tree

3 files changed

+136
-173
lines changed

3 files changed

+136
-173
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 59 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use std::iter;
22
use std::rc::Rc;
33

4-
use rustc_data_structures::frozen::Frozen;
54
use rustc_data_structures::fx::FxHashMap;
65
use rustc_hir::def_id::DefId;
76
use rustc_index::IndexVec;
7+
use rustc_infer::infer::outlives::env::RegionBoundPairs;
88
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
9+
use rustc_infer::traits::ObligationCause;
910
use rustc_macros::extension;
1011
use rustc_middle::bug;
11-
use rustc_middle::mir::ConstraintCategory;
12-
use rustc_middle::ty::relate::{
13-
Relate, RelateResult, TypeRelation, structurally_relate_consts, structurally_relate_tys,
14-
};
12+
use rustc_middle::mir::{Body, ConstraintCategory};
1513
use rustc_middle::ty::{
1614
self, DefiningScopeKind, GenericArgsRef, OpaqueHiddenType, OpaqueTypeKey, Region, RegionVid,
1715
Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable,
@@ -23,13 +21,16 @@ use rustc_trait_selection::error_reporting::infer::region::unexpected_hidden_reg
2321
use rustc_trait_selection::opaque_types::{
2422
InvalidOpaqueTypeArgs, check_opaque_type_parameter_valid,
2523
};
24+
use rustc_trait_selection::solve::NoSolution;
25+
use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
2626
use tracing::{debug, instrument};
2727

2828
use super::reverse_sccs::ReverseSccGraph;
2929
use super::values::RegionValues;
3030
use super::{ConstraintSccs, RegionDefinition, RegionInferenceContext};
3131
use crate::constraints::ConstraintSccIndex;
3232
use crate::consumers::OutlivesConstraint;
33+
use crate::type_check::canonical::fully_perform_op_raw;
3334
use crate::type_check::free_region_relations::UniversalRegionRelations;
3435
use crate::type_check::{Locations, MirTypeckRegionConstraints};
3536
use crate::universal_regions::{RegionClassification, UniversalRegions};
@@ -52,8 +53,11 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
5253
pub(crate) fn handle_opaque_type_uses(&mut self, borrowck_state: &mut BorrowckState<'tcx>) {
5354
let BorrowckState {
5455
infcx,
55-
constraints,
56+
body_owned,
5657
universal_region_relations,
58+
region_bound_pairs,
59+
known_type_outlives_obligations,
60+
constraints,
5761
location_map,
5862
deferred_opaque_type_errors,
5963
..
@@ -62,10 +66,13 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
6266
handle_opaque_type_uses(
6367
self,
6468
infcx,
65-
constraints,
66-
deferred_opaque_type_errors,
69+
body_owned,
6770
universal_region_relations,
71+
region_bound_pairs,
72+
known_type_outlives_obligations,
6873
location_map,
74+
constraints,
75+
deferred_opaque_type_errors,
6976
)
7077
}
7178
}
@@ -82,7 +89,7 @@ struct RegionCtxt<'a, 'tcx> {
8289
impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
8390
fn new(
8491
infcx: &'a BorrowckInferCtxt<'tcx>,
85-
universal_region_relations: &'a Frozen<UniversalRegionRelations<'tcx>>,
92+
universal_region_relations: &'a UniversalRegionRelations<'tcx>,
8693
location_map: Rc<DenseLocationMap>,
8794
constraints: &MirTypeckRegionConstraints<'tcx>,
8895
) -> RegionCtxt<'a, 'tcx> {
@@ -160,10 +167,13 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
160167
pub(crate) fn handle_opaque_type_uses<'tcx>(
161168
root_cx: &mut BorrowCheckRootCtxt<'tcx>,
162169
infcx: &BorrowckInferCtxt<'tcx>,
170+
body: &Body<'tcx>,
171+
universal_region_relations: &UniversalRegionRelations<'tcx>,
172+
region_bound_pairs: &RegionBoundPairs<'tcx>,
173+
known_type_outlives_obligations: &[ty::PolyTypeOutlivesPredicate<'tcx>],
174+
location_map: &Rc<DenseLocationMap>,
163175
constraints: &mut MirTypeckRegionConstraints<'tcx>,
164176
deferred_errors: &mut Vec<DeferredOpaqueTypeError<'tcx>>,
165-
universal_region_relations: &Frozen<UniversalRegionRelations<'tcx>>,
166-
location_map: &Rc<DenseLocationMap>,
167177
) {
168178
let opaque_types = infcx.take_opaque_types();
169179
if opaque_types.is_empty() {
@@ -197,7 +207,16 @@ pub(crate) fn handle_opaque_type_uses<'tcx>(
197207
&mut opaque_types,
198208
);
199209

200-
apply_defining_uses(root_cx, infcx, constraints, universal_region_relations, &opaque_types);
210+
apply_defining_uses(
211+
root_cx,
212+
infcx,
213+
body,
214+
&universal_region_relations.universal_regions,
215+
region_bound_pairs,
216+
known_type_outlives_obligations,
217+
constraints,
218+
&opaque_types,
219+
);
201220
}
202221

203222
#[derive(Debug)]
@@ -212,7 +231,7 @@ fn collect_defining_uses<'tcx>(
212231
infcx: &BorrowckInferCtxt<'tcx>,
213232
constraints: &mut MirTypeckRegionConstraints<'tcx>,
214233
deferred_errors: &mut Vec<DeferredOpaqueTypeError<'tcx>>,
215-
universal_region_relations: &Frozen<UniversalRegionRelations<'tcx>>,
234+
universal_region_relations: &UniversalRegionRelations<'tcx>,
216235
location_map: &Rc<DenseLocationMap>,
217236
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
218237
) {
@@ -546,8 +565,11 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for HiddenTypeMeh<'_, 'tcx> {
546565
fn apply_defining_uses<'tcx>(
547566
root_cx: &mut BorrowCheckRootCtxt<'tcx>,
548567
infcx: &BorrowckInferCtxt<'tcx>,
568+
body: &Body<'tcx>,
569+
universal_regions: &UniversalRegions<'tcx>,
570+
region_bound_pairs: &RegionBoundPairs<'tcx>,
571+
known_type_outlives_obligations: &[ty::PolyTypeOutlivesPredicate<'tcx>],
549572
constraints: &mut MirTypeckRegionConstraints<'tcx>,
550-
universal_region_relations: &Frozen<UniversalRegionRelations<'tcx>>,
551573
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
552574
) {
553575
let tcx = infcx.tcx;
@@ -570,122 +592,30 @@ fn apply_defining_uses<'tcx>(
570592
}
571593
});
572594

573-
let universal_regions = &universal_region_relations.universal_regions;
574-
let mut relation =
575-
EquateRegions { infcx, span: hidden_type.span, universal_regions, constraints };
576-
match TypeRelation::relate(&mut relation, hidden_type.ty, expected.ty) {
577-
Ok(_) => {}
578-
Err(_) => {
579-
let _ = hidden_type.build_mismatch_error(&expected, tcx).map(|d| d.emit());
580-
}
581-
}
582-
583-
// normalize -> equate
584-
//
585-
// Do we need to normalize?
586-
// Only to support non-universal type or const args. It feels likely that we need (and want) to do so
587-
// This once again needs to be careful about cycles: normalizing and equating while defining/revealing
588-
// opaque types may end up introducing new defining uses.
589-
590-
// How can we equate here?
591-
// If we need to normalize the answer is just "it's TypeChecker time"
592-
// without it we could manually walk over the types using a type relation and equate region vars
593-
// that way.
594-
}
595-
}
596-
597-
fn to_region_vid<'tcx>(
598-
constraints: &MirTypeckRegionConstraints<'tcx>,
599-
universal_regions: &UniversalRegions<'tcx>,
600-
r: Region<'tcx>,
601-
) -> RegionVid {
602-
if let ty::RePlaceholder(placeholder) = r.kind() {
603-
constraints.get_placeholder_region(placeholder).as_var()
604-
} else {
605-
universal_regions.to_region_vid(r)
606-
}
607-
}
608-
609-
// FUCKING SHIT NO!wegrdtfhergfsg
610-
struct EquateRegions<'a, 'tcx> {
611-
infcx: &'a BorrowckInferCtxt<'tcx>,
612-
span: Span,
613-
universal_regions: &'a UniversalRegions<'tcx>,
614-
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
615-
}
616-
617-
impl<'tcx> TypeRelation<TyCtxt<'tcx>> for EquateRegions<'_, 'tcx> {
618-
fn cx(&self) -> TyCtxt<'tcx> {
619-
self.infcx.tcx
620-
}
621-
622-
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
623-
&mut self,
624-
_variance: ty::Variance,
625-
_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
626-
a: T,
627-
b: T,
628-
) -> RelateResult<'tcx, T> {
629-
self.relate(a, b)
630-
}
631-
632-
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
633-
structurally_relate_tys(self, a, b)
634-
}
635-
636-
fn regions(
637-
&mut self,
638-
a: ty::Region<'tcx>,
639-
b: ty::Region<'tcx>,
640-
) -> RelateResult<'tcx, ty::Region<'tcx>> {
641-
if matches!(a.kind(), ty::ReBound(..)) || matches!(b.kind(), ty::ReBound(..)) {
642-
assert_eq!(a, b);
643-
return Ok(a);
644-
}
645-
646-
let a_vid = to_region_vid(self.constraints, self.universal_regions, a);
647-
let b_vid = to_region_vid(self.constraints, self.universal_regions, b);
648-
let locations = Locations::All(self.span);
649-
self.constraints.outlives_constraints.push(OutlivesConstraint {
650-
sup: a_vid,
651-
sub: b_vid,
652-
locations,
653-
span: self.span,
654-
category: ConstraintCategory::OpaqueType,
655-
variance_info: ty::VarianceDiagInfo::None,
656-
from_closure: false,
657-
});
658-
self.constraints.outlives_constraints.push(OutlivesConstraint {
659-
sup: b_vid,
660-
sub: a_vid,
595+
let locations = Locations::All(hidden_type.span);
596+
fully_perform_op_raw(
597+
infcx,
598+
body,
599+
universal_regions,
600+
region_bound_pairs,
601+
known_type_outlives_obligations,
602+
constraints,
661603
locations,
662-
span: self.span,
663-
category: ConstraintCategory::OpaqueType,
664-
variance_info: ty::VarianceDiagInfo::None,
665-
from_closure: false,
666-
});
667-
668-
Ok(a)
669-
}
670-
671-
fn consts(
672-
&mut self,
673-
a: ty::Const<'tcx>,
674-
b: ty::Const<'tcx>,
675-
) -> RelateResult<'tcx, ty::Const<'tcx>> {
676-
structurally_relate_consts(self, a, b)
677-
}
678-
679-
fn binders<T>(
680-
&mut self,
681-
a: ty::Binder<'tcx, T>,
682-
b: ty::Binder<'tcx, T>,
683-
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
684-
where
685-
T: Relate<TyCtxt<'tcx>>,
686-
{
687-
self.relate(a.skip_binder(), b.skip_binder())?;
688-
Ok(a)
604+
ConstraintCategory::OpaqueType,
605+
CustomTypeOp::new(
606+
|ocx| {
607+
let cause = ObligationCause::misc(
608+
hidden_type.span,
609+
body.source.def_id().expect_local(),
610+
);
611+
let expected = ocx.normalize(&cause, infcx.param_env, expected);
612+
ocx.eq(&cause, infcx.param_env, expected.ty, hidden_type.ty)
613+
.map_err(|_| NoSolution)
614+
},
615+
"equating opaque types",
616+
),
617+
)
618+
.expect("failed to relate opaque types");
689619
}
690620
}
691621

0 commit comments

Comments
 (0)