1use std::ops::Range;
4use std::sync::Arc;
5use std::{cmp, fmt, mem};
6
7use ena::undo_log::{Rollback, UndoLogs};
8use ena::unify as ut;
9use rustc_hash::FxHashMap;
10use rustc_index::IndexVec;
11use rustc_type_ir::inherent::IntoKind;
12use rustc_type_ir::{RegionKind, RegionVid, UniverseIndex};
13use tracing::{debug, instrument};
14
15use self::CombineMapType::*;
16use self::UndoLog::*;
17use super::MemberConstraint;
18use super::unify_key::RegionVidKey;
19use crate::next_solver::infer::snapshot::undo_log::{InferCtxtUndoLogs, Snapshot};
20use crate::next_solver::infer::unify_key::RegionVariableValue;
21use crate::next_solver::{
22 AliasTy, Binder, DbInterner, OpaqueTypeKey, ParamTy, PlaceholderTy, Region, Ty,
23};
24
25#[derive(Clone, Default)]
26pub struct RegionConstraintStorage<'db> {
27 pub(super) var_infos: IndexVec<RegionVid, RegionVariableInfo>,
29
30 pub(super) data: RegionConstraintData<'db>,
31
32 lubs: CombineMap<'db>,
36
37 glbs: CombineMap<'db>,
41
42 pub(super) unification_table: ut::UnificationTableStorage<RegionVidKey<'db>>,
51
52 any_unifications: bool,
55}
56
57pub struct RegionConstraintCollector<'db, 'a> {
58 storage: &'a mut RegionConstraintStorage<'db>,
59 undo_log: &'a mut InferCtxtUndoLogs<'db>,
60}
61
62pub type VarInfos = IndexVec<RegionVid, RegionVariableInfo>;
63
64#[derive(Debug, Default, Clone)]
69pub struct RegionConstraintData<'db> {
70 pub constraints: Vec<Constraint<'db>>,
73
74 pub member_constraints: Vec<MemberConstraint<'db>>,
78
79 pub verifys: Vec<Verify<'db>>,
86}
87
88#[derive(Clone, PartialEq, Eq, Debug, Hash)]
90pub enum Constraint<'db> {
91 VarSubVar(RegionVid, RegionVid),
93
94 RegSubVar(Region<'db>, RegionVid),
96
97 VarSubReg(RegionVid, Region<'db>),
101
102 RegSubReg(Region<'db>, Region<'db>),
106}
107
108impl<'db> Constraint<'db> {
109 pub fn involves_placeholders(&self) -> bool {
110 match self {
111 Constraint::VarSubVar(_, _) => false,
112 Constraint::VarSubReg(_, r) | Constraint::RegSubVar(r, _) => r.is_placeholder(),
113 Constraint::RegSubReg(r, s) => r.is_placeholder() || s.is_placeholder(),
114 }
115 }
116}
117
118#[derive(Debug, Clone)]
119pub struct Verify<'db> {
120 pub kind: GenericKind<'db>,
121 pub region: Region<'db>,
122 pub bound: VerifyBound<'db>,
123}
124
125#[derive(Clone, PartialEq, Eq, Hash)]
126pub enum GenericKind<'db> {
127 Param(ParamTy),
128 Placeholder(PlaceholderTy),
129 Alias(AliasTy<'db>),
130}
131
132#[derive(Debug, Clone)]
150pub enum VerifyBound<'db> {
151 IfEq(Binder<'db, VerifyIfEq<'db>>),
153
154 OutlivedBy(Region<'db>),
165
166 IsEmpty,
168
169 AnyBound(Vec<VerifyBound<'db>>),
180
181 AllBounds(Vec<VerifyBound<'db>>),
193}
194
195#[derive(Debug, Clone)]
234pub struct VerifyIfEq<'db> {
235 pub ty: Ty<'db>,
237
238 pub bound: Region<'db>,
240}
241
242#[derive(Clone, PartialEq, Eq, Hash)]
243pub(crate) struct TwoRegions<'db> {
244 a: Region<'db>,
245 b: Region<'db>,
246}
247
248#[derive(Clone, PartialEq)]
249pub(crate) enum UndoLog<'db> {
250 AddVar(RegionVid),
252
253 AddConstraint(usize),
255
256 AddVerify(usize),
258
259 AddCombination(CombineMapType, TwoRegions<'db>),
261}
262
263#[derive(Clone, PartialEq)]
264pub(crate) enum CombineMapType {
265 Lub,
266 Glb,
267}
268
269type CombineMap<'db> = FxHashMap<TwoRegions<'db>, RegionVid>;
270
271#[derive(Debug, Clone)]
272pub struct RegionVariableInfo {
273 pub universe: UniverseIndex,
286}
287
288pub(crate) struct RegionSnapshot {
289 any_unifications: bool,
290}
291
292impl<'db> RegionConstraintStorage<'db> {
293 #[inline]
294 pub(crate) fn with_log<'a>(
295 &'a mut self,
296 undo_log: &'a mut InferCtxtUndoLogs<'db>,
297 ) -> RegionConstraintCollector<'db, 'a> {
298 RegionConstraintCollector { storage: self, undo_log }
299 }
300}
301
302impl<'db> RegionConstraintCollector<'db, '_> {
303 pub fn num_region_vars(&self) -> usize {
304 self.storage.var_infos.len()
305 }
306
307 pub fn region_constraint_data(&self) -> &RegionConstraintData<'db> {
308 &self.storage.data
309 }
310
311 pub fn take_and_reset_data(&mut self) -> RegionConstraintData<'db> {
324 assert!(!UndoLogs::<UndoLog<'db>>::in_snapshot(&self.undo_log));
325
326 let RegionConstraintStorage {
330 var_infos: _,
331 data,
332 lubs,
333 glbs,
334 unification_table: _,
335 any_unifications,
336 } = self.storage;
337
338 lubs.clear();
343 glbs.clear();
344
345 let data = mem::take(data);
346
347 if *any_unifications {
352 *any_unifications = false;
353 ut::UnificationTable::with_log(&mut self.storage.unification_table, &mut self.undo_log)
355 .reset_unifications(|key| RegionVariableValue::Unknown {
356 universe: self.storage.var_infos[key.vid].universe,
357 });
358 }
359
360 data
361 }
362
363 pub fn data(&self) -> &RegionConstraintData<'db> {
364 &self.storage.data
365 }
366
367 pub(super) fn start_snapshot(&self) -> RegionSnapshot {
368 debug!("RegionConstraintCollector: start_snapshot");
369 RegionSnapshot { any_unifications: self.storage.any_unifications }
370 }
371
372 pub(super) fn rollback_to(&mut self, snapshot: RegionSnapshot) {
373 debug!("RegionConstraintCollector: rollback_to({:?})", snapshot);
374 self.storage.any_unifications = snapshot.any_unifications;
375 }
376
377 pub(super) fn new_region_var(&mut self, universe: UniverseIndex) -> RegionVid {
378 let vid = self.storage.var_infos.push(RegionVariableInfo { universe });
379
380 let u_vid = self.unification_table_mut().new_key(RegionVariableValue::Unknown { universe });
381 assert_eq!(vid, u_vid.vid);
382 self.undo_log.push(AddVar(vid));
383 debug!("created new region variable {:?} in {:?}", vid, universe);
384 vid
385 }
386
387 fn add_constraint(&mut self, constraint: Constraint<'db>) {
388 debug!("RegionConstraintCollector: add_constraint({:?})", constraint);
390
391 let index = self.storage.data.constraints.len();
392 self.storage.data.constraints.push(constraint);
393 self.undo_log.push(AddConstraint(index));
394 }
395
396 pub(super) fn make_eqregion(&mut self, a: Region<'db>, b: Region<'db>) {
397 if a != b {
398 self.make_subregion(a, b);
401 self.make_subregion(b, a);
402
403 match (a.kind(), b.kind()) {
404 (RegionKind::ReVar(a), RegionKind::ReVar(b)) => {
405 debug!("make_eqregion: unifying {:?} with {:?}", a, b);
406 if self.unification_table_mut().unify_var_var(a, b).is_ok() {
407 self.storage.any_unifications = true;
408 }
409 }
410 (RegionKind::ReVar(vid), _) => {
411 debug!("make_eqregion: unifying {:?} with {:?}", vid, b);
412 if self
413 .unification_table_mut()
414 .unify_var_value(vid, RegionVariableValue::Known { value: b })
415 .is_ok()
416 {
417 self.storage.any_unifications = true;
418 };
419 }
420 (_, RegionKind::ReVar(vid)) => {
421 debug!("make_eqregion: unifying {:?} with {:?}", a, vid);
422 if self
423 .unification_table_mut()
424 .unify_var_value(vid, RegionVariableValue::Known { value: a })
425 .is_ok()
426 {
427 self.storage.any_unifications = true;
428 };
429 }
430 (_, _) => {}
431 }
432 }
433 }
434
435 #[instrument(skip(self), level = "debug")]
436 pub(super) fn make_subregion(&mut self, sub: Region<'db>, sup: Region<'db>) {
437 match (sub.kind(), sup.kind()) {
440 (RegionKind::ReBound(..), _) | (_, RegionKind::ReBound(..)) => {
441 panic!("cannot relate bound region: {sub:?} <= {sup:?}");
442 }
443 (_, RegionKind::ReStatic) => {
444 }
446 (RegionKind::ReVar(sub_id), RegionKind::ReVar(sup_id)) => {
447 self.add_constraint(Constraint::VarSubVar(sub_id, sup_id));
448 }
449 (_, RegionKind::ReVar(sup_id)) => {
450 self.add_constraint(Constraint::RegSubVar(sub, sup_id));
451 }
452 (RegionKind::ReVar(sub_id), _) => {
453 self.add_constraint(Constraint::VarSubReg(sub_id, sup));
454 }
455 _ => {
456 self.add_constraint(Constraint::RegSubReg(sub, sup));
457 }
458 }
459 }
460
461 pub fn opportunistic_resolve_var(
464 &mut self,
465 cx: DbInterner<'db>,
466 vid: RegionVid,
467 ) -> Region<'db> {
468 let mut ut = self.unification_table_mut();
469 let root_vid = ut.find(vid).vid;
470 match ut.probe_value(root_vid) {
471 RegionVariableValue::Known { value } => value,
472 RegionVariableValue::Unknown { .. } => Region::new_var(cx, root_vid),
473 }
474 }
475
476 pub fn probe_value(&mut self, vid: RegionVid) -> Result<Region<'db>, UniverseIndex> {
477 match self.unification_table_mut().probe_value(vid) {
478 RegionVariableValue::Known { value } => Ok(value),
479 RegionVariableValue::Unknown { universe } => Err(universe),
480 }
481 }
482
483 fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'db> {
484 match t {
485 Glb => &mut self.storage.glbs,
486 Lub => &mut self.storage.lubs,
487 }
488 }
489
490 fn combine_vars(
491 &mut self,
492 cx: DbInterner<'db>,
493 t: CombineMapType,
494 a: Region<'db>,
495 b: Region<'db>,
496 ) -> Region<'db> {
497 let vars = TwoRegions { a, b };
498 if let Some(c) = self.combine_map(t.clone()).get(&vars) {
499 return Region::new_var(cx, *c);
500 }
501 let a_universe = self.universe(a);
502 let b_universe = self.universe(b);
503 let c_universe = cmp::max(a_universe, b_universe);
504 let c = self.new_region_var(c_universe);
505 self.combine_map(t.clone()).insert(vars.clone(), c);
506 self.undo_log.push(AddCombination(t.clone(), vars));
507 let new_r = Region::new_var(cx, c);
508 for old_r in [a, b] {
509 match t {
510 Glb => self.make_subregion(new_r, old_r),
511 Lub => self.make_subregion(old_r, new_r),
512 }
513 }
514 debug!("combine_vars() c={:?}", c);
515 new_r
516 }
517
518 pub fn universe(&mut self, region: Region<'db>) -> UniverseIndex {
519 match region.kind() {
520 RegionKind::ReStatic
521 | RegionKind::ReErased
522 | RegionKind::ReLateParam(..)
523 | RegionKind::ReEarlyParam(..)
524 | RegionKind::ReError(_) => UniverseIndex::ROOT,
525 RegionKind::RePlaceholder(placeholder) => placeholder.universe,
526 RegionKind::ReVar(vid) => match self.probe_value(vid) {
527 Ok(value) => self.universe(value),
528 Err(universe) => universe,
529 },
530 RegionKind::ReBound(..) => panic!("universe(): encountered bound region {region:?}"),
531 }
532 }
533
534 #[inline]
535 fn unification_table_mut(&mut self) -> super::UnificationTable<'_, 'db, RegionVidKey<'db>> {
536 ut::UnificationTable::with_log(&mut self.storage.unification_table, self.undo_log)
537 }
538}
539
540impl fmt::Debug for RegionSnapshot {
541 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
542 write!(f, "RegionSnapshot")
543 }
544}
545
546impl<'db> fmt::Debug for GenericKind<'db> {
547 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
548 match *self {
549 GenericKind::Param(ref p) => write!(f, "{p:?}"),
550 GenericKind::Placeholder(ref p) => write!(f, "{p:?}"),
551 GenericKind::Alias(ref p) => write!(f, "{p:?}"),
552 }
553 }
554}
555
556impl<'db> fmt::Display for GenericKind<'db> {
557 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
558 match *self {
559 GenericKind::Param(ref p) => write!(f, "{p:?}"),
560 GenericKind::Placeholder(ref p) => write!(f, "{p:?}"),
561 GenericKind::Alias(ref p) => write!(f, "{p}"),
562 }
563 }
564}
565
566impl<'db> GenericKind<'db> {
567 pub fn to_ty(&self, interner: DbInterner<'db>) -> Ty<'db> {
568 match *self {
569 GenericKind::Param(ref p) => (*p).to_ty(interner),
570 GenericKind::Placeholder(ref p) => Ty::new_placeholder(interner, *p),
571 GenericKind::Alias(ref p) => (*p).to_ty(interner),
572 }
573 }
574}
575
576impl<'db> VerifyBound<'db> {
577 pub fn must_hold(&self) -> bool {
578 match self {
579 VerifyBound::IfEq(..) => false,
580 VerifyBound::OutlivedBy(re) => re.is_static(),
581 VerifyBound::IsEmpty => false,
582 VerifyBound::AnyBound(bs) => bs.iter().any(|b| b.must_hold()),
583 VerifyBound::AllBounds(bs) => bs.iter().all(|b| b.must_hold()),
584 }
585 }
586
587 pub fn cannot_hold(&self) -> bool {
588 match self {
589 VerifyBound::IfEq(..) => false,
590 VerifyBound::IsEmpty => false,
591 VerifyBound::OutlivedBy(_) => false,
592 VerifyBound::AnyBound(bs) => bs.iter().all(|b| b.cannot_hold()),
593 VerifyBound::AllBounds(bs) => bs.iter().any(|b| b.cannot_hold()),
594 }
595 }
596
597 pub fn or(self, vb: VerifyBound<'db>) -> VerifyBound<'db> {
598 if self.must_hold() || vb.cannot_hold() {
599 self
600 } else if self.cannot_hold() || vb.must_hold() {
601 vb
602 } else {
603 VerifyBound::AnyBound(vec![self, vb])
604 }
605 }
606}
607
608impl<'db> RegionConstraintData<'db> {
609 pub fn is_empty(&self) -> bool {
612 let RegionConstraintData { constraints, member_constraints, verifys } = self;
613 constraints.is_empty() && member_constraints.is_empty() && verifys.is_empty()
614 }
615}
616
617impl<'db> Rollback<UndoLog<'db>> for RegionConstraintStorage<'db> {
618 fn reverse(&mut self, undo: UndoLog<'db>) {
619 match undo {
620 AddVar(vid) => {
621 self.var_infos.pop().unwrap();
622 assert_eq!(self.var_infos.len(), vid.index());
623 }
624 AddConstraint(index) => {
625 self.data.constraints.pop().unwrap();
626 assert_eq!(self.data.constraints.len(), index);
627 }
628 AddVerify(index) => {
629 self.data.verifys.pop();
630 assert_eq!(self.data.verifys.len(), index);
631 }
632 AddCombination(Glb, ref regions) => {
633 self.glbs.remove(regions);
634 }
635 AddCombination(Lub, ref regions) => {
636 self.lubs.remove(regions);
637 }
638 }
639 }
640}