1use std::{cell::RefCell, fmt, iter};
2
3use flux_common::{bug, dbg, tracked_span_assert_eq, tracked_span_bug, tracked_span_dbg_assert_eq};
4use flux_config::{self as config, InferOpts, OverflowMode, RawDerefMode};
5use flux_macros::{TypeFoldable, TypeVisitable};
6use flux_middle::{
7 FixpointQueryKind, PanicSpec,
8 def_id::MaybeExternId,
9 global_env::GlobalEnv,
10 metrics::{self, Metric},
11 queries::{QueryErr, QueryResult},
12 query_bug,
13 rty::{
14 self, AliasKind, AliasTy, BaseTy, Binder, BoundReftKind, BoundVariableKinds,
15 CoroutineObligPredicate, Ctor, ESpan, EVid, EarlyBinder, Expr, ExprKind, FieldProj,
16 GenericArg, HoleKind, InferMode, Lambda, List, Loc, Mutability, Name, NameProvenance, Path,
17 PolyVariant, PtrKind, RefineArgs, RefineArgsExt, Region, Sort, Ty, TyCtor, TyKind, Var,
18 canonicalize::{Hoister, HoisterDelegate},
19 fold::TypeFoldable,
20 },
21};
22use itertools::{Itertools, izip};
23use rustc_hir::def_id::{DefId, LocalDefId};
24use rustc_macros::extension;
25use rustc_middle::{
26 mir::BasicBlock,
27 ty::{TyCtxt, Variance},
28};
29use rustc_span::{Span, Symbol};
30use rustc_type_ir::Variance::Invariant;
31
32use crate::{
33 evars::{EVarState, EVarStore},
34 fixpoint_encoding::{
35 Answer, Backend, FixQueryCache, FixpointCtxt, KVarGen, lean_task_key, record_lean_task,
36 },
37 fn_subtyping::{SubFn, check_fn_subtyping},
38 lean_encoding::{hyperlink_proof, log_proof},
39 projections::NormalizeExt as _,
40 refine_tree::{Cursor, Marker, RefineTree, Scope},
41};
42
43pub type InferResult<T = ()> = std::result::Result<T, InferErr>;
44
45#[derive(PartialEq, Eq, Clone, Copy, Hash)]
46pub struct Tag {
47 pub reason: ConstrReason,
48 pub src_span: Span,
49 pub dst_span: Option<ESpan>,
50}
51
52impl Tag {
53 pub fn new(reason: ConstrReason, span: Span) -> Self {
54 Self { reason, src_span: span, dst_span: None }
55 }
56
57 pub fn with_dst(self, dst_span: Option<ESpan>) -> Self {
58 Self { dst_span, ..self }
59 }
60}
61
62#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
63pub enum SubtypeReason {
64 Input,
65 Output,
66 Requires,
67 Ensures,
68}
69
70#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
71pub enum ConstrReason {
72 Call,
73 Assign,
74 Ret,
75 Fold,
76 FoldLocal,
77 Predicate,
78 Assert(&'static str),
79 Div,
80 Rem,
81 Goto(BasicBlock),
82 Overflow,
83 Underflow,
84 Subtype(SubtypeReason),
85 NoPanic(DefId, PanicSpec),
86 Other,
87}
88
89pub struct InferCtxtRoot<'genv, 'tcx> {
90 pub genv: GlobalEnv<'genv, 'tcx>,
91 inner: RefCell<InferCtxtInner>,
92 refine_tree: RefineTree,
93 opts: InferOpts,
94}
95
96pub struct InferCtxtRootBuilder<'a, 'genv, 'tcx> {
97 genv: GlobalEnv<'genv, 'tcx>,
98 opts: InferOpts,
99 params: Vec<(Var, Sort)>,
100 assumptions: Vec<Expr>,
102 infcx: &'a rustc_infer::infer::InferCtxt<'tcx>,
103 dummy_kvars: bool,
104}
105
106#[extension(pub trait GlobalEnvExt<'genv, 'tcx>)]
107impl<'genv, 'tcx> GlobalEnv<'genv, 'tcx> {
108 fn infcx_root<'a>(
109 self,
110 infcx: &'a rustc_infer::infer::InferCtxt<'tcx>,
111 opts: InferOpts,
112 ) -> InferCtxtRootBuilder<'a, 'genv, 'tcx> {
113 InferCtxtRootBuilder {
114 genv: self,
115 infcx,
116 params: vec![],
117 assumptions: vec![],
118 opts,
119 dummy_kvars: false,
120 }
121 }
122}
123
124impl<'genv, 'tcx> InferCtxtRootBuilder<'_, 'genv, 'tcx> {
125 pub fn with_dummy_kvars(mut self) -> Self {
126 self.dummy_kvars = true;
127 self
128 }
129
130 pub fn with_const_generics(mut self, def_id: DefId) -> QueryResult<Self> {
133 let tcx = self.genv.tcx();
134 for (param_const, sort, param_def_id) in
135 self.genv.generics_of(def_id)?.const_params(self.genv)?
136 {
137 let param_ty = tcx
139 .type_of(param_def_id)
140 .instantiate_identity()
141 .skip_normalization();
142 let bty = match param_ty.kind() {
143 rustc_middle::ty::Uint(uint_ty) => Some(BaseTy::Uint(*uint_ty)),
144 rustc_middle::ty::Int(int_ty) => Some(BaseTy::Int(*int_ty)),
145 rustc_middle::ty::Char => Some(BaseTy::Char),
146 _ => None,
147 };
148 if let Some(bty) = bty {
149 let idx = Expr::const_generic(param_const);
150 self.assumptions.extend(
151 bty.invariants(self.genv, self.opts.check_overflow)
152 .map(|inv| inv.apply(&idx)),
153 );
154 }
155 self.params.push((Var::ConstGeneric(param_const), sort));
156 }
157 Ok(self)
158 }
159
160 pub fn with_refinement_generics(
161 mut self,
162 def_id: DefId,
163 args: &[GenericArg],
164 ) -> QueryResult<Self> {
165 for (index, param) in self
166 .genv
167 .refinement_generics_of(def_id)?
168 .iter_own_params()
169 .enumerate()
170 {
171 let param = param.instantiate(self.genv.tcx(), args, &[]);
172 let sort = param
173 .sort
174 .deeply_normalize_sorts(def_id, self.genv, self.infcx)?;
175
176 let var =
177 Var::EarlyParam(rty::EarlyReftParam { index: index as u32, name: param.name });
178 self.params.push((var, sort));
179 }
180 Ok(self)
181 }
182
183 pub fn identity_for_item(mut self, def_id: DefId) -> QueryResult<Self> {
184 self = self.with_const_generics(def_id)?;
185 let offset = self.params.len();
186 self.genv.refinement_generics_of(def_id)?.fill_item(
187 self.genv,
188 &mut self.params,
189 &mut |param, index| {
190 let index = (index - offset) as u32;
191 let param = param.instantiate_identity();
192 let sort = param
193 .sort
194 .deeply_normalize_sorts(def_id, self.genv, self.infcx)?;
195
196 let var = Var::EarlyParam(rty::EarlyReftParam { index, name: param.name });
197 Ok((var, sort))
198 },
199 )?;
200 Ok(self)
201 }
202
203 pub fn build(self) -> QueryResult<InferCtxtRoot<'genv, 'tcx>> {
204 Ok(InferCtxtRoot {
205 genv: self.genv,
206 inner: RefCell::new(InferCtxtInner::new(self.dummy_kvars)),
207 refine_tree: RefineTree::new(self.params, self.assumptions),
208 opts: self.opts,
209 })
210 }
211}
212
213impl<'genv, 'tcx> InferCtxtRoot<'genv, 'tcx> {
214 pub fn infcx<'a>(
215 &'a mut self,
216 def_id: DefId,
217 region_infcx: &'a rustc_infer::infer::InferCtxt<'tcx>,
218 ) -> InferCtxt<'a, 'genv, 'tcx> {
219 InferCtxt {
220 genv: self.genv,
221 region_infcx,
222 def_id,
223 cursor: self.refine_tree.cursor_at_root(),
224 inner: &self.inner,
225 check_overflow: self.opts.check_overflow,
226 allow_raw_deref: self.opts.allow_raw_deref,
227 }
228 }
229
230 pub fn fresh_kvar_in_scope(&self, binders: &[BoundVariableKinds], scope: &Scope) -> Expr {
231 let inner = &mut *self.inner.borrow_mut();
232 inner.kvars.fresh(binders, scope.iter())
233 }
234
235 pub fn execute_lean_query(
236 self,
237 cache: &mut FixQueryCache,
238 def_id: MaybeExternId,
239 ) -> QueryResult {
240 let inner = self.inner.into_inner();
241 let kvars = inner.kvars;
242 let evars = inner.evars;
243 let mut refine_tree = self.refine_tree;
244 refine_tree.replace_evars(&evars).unwrap();
245 refine_tree.simplify(self.genv);
246
247 let solver = match self.opts.solver {
248 flux_config::SmtSolver::Z3 => liquid_fixpoint::SmtSolver::Z3,
249 flux_config::SmtSolver::CVC5 => liquid_fixpoint::SmtSolver::CVC5,
250 };
251 let mut fcx = FixpointCtxt::new(self.genv, def_id, kvars, Backend::Lean, self.opts.uif_ops);
252 let cstr = refine_tree.to_fixpoint(&mut fcx)?;
253 let (task, _) = fcx.create_task(def_id, cstr, self.opts.scrape_quals, solver)?;
254
255 log_proof(self.genv, def_id)?;
256 let key = lean_task_key(self.genv.tcx(), def_id.resolved_id());
258 let hash = task.hash_with_default();
259 let cached = config::is_cache_enabled() && cache.lookup(&key, hash).is_some();
260 if !cached {
261 fcx.generate_lean_files(def_id, task)?;
262 if config::is_cache_enabled() {
263 record_lean_task(cache, key, hash);
264 }
265 }
266 hyperlink_proof(self.genv, def_id);
268 Ok(())
269 }
270
271 pub fn execute_fixpoint_query(
272 self,
273 cache: &mut FixQueryCache,
274 def_id: MaybeExternId,
275 kind: FixpointQueryKind,
276 ) -> QueryResult<Answer<Tag>> {
277 let inner = self.inner.into_inner();
278 let kvars = inner.kvars;
279 let evars = inner.evars;
280
281 let ext = kind.ext();
282
283 let mut refine_tree = self.refine_tree;
284
285 refine_tree.replace_evars(&evars).unwrap();
286
287 if config::dump_constraint() {
288 dbg::dump_item_info(self.genv.tcx(), def_id.resolved_id(), ext, &refine_tree).unwrap();
289 }
290 refine_tree.simplify(self.genv);
291 if config::dump_constraint() {
292 let simp_ext = format!("simp.{ext}");
293 dbg::dump_item_info(self.genv.tcx(), def_id.resolved_id(), simp_ext, &refine_tree)
294 .unwrap();
295 }
296
297 let backend = match self.opts.solver {
298 flux_config::SmtSolver::Z3 => liquid_fixpoint::SmtSolver::Z3,
299 flux_config::SmtSolver::CVC5 => liquid_fixpoint::SmtSolver::CVC5,
300 };
301
302 let mut fcx =
303 FixpointCtxt::new(self.genv, def_id, kvars, Backend::Fixpoint, self.opts.uif_ops);
304 let cstr = refine_tree.to_fixpoint(&mut fcx)?;
305
306 let count = cstr.concrete_head_count();
308 metrics::incr_metric(Metric::CsTotal, count as u32);
309 if count == 0 {
310 metrics::incr_metric_if(kind.is_body(), Metric::FnTrivial);
311 return Ok(Answer::trivial());
312 }
313
314 let (task, suggestion_ctx) =
315 fcx.create_task(def_id, cstr, self.opts.scrape_quals, backend)?;
316 let result = fcx.run_task(cache, def_id, kind, &task)?;
317 fcx.result_to_answer(result, suggestion_ctx)
318 }
319
320 pub fn split(self) -> (RefineTree, KVarGen) {
321 (self.refine_tree, self.inner.into_inner().kvars)
322 }
323}
324
325pub struct InferCtxt<'infcx, 'genv, 'tcx> {
326 pub genv: GlobalEnv<'genv, 'tcx>,
327 pub region_infcx: &'infcx rustc_infer::infer::InferCtxt<'tcx>,
328 pub def_id: DefId,
329 pub check_overflow: OverflowMode,
330 pub allow_raw_deref: flux_config::RawDerefMode,
331 cursor: Cursor<'infcx>,
332 inner: &'infcx RefCell<InferCtxtInner>,
333}
334
335struct InferCtxtInner {
336 kvars: KVarGen,
337 evars: EVarStore,
338}
339
340impl InferCtxtInner {
341 fn new(dummy_kvars: bool) -> Self {
342 Self { kvars: KVarGen::new(dummy_kvars), evars: Default::default() }
343 }
344}
345
346impl<'infcx, 'genv, 'tcx> InferCtxt<'infcx, 'genv, 'tcx> {
347 pub fn at(&mut self, span: Span) -> InferCtxtAt<'_, 'infcx, 'genv, 'tcx> {
348 InferCtxtAt { infcx: self, span }
349 }
350
351 pub fn instantiate_refine_args(
352 &mut self,
353 callee_def_id: DefId,
354 args: &[rty::GenericArg],
355 ) -> InferResult<List<Expr>> {
356 Ok(RefineArgs::for_item(self.genv, callee_def_id, |param, _| {
357 let param = param.instantiate(self.genv.tcx(), args, &[]);
358 Ok(self.fresh_infer_var(¶m.sort, param.mode))
359 })?)
360 }
361
362 pub fn instantiate_generic_args(&mut self, args: &[GenericArg]) -> Vec<GenericArg> {
363 args.iter()
364 .map(|a| a.replace_holes(|binders, kind| self.fresh_infer_var_for_hole(binders, kind)))
365 .collect_vec()
366 }
367
368 pub fn fresh_infer_var(&self, sort: &Sort, mode: InferMode) -> Expr {
369 match mode {
370 InferMode::KVar => {
371 let fsort = sort.expect_func().expect_mono();
372 let vars = fsort.inputs().iter().cloned().map_into().collect();
373 let kvar = self.fresh_kvar(&[vars]);
374 Expr::abs(Lambda::bind_with_fsort(kvar, fsort))
375 }
376 InferMode::EVar => self.fresh_evar(),
377 }
378 }
379
380 pub fn fresh_infer_var_for_hole(
381 &mut self,
382 binders: &[BoundVariableKinds],
383 kind: HoleKind,
384 ) -> Expr {
385 match kind {
386 HoleKind::Pred => self.fresh_kvar(binders),
387 HoleKind::Expr(_) => {
388 self.fresh_evar()
392 }
393 }
394 }
395
396 pub fn fresh_kvar_in_scope(&self, binders: &[BoundVariableKinds], scope: &Scope) -> Expr {
398 let inner = &mut *self.inner.borrow_mut();
399 inner.kvars.fresh(binders, scope.iter())
400 }
401
402 pub fn fresh_kvar(&self, binders: &[BoundVariableKinds]) -> Expr {
404 let inner = &mut *self.inner.borrow_mut();
405 inner.kvars.fresh(binders, self.cursor.vars())
406 }
407
408 fn fresh_evar(&self) -> Expr {
409 let evars = &mut self.inner.borrow_mut().evars;
410 Expr::evar(evars.fresh(self.cursor.marker()))
411 }
412
413 pub fn unify_exprs(&self, a: &Expr, b: &Expr) {
414 if a.has_evars() {
415 return;
416 }
417 let evars = &mut self.inner.borrow_mut().evars;
418 if let ExprKind::Var(Var::EVar(evid)) = b.kind()
419 && let EVarState::Unsolved(marker) = evars.get(*evid)
420 && !marker.has_free_vars(a)
421 {
422 evars.solve(*evid, a.clone());
423 }
424 }
425
426 fn enter_exists<T, U>(
427 &mut self,
428 t: &Binder<T>,
429 f: impl FnOnce(&mut InferCtxt<'_, 'genv, 'tcx>, T) -> InferResult<U>,
430 ) -> InferResult<U>
431 where
432 T: TypeFoldable,
433 {
434 self.ensure_resolved_evars(|infcx| {
435 let t = t.replace_bound_refts_with(|sort, mode, _| infcx.fresh_infer_var(sort, mode));
436 f(infcx, t)
437 })
438 }
439
440 pub fn push_evar_scope(&mut self) {
445 self.inner.borrow_mut().evars.push_scope();
446 }
447
448 pub fn pop_evar_scope(&mut self) -> InferResult {
451 self.inner
452 .borrow_mut()
453 .evars
454 .pop_scope()
455 .map_err(InferErr::UnsolvedEvar)
456 }
457
458 pub fn ensure_resolved_evars<R>(
460 &mut self,
461 f: impl FnOnce(&mut Self) -> InferResult<R>,
462 ) -> InferResult<R> {
463 self.push_evar_scope();
464 let r = f(self)?;
465 self.pop_evar_scope()?;
466 Ok(r)
467 }
468
469 pub fn fully_resolve_evars<T: TypeFoldable>(&self, t: &T) -> T {
470 self.inner.borrow().evars.replace_evars(t).unwrap()
471 }
472
473 pub fn tcx(&self) -> TyCtxt<'tcx> {
474 self.genv.tcx()
475 }
476
477 pub fn cursor(&self) -> &Cursor<'infcx> {
478 &self.cursor
479 }
480
481 pub fn allow_raw_deref(&self) -> bool {
482 matches!(self.allow_raw_deref, RawDerefMode::Ok)
483 }
484}
485
486impl<'infcx, 'genv, 'tcx> InferCtxt<'infcx, 'genv, 'tcx> {
488 pub fn change_item<'a>(
489 &'a mut self,
490 def_id: LocalDefId,
491 region_infcx: &'a rustc_infer::infer::InferCtxt<'tcx>,
492 ) -> InferCtxt<'a, 'genv, 'tcx> {
493 InferCtxt {
494 def_id: def_id.to_def_id(),
495 cursor: self.cursor.branch(),
496 region_infcx,
497 ..*self
498 }
499 }
500
501 pub fn move_to(&mut self, marker: &Marker, clear_children: bool) -> InferCtxt<'_, 'genv, 'tcx> {
502 InferCtxt {
503 cursor: self
504 .cursor
505 .move_to(marker, clear_children)
506 .unwrap_or_else(|| tracked_span_bug!()),
507 ..*self
508 }
509 }
510
511 pub fn branch(&mut self) -> InferCtxt<'_, 'genv, 'tcx> {
512 InferCtxt { cursor: self.cursor.branch(), ..*self }
513 }
514
515 fn define_var(&mut self, sort: &Sort, provenance: NameProvenance) -> Name {
516 self.cursor.define_var(sort, provenance)
517 }
518
519 pub fn define_bound_reft_var(&mut self, sort: &Sort, kind: BoundReftKind) -> Name {
520 self.define_var(sort, NameProvenance::UnfoldBoundReft(kind))
521 }
522
523 pub fn define_unknown_var(&mut self, sort: &Sort) -> Name {
524 self.cursor.define_var(sort, NameProvenance::Unknown)
525 }
526
527 pub fn check_pred(&mut self, pred: impl Into<Expr>, tag: Tag) {
528 self.cursor.check_pred(pred, tag);
529 }
530
531 pub fn assume_pred(&mut self, pred: impl Into<Expr>) {
532 self.cursor.assume_pred(pred);
533 }
534
535 pub fn unpack(&mut self, ty: &Ty) -> Ty {
536 self.hoister(false).hoist(ty)
537 }
538
539 pub fn unpack_at_name(&mut self, name: Option<Symbol>, ty: &Ty) -> Ty {
540 let mut hoister = self.hoister(false);
541 hoister.delegate.name = name;
542 hoister.hoist(ty)
543 }
544
545 pub fn marker(&self) -> Marker {
546 self.cursor.marker()
547 }
548
549 pub fn hoister(
550 &mut self,
551 assume_invariants: bool,
552 ) -> Hoister<Unpacker<'_, 'infcx, 'genv, 'tcx>> {
553 Hoister::with_delegate(Unpacker { infcx: self, assume_invariants, name: None })
554 .transparent()
555 }
556
557 pub fn assume_invariants(&mut self, ty: &Ty) {
558 self.cursor
559 .assume_invariants(self.genv, ty, self.check_overflow);
560 }
561
562 fn check_impl(&mut self, pred1: impl Into<Expr>, pred2: impl Into<Expr>, tag: Tag) {
563 self.cursor.check_impl(pred1, pred2, tag);
564 }
565}
566
567pub struct Unpacker<'a, 'infcx, 'genv, 'tcx> {
568 infcx: &'a mut InferCtxt<'infcx, 'genv, 'tcx>,
569 assume_invariants: bool,
570 name: Option<Symbol>,
571}
572
573impl HoisterDelegate for Unpacker<'_, '_, '_, '_> {
574 fn hoist_exists(&mut self, ty_ctor: &TyCtor) -> Ty {
575 let ty = ty_ctor.replace_bound_refts_with(|sort, _, kind| {
576 let kind = if let Some(name) = self.name { BoundReftKind::Named(name) } else { kind };
577 Expr::fvar(self.infcx.define_bound_reft_var(sort, kind))
578 });
579 if self.assume_invariants {
580 self.infcx.assume_invariants(&ty);
581 }
582 ty
583 }
584
585 fn hoist_constr(&mut self, pred: Expr) {
586 self.infcx.assume_pred(pred);
587 }
588}
589
590impl std::fmt::Debug for InferCtxt<'_, '_, '_> {
591 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
592 std::fmt::Debug::fmt(&self.cursor, f)
593 }
594}
595
596#[derive(Debug)]
597pub struct InferCtxtAt<'a, 'infcx, 'genv, 'tcx> {
598 pub infcx: &'a mut InferCtxt<'infcx, 'genv, 'tcx>,
599 pub span: Span,
600}
601
602impl<'genv, 'tcx> InferCtxtAt<'_, '_, 'genv, 'tcx> {
603 fn tag(&self, reason: ConstrReason) -> Tag {
604 Tag::new(reason, self.span)
605 }
606
607 pub fn check_pred(&mut self, pred: impl Into<Expr>, reason: ConstrReason) {
608 let tag = self.tag(reason);
609 self.infcx.check_pred(pred, tag);
610 }
611
612 pub fn check_non_closure_clauses(
613 &mut self,
614 clauses: &[rty::Clause],
615 reason: ConstrReason,
616 ) -> InferResult {
617 for clause in clauses {
618 if let rty::ClauseKind::Projection(projection_pred) = clause.kind_skipping_binder() {
619 let impl_elem = BaseTy::Alias(projection_pred.projection_term.to_alias_ty())
620 .to_ty()
621 .deeply_normalize(self)?;
622 let term = projection_pred.term.to_ty().deeply_normalize(self)?;
623
624 self.subtyping(&impl_elem, &term, reason)?;
626 self.subtyping(&term, &impl_elem, reason)?;
627 }
628 }
629 Ok(())
630 }
631
632 pub fn subtyping_with_env(
635 &mut self,
636 env: &mut impl LocEnv,
637 a: &Ty,
638 b: &Ty,
639 reason: ConstrReason,
640 ) -> InferResult<Vec<Binder<rty::CoroutineObligPredicate>>> {
641 let mut sub = Sub::new(env, reason, self.span);
642 sub.tys(self.infcx, a, b)?;
643 Ok(sub.obligations)
644 }
645
646 pub fn subtyping(
651 &mut self,
652 a: &Ty,
653 b: &Ty,
654 reason: ConstrReason,
655 ) -> InferResult<Vec<Binder<rty::CoroutineObligPredicate>>> {
656 let mut env = DummyEnv;
657 let mut sub = Sub::new(&mut env, reason, self.span);
658 sub.tys(self.infcx, a, b)?;
659 Ok(sub.obligations)
660 }
661
662 pub fn subtyping_generic_args(
663 &mut self,
664 variance: Variance,
665 a: &GenericArg,
666 b: &GenericArg,
667 reason: ConstrReason,
668 ) -> InferResult<Vec<Binder<rty::CoroutineObligPredicate>>> {
669 let mut env = DummyEnv;
670 let mut sub = Sub::new(&mut env, reason, self.span);
671 sub.generic_args(self.infcx, variance, a, b)?;
672 Ok(sub.obligations)
673 }
674
675 pub fn check_constructor(
679 &mut self,
680 variant: EarlyBinder<PolyVariant>,
681 generic_args: &[GenericArg],
682 fields: &[Ty],
683 reason: ConstrReason,
684 ) -> InferResult<Ty> {
685 let ret = self.ensure_resolved_evars(|this| {
686 let generic_args = this.instantiate_generic_args(generic_args);
688
689 let variant = variant
690 .instantiate(this.tcx(), &generic_args, &[])
691 .replace_bound_refts_with(|sort, mode, _| this.fresh_infer_var(sort, mode));
692
693 for (actual, formal) in iter::zip(fields, variant.fields()) {
695 this.subtyping(actual, formal, reason)?;
696 }
697
698 for require in &variant.requires {
700 this.check_pred(require, ConstrReason::Fold);
701 }
702
703 Ok(variant.ret())
704 })?;
705 Ok(self.fully_resolve_evars(&ret))
706 }
707
708 pub fn ensure_resolved_evars<R>(
709 &mut self,
710 f: impl FnOnce(&mut InferCtxtAt<'_, '_, 'genv, 'tcx>) -> InferResult<R>,
711 ) -> InferResult<R> {
712 self.infcx
713 .ensure_resolved_evars(|infcx| f(&mut infcx.at(self.span)))
714 }
715}
716
717impl<'a, 'genv, 'tcx> std::ops::Deref for InferCtxtAt<'_, 'a, 'genv, 'tcx> {
718 type Target = InferCtxt<'a, 'genv, 'tcx>;
719
720 fn deref(&self) -> &Self::Target {
721 self.infcx
722 }
723}
724
725impl std::ops::DerefMut for InferCtxtAt<'_, '_, '_, '_> {
726 fn deref_mut(&mut self) -> &mut Self::Target {
727 self.infcx
728 }
729}
730
731#[derive(TypeVisitable, TypeFoldable)]
735pub(crate) enum TypeTrace {
736 Types(Ty, Ty),
737 BaseTys(BaseTy, BaseTy),
738}
739
740#[expect(dead_code, reason = "we use this for debugging some time")]
741impl TypeTrace {
742 fn tys(a: &Ty, b: &Ty) -> Self {
743 Self::Types(a.clone(), b.clone())
744 }
745
746 fn btys(a: &BaseTy, b: &BaseTy) -> Self {
747 Self::BaseTys(a.clone(), b.clone())
748 }
749}
750
751impl fmt::Debug for TypeTrace {
752 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
753 match self {
754 TypeTrace::Types(a, b) => write!(f, "{a:?} - {b:?}"),
755 TypeTrace::BaseTys(a, b) => write!(f, "{a:?} - {b:?}"),
756 }
757 }
758}
759
760pub trait LocEnv {
761 fn ptr_to_ref(
762 &mut self,
763 infcx: &mut InferCtxtAt,
764 reason: ConstrReason,
765 re: Region,
766 path: &Path,
767 bound: Ty,
768 ) -> InferResult<Ty>;
769
770 fn unfold_strg_ref(&mut self, infcx: &mut InferCtxt, path: &Path, ty: &Ty) -> InferResult<Loc>;
771
772 fn unfold_local_ptr(&mut self, infcx: &mut InferCtxt, bound: &Ty) -> InferResult<Loc>;
774
775 fn fold_local_ptrs(&mut self, infcx: &mut InferCtxtAt) -> InferResult;
777
778 fn get(&self, path: &Path) -> Ty;
779
780 fn update_path(&mut self, path: &Path, new_ty: Ty, span: Span);
781
782 fn assume_ensures(&mut self, infcx: &mut InferCtxt, ensures: &[rty::Ensures], span: Span) {
783 for ensure in ensures {
784 match ensure {
785 rty::Ensures::Type(path, updated_ty) => {
786 let updated_ty = infcx.unpack(updated_ty);
787 infcx.assume_invariants(&updated_ty);
788 self.update_path(path, updated_ty, span);
789 }
790 rty::Ensures::Pred(e) => infcx.assume_pred(e),
791 }
792 }
793 }
794
795 fn check_ensures(
796 &mut self,
797 at: &mut InferCtxtAt,
798 ensures: &[rty::Ensures],
799 reason: ConstrReason,
800 ) -> InferResult {
801 for constraint in ensures {
802 match constraint {
803 rty::Ensures::Type(path, ty) => {
804 let actual_ty = self.get(path).unblocked(); at.subtyping(&actual_ty, ty, reason)?;
806 }
807 rty::Ensures::Pred(e) => {
808 at.check_pred(e, ConstrReason::Ret);
809 }
810 }
811 }
812 Ok(())
813 }
814}
815
816pub(crate) struct DummyEnv;
819
820impl LocEnv for DummyEnv {
821 fn ptr_to_ref(
822 &mut self,
823 _: &mut InferCtxtAt,
824 _: ConstrReason,
825 _: Region,
826 _: &Path,
827 _: Ty,
828 ) -> InferResult<Ty> {
829 tracked_span_bug!("call to `ptr_to_ref` on `DummyEnv`")
830 }
831
832 fn unfold_strg_ref(&mut self, _: &mut InferCtxt, _: &Path, _: &Ty) -> InferResult<Loc> {
833 tracked_span_bug!("call to `unfold_str_ref` on `DummyEnv`")
834 }
835
836 fn unfold_local_ptr(&mut self, _: &mut InferCtxt, _: &Ty) -> InferResult<Loc> {
837 tracked_span_bug!("call to `unfold_local_ptr` on `DummyEnv`")
838 }
839
840 fn fold_local_ptrs(&mut self, _: &mut InferCtxtAt) -> InferResult {
841 Ok(())
843 }
844
845 fn get(&self, _: &Path) -> Ty {
846 tracked_span_bug!("call to `get` on `DummyEnv`")
847 }
848
849 fn update_path(&mut self, _: &Path, _: Ty, _: Span) {
850 tracked_span_bug!("call to `update_path` on `DummyEnv`");
851 }
852}
853
854struct Sub<'a, E> {
856 env: &'a mut E,
858 reason: ConstrReason,
859 span: Span,
860 obligations: Vec<Binder<rty::CoroutineObligPredicate>>,
864}
865
866impl<'a, E: LocEnv> Sub<'a, E> {
867 fn new(env: &'a mut E, reason: ConstrReason, span: Span) -> Self {
868 Self { env, reason, span, obligations: vec![] }
869 }
870
871 fn tag(&self) -> Tag {
872 Tag::new(self.reason, self.span)
873 }
874
875 fn tys(&mut self, infcx: &mut InferCtxt, a: &Ty, b: &Ty) -> InferResult {
876 let infcx = &mut infcx.branch();
877 let a = infcx.unpack(a);
883
884 match (a.kind(), b.kind()) {
885 (TyKind::Exists(..), _) => {
886 bug!("existentials should have been removed by the unpacking above");
887 }
888 (TyKind::Constr(..), _) => {
889 bug!("constraint types should have been removed by the unpacking above");
890 }
891
892 (_, TyKind::Exists(ctor_b)) => {
893 infcx.enter_exists(ctor_b, |infcx, ty_b| self.tys(infcx, &a, &ty_b))
894 }
895 (_, TyKind::Constr(pred_b, ty_b)) => {
896 infcx.check_pred(pred_b, self.tag());
897 self.tys(infcx, &a, ty_b)
898 }
899
900 (TyKind::Ptr(PtrKind::Mut(_), path_a), TyKind::StrgRef(_, path_b, ty_b)) => {
901 let ty_a = self.env.get(path_a);
905 infcx.unify_exprs(&path_a.to_expr(), &path_b.to_expr());
906 self.tys(infcx, &ty_a, ty_b)
907 }
908 (TyKind::StrgRef(re, path_a, ty_a), _) => {
909 self.env.unfold_strg_ref(infcx, path_a, ty_a)?;
919 self.tys(infcx, &Ty::ptr(PtrKind::Mut(*re), path_a.clone()), b)
920 }
921 (
922 TyKind::Ptr(PtrKind::Mut(re), path),
923 TyKind::Indexed(BaseTy::Ref(_, bound, Mutability::Mut), idx),
924 ) => {
925 self.idxs_eq(infcx, &Expr::unit(), idx);
928
929 self.env.ptr_to_ref(
930 &mut infcx.at(self.span),
931 self.reason,
932 *re,
933 path,
934 bound.clone(),
935 )?;
936 Ok(())
937 }
938
939 (TyKind::Indexed(bty_a, idx_a), TyKind::Indexed(bty_b, idx_b)) => {
940 self.btys(infcx, bty_a, bty_b)?;
941 self.idxs_eq(infcx, idx_a, idx_b);
942 Ok(())
943 }
944 (TyKind::Ptr(pk_a, path_a), TyKind::Ptr(pk_b, path_b)) => {
945 debug_assert_eq!(pk_a, pk_b);
946 debug_assert_eq!(path_a, path_b);
947 Ok(())
948 }
949 (TyKind::Param(param_ty_a), TyKind::Param(param_ty_b)) => {
950 debug_assert_eq!(param_ty_a, param_ty_b);
951 Ok(())
952 }
953 (_, TyKind::Uninit) => Ok(()),
954 (TyKind::Downcast(.., fields_a), TyKind::Downcast(.., fields_b)) => {
955 debug_assert_eq!(fields_a.len(), fields_b.len());
956 for (ty_a, ty_b) in iter::zip(fields_a, fields_b) {
957 self.tys(infcx, ty_a, ty_b)?;
958 }
959 Ok(())
960 }
961 _ => Err(query_bug!("incompatible types: `{a:?}` - `{b:?}`"))?,
962 }
963 }
964
965 fn btys(&mut self, infcx: &mut InferCtxt, a: &BaseTy, b: &BaseTy) -> InferResult {
966 match (a, b) {
969 (BaseTy::Int(int_ty_a), BaseTy::Int(int_ty_b)) => {
970 debug_assert_eq!(int_ty_a, int_ty_b);
971 Ok(())
972 }
973 (BaseTy::Uint(uint_ty_a), BaseTy::Uint(uint_ty_b)) => {
974 debug_assert_eq!(uint_ty_a, uint_ty_b);
975 Ok(())
976 }
977 (BaseTy::Adt(a_adt, a_args), BaseTy::Adt(b_adt, b_args)) => {
978 tracked_span_dbg_assert_eq!(a_adt.did(), b_adt.did());
979 tracked_span_dbg_assert_eq!(a_args.len(), b_args.len());
980 let variances = infcx.genv.variances_of(a_adt.did());
981 for (variance, ty_a, ty_b) in izip!(variances, a_args.iter(), b_args.iter()) {
982 self.generic_args(infcx, *variance, ty_a, ty_b)?;
983 }
984 Ok(())
985 }
986 (BaseTy::FnDef(a_def_id, a_args), BaseTy::FnDef(b_def_id, b_args)) => {
987 debug_assert_eq!(a_def_id, b_def_id);
988 debug_assert_eq!(a_args.len(), b_args.len());
989 for (arg_a, arg_b) in iter::zip(a_args, b_args) {
997 match (arg_a, arg_b) {
998 (GenericArg::Ty(ty_a), GenericArg::Ty(ty_b)) => {
999 let bty_a = ty_a.as_bty_skipping_existentials();
1000 let bty_b = ty_b.as_bty_skipping_existentials();
1001 tracked_span_dbg_assert_eq!(bty_a, bty_b);
1002 }
1003 (GenericArg::Base(ctor_a), GenericArg::Base(ctor_b)) => {
1004 let bty_a = ctor_a.as_bty_skipping_binder();
1005 let bty_b = ctor_b.as_bty_skipping_binder();
1006 tracked_span_dbg_assert_eq!(bty_a, bty_b);
1007 }
1008 (_, _) => tracked_span_dbg_assert_eq!(arg_a, arg_b),
1009 }
1010 }
1011 Ok(())
1012 }
1013 (BaseTy::Float(float_ty_a), BaseTy::Float(float_ty_b)) => {
1014 debug_assert_eq!(float_ty_a, float_ty_b);
1015 Ok(())
1016 }
1017 (BaseTy::Slice(ty_a), BaseTy::Slice(ty_b)) => self.tys(infcx, ty_a, ty_b),
1018
1019 (BaseTy::RawPtr(ty_a, mut_a), BaseTy::RawPtr(ty_b, mut_b)) => {
1020 debug_assert_eq!(mut_a, mut_b);
1021 self.tys(infcx, ty_a, ty_b)?;
1022 if matches!(mut_a, Mutability::Mut) {
1023 self.tys(infcx, ty_b, ty_a)?;
1024 }
1025 Ok(())
1026 }
1027
1028 (BaseTy::Ref(_, ty_a, Mutability::Mut), BaseTy::Ref(_, ty_b, Mutability::Mut)) => {
1029 if ty_a.is_slice()
1030 && let TyKind::Indexed(_, idx_a) = ty_a.kind()
1031 && let TyKind::Exists(bty_b) = ty_b.kind()
1032 {
1033 self.tys(infcx, ty_a, ty_b)?;
1038 self.tys(infcx, &bty_b.replace_bound_reft(idx_a), ty_a)
1039 } else {
1040 self.tys(infcx, ty_a, ty_b)?;
1041 self.tys(infcx, ty_b, ty_a)
1042 }
1043 }
1044 (BaseTy::Ref(_, ty_a, Mutability::Not), BaseTy::Ref(_, ty_b, Mutability::Not)) => {
1045 self.tys(infcx, ty_a, ty_b)
1046 }
1047 (BaseTy::Tuple(tys_a), BaseTy::Tuple(tys_b)) => {
1048 debug_assert_eq!(tys_a.len(), tys_b.len());
1049 for (ty_a, ty_b) in iter::zip(tys_a, tys_b) {
1050 self.tys(infcx, ty_a, ty_b)?;
1051 }
1052 Ok(())
1053 }
1054 (
1055 BaseTy::Alias(AliasTy {
1056 kind: AliasKind::Opaque { def_id: def_id_a },
1057 args: args_a,
1058 refine_args: refine_args_a,
1059 }),
1060 BaseTy::Alias(AliasTy {
1061 kind: AliasKind::Opaque { def_id: def_id_b },
1062 args: args_b,
1063 refine_args: refine_args_b,
1064 }),
1065 ) => {
1066 debug_assert_eq!(def_id_a, def_id_b);
1067
1068 for (ty_a, ty_b) in izip!(args_a.iter(), args_b.iter()) {
1070 self.generic_args(infcx, Invariant, ty_a, ty_b)?;
1071 }
1072
1073 debug_assert_eq!(refine_args_a.len(), refine_args_b.len());
1075 iter::zip(refine_args_a.iter(), refine_args_b.iter())
1076 .for_each(|(expr_a, expr_b)| infcx.unify_exprs(expr_a, expr_b));
1077
1078 Ok(())
1079 }
1080 (
1081 _,
1082 BaseTy::Alias(AliasTy { kind: AliasKind::Opaque { def_id }, args, refine_args }),
1083 ) => {
1084 self.handle_opaque_type(infcx, a, *def_id, args, refine_args)
1086 }
1087 (
1088 BaseTy::Alias(alias_ty_a @ AliasTy { kind: AliasKind::Projection { .. }, .. }),
1089 BaseTy::Alias(alias_ty_b @ AliasTy { kind: AliasKind::Projection { .. }, .. }),
1090 ) => {
1091 tracked_span_dbg_assert_eq!(alias_ty_a.erase_regions(), alias_ty_b.erase_regions());
1092 Ok(())
1093 }
1094 (BaseTy::Array(ty_a, len_a), BaseTy::Array(ty_b, len_b)) => {
1095 tracked_span_dbg_assert_eq!(len_a, len_b);
1096 self.tys(infcx, ty_a, ty_b)
1097 }
1098 (BaseTy::Param(param_a), BaseTy::Param(param_b)) => {
1099 debug_assert_eq!(param_a, param_b);
1100 Ok(())
1101 }
1102 (BaseTy::Bool, BaseTy::Bool)
1103 | (BaseTy::Str, BaseTy::Str)
1104 | (BaseTy::Char, BaseTy::Char)
1105 | (BaseTy::RawPtrMetadata(_), BaseTy::RawPtrMetadata(_)) => Ok(()),
1106 (BaseTy::Dynamic(preds_a, _), BaseTy::Dynamic(preds_b, _)) => {
1107 tracked_span_assert_eq!(preds_a.erase_regions(), preds_b.erase_regions());
1108 Ok(())
1109 }
1110 (BaseTy::Closure(did1, tys_a, _, _), BaseTy::Closure(did2, tys_b, _, _))
1111 if did1 == did2 =>
1112 {
1113 debug_assert_eq!(tys_a.len(), tys_b.len());
1114 for (ty_a, ty_b) in iter::zip(tys_a, tys_b) {
1115 self.tys(infcx, ty_a, ty_b)?;
1116 }
1117 Ok(())
1118 }
1119 (BaseTy::FnPtr(sig_a), BaseTy::FnPtr(sig_b)) => {
1120 if sig_a == sig_b {
1121 return Ok(());
1122 }
1123 let sub_sig = SubFn::Mono(sig_a.clone());
1124 check_fn_subtyping(infcx, &mut DummyEnv, sub_sig, sig_b, self.span)
1125 }
1126 (BaseTy::Never, BaseTy::Never) => Ok(()),
1127 (
1128 BaseTy::Coroutine(did1, resume_ty_a, tys_a, _),
1129 BaseTy::Coroutine(did2, resume_ty_b, tys_b, _),
1130 ) if did1 == did2 => {
1131 debug_assert_eq!(tys_a.len(), tys_b.len());
1132 for (ty_a, ty_b) in iter::zip(tys_a, tys_b) {
1133 self.tys(infcx, ty_a, ty_b)?;
1134 }
1135 self.tys(infcx, resume_ty_b, resume_ty_a)?;
1137 self.tys(infcx, resume_ty_a, resume_ty_b)?;
1138
1139 Ok(())
1140 }
1141 (BaseTy::Foreign(did_a), BaseTy::Foreign(did_b)) if did_a == did_b => Ok(()),
1142 _ => Err(query_bug!("incompatible base types: `{a:#?}` - `{b:#?}`"))?,
1143 }
1144 }
1145
1146 fn generic_args(
1147 &mut self,
1148 infcx: &mut InferCtxt,
1149 variance: Variance,
1150 a: &GenericArg,
1151 b: &GenericArg,
1152 ) -> InferResult {
1153 let (ty_a, ty_b) = match (a, b) {
1154 (GenericArg::Ty(ty_a), GenericArg::Ty(ty_b)) => (ty_a.clone(), ty_b.clone()),
1155 (GenericArg::Base(ctor_a), GenericArg::Base(ctor_b)) => {
1156 tracked_span_dbg_assert_eq!(
1157 ctor_a.sort().erase_regions(),
1158 ctor_b.sort().erase_regions()
1159 );
1160 (ctor_a.to_ty(), ctor_b.to_ty())
1161 }
1162 (GenericArg::Lifetime(_), GenericArg::Lifetime(_)) => return Ok(()),
1163 (GenericArg::Const(cst_a), GenericArg::Const(cst_b)) => {
1164 debug_assert_eq!(cst_a, cst_b);
1165 return Ok(());
1166 }
1167 _ => Err(query_bug!("incompatible generic args: `{a:?}` `{b:?}`"))?,
1168 };
1169 match variance {
1170 Variance::Covariant => self.tys(infcx, &ty_a, &ty_b),
1171 Variance::Invariant => {
1172 self.tys(infcx, &ty_a, &ty_b)?;
1173 self.tys(infcx, &ty_b, &ty_a)
1174 }
1175 Variance::Contravariant => self.tys(infcx, &ty_b, &ty_a),
1176 Variance::Bivariant => Ok(()),
1177 }
1178 }
1179
1180 fn idxs_eq(&mut self, infcx: &mut InferCtxt, a: &Expr, b: &Expr) {
1181 if a == b {
1182 return;
1183 }
1184 match (a.kind(), b.kind()) {
1185 (
1186 ExprKind::Ctor(Ctor::Struct(did_a), flds_a),
1187 ExprKind::Ctor(Ctor::Struct(did_b), flds_b),
1188 ) => {
1189 debug_assert_eq!(did_a, did_b);
1190 for (a, b) in iter::zip(flds_a, flds_b) {
1191 self.idxs_eq(infcx, a, b);
1192 }
1193 }
1194 (ExprKind::Tuple(flds_a), ExprKind::Tuple(flds_b)) => {
1195 for (a, b) in iter::zip(flds_a, flds_b) {
1196 self.idxs_eq(infcx, a, b);
1197 }
1198 }
1199 (ExprKind::Ctor(Ctor::RawPtr, flds_a), ExprKind::Ctor(Ctor::RawPtr, flds_b)) => {
1200 for (a, b) in iter::zip(flds_a, flds_b) {
1201 self.idxs_eq(infcx, a, b);
1202 }
1203 }
1204 (_, ExprKind::Tuple(flds_b)) => {
1205 for (f, b) in flds_b.iter().enumerate() {
1206 let proj = FieldProj::Tuple { arity: flds_b.len(), field: f as u32 };
1207 let a = a.proj_and_reduce(proj);
1208 self.idxs_eq(infcx, &a, b);
1209 }
1210 }
1211 (_, ExprKind::Ctor(Ctor::RawPtr, flds_b)) => {
1212 for (f, b) in flds_b.iter().enumerate() {
1213 let field = rty::RawPtrField::from_index(f as u32).unwrap();
1214 let a = a.proj_and_reduce(FieldProj::RawPtr { field });
1215 self.idxs_eq(infcx, &a, b);
1216 }
1217 }
1218
1219 (_, ExprKind::Ctor(Ctor::Struct(def_id), flds_b)) => {
1220 for (f, b) in flds_b.iter().enumerate() {
1221 let proj = FieldProj::Adt { def_id: *def_id, field: f as u32 };
1222 let a = a.proj_and_reduce(proj);
1223 self.idxs_eq(infcx, &a, b);
1224 }
1225 }
1226
1227 (ExprKind::Tuple(flds_a), _) => {
1228 infcx.unify_exprs(a, b);
1229 for (f, a) in flds_a.iter().enumerate() {
1230 let proj = FieldProj::Tuple { arity: flds_a.len(), field: f as u32 };
1231 let b = b.proj_and_reduce(proj);
1232 self.idxs_eq(infcx, a, &b);
1233 }
1234 }
1235 (ExprKind::Ctor(Ctor::RawPtr, flds_a), _) => {
1236 infcx.unify_exprs(a, b);
1237 for (f, a) in flds_a.iter().enumerate() {
1238 let field = rty::RawPtrField::from_index(f as u32).unwrap();
1239 let b = b.proj_and_reduce(FieldProj::RawPtr { field });
1240 self.idxs_eq(infcx, a, &b);
1241 }
1242 }
1243 (ExprKind::Ctor(Ctor::Struct(def_id), flds_a), _) => {
1244 infcx.unify_exprs(a, b);
1245 for (f, a) in flds_a.iter().enumerate() {
1246 let proj = FieldProj::Adt { def_id: *def_id, field: f as u32 };
1247 let b = b.proj_and_reduce(proj);
1248 self.idxs_eq(infcx, a, &b);
1249 }
1250 }
1251 (ExprKind::Abs(lam_a), ExprKind::Abs(lam_b)) => {
1252 self.abs_eq(infcx, lam_a, lam_b);
1253 }
1254 (_, ExprKind::Abs(lam_b)) => {
1255 self.abs_eq(infcx, &a.eta_expand_abs(lam_b.vars(), lam_b.output()), lam_b);
1256 }
1257 (ExprKind::Abs(lam_a), _) => {
1258 infcx.unify_exprs(a, b);
1259 self.abs_eq(infcx, lam_a, &b.eta_expand_abs(lam_a.vars(), lam_a.output()));
1260 }
1261 (ExprKind::KVar(_), _) | (_, ExprKind::KVar(_)) => {
1262 infcx.check_impl(a, b, self.tag());
1263 infcx.check_impl(b, a, self.tag());
1264 }
1265 _ => {
1266 infcx.unify_exprs(a, b);
1267 let span = b.span();
1268 infcx.check_pred(Expr::binary_op(rty::BinOp::Eq, a, b).at_opt(span), self.tag());
1269 }
1270 }
1271 }
1272
1273 fn abs_eq(&mut self, infcx: &mut InferCtxt, a: &Lambda, b: &Lambda) {
1274 debug_assert_eq!(a.vars().len(), b.vars().len());
1275 let vars = a
1276 .vars()
1277 .iter()
1278 .map(|kind| {
1279 let (sort, _, kind) = kind.expect_refine();
1280 Expr::fvar(infcx.define_bound_reft_var(sort, kind))
1281 })
1282 .collect_vec();
1283 let body_a = a.apply(&vars);
1284 let body_b = b.apply(&vars);
1285 self.idxs_eq(infcx, &body_a, &body_b);
1286 }
1287
1288 fn handle_opaque_type(
1289 &mut self,
1290 infcx: &mut InferCtxt,
1291 bty: &BaseTy,
1292 opaque_def_id: DefId,
1293 opaque_args: &rty::GenericArgs,
1294 opaque_refine_args: &rty::RefineArgs,
1295 ) -> InferResult {
1296 if let BaseTy::Coroutine(def_id, resume_ty, upvar_tys, args) = bty {
1297 let obligs = mk_coroutine_obligations(
1298 infcx.genv,
1299 def_id,
1300 resume_ty,
1301 upvar_tys,
1302 &opaque_def_id,
1303 args.clone(),
1304 )?;
1305 self.obligations.extend(obligs);
1306 } else {
1307 let bounds = infcx.genv.item_bounds(opaque_def_id)?.instantiate(
1308 infcx.tcx(),
1309 opaque_args,
1310 opaque_refine_args,
1311 );
1312 for clause in &bounds {
1313 if !clause.kind().vars().is_empty() {
1314 Err(query_bug!("handle_opaque_types: clause with bound vars: `{clause:?}`"))?;
1315 }
1316 if let rty::ClauseKind::Projection(pred) = clause.kind_skipping_binder() {
1317 let alias_ty = pred
1318 .projection_term
1319 .with_self_ty(bty.to_subset_ty_ctor())
1320 .to_alias_ty();
1321 let ty1 = BaseTy::Alias(alias_ty)
1322 .to_ty()
1323 .deeply_normalize(&mut infcx.at(self.span))?;
1324 let ty2 = pred.term.to_ty();
1325 self.tys(infcx, &ty1, &ty2)?;
1326 }
1327 }
1328 }
1329 Ok(())
1330 }
1331}
1332
1333fn mk_coroutine_obligations(
1334 genv: GlobalEnv,
1335 generator_did: &DefId,
1336 resume_ty: &Ty,
1337 upvar_tys: &List<Ty>,
1338 opaque_def_id: &DefId,
1339 args: flux_rustc_bridge::ty::GenericArgs,
1340) -> InferResult<Vec<Binder<rty::CoroutineObligPredicate>>> {
1341 let bounds = genv.item_bounds(*opaque_def_id)?.skip_binder();
1342 for bound in &bounds {
1343 if let Some(proj_clause) = bound.as_projection_clause() {
1344 return Ok(vec![proj_clause.map(|proj_clause| {
1345 let output = proj_clause.term;
1346 CoroutineObligPredicate {
1347 def_id: *generator_did,
1348 resume_ty: resume_ty.clone(),
1349 upvar_tys: upvar_tys.clone(),
1350 output: output.to_ty(),
1351 args,
1352 }
1353 })]);
1354 }
1355 }
1356 bug!("no projection predicate")
1357}
1358
1359#[derive(Debug)]
1360pub enum InferErr {
1361 UnsolvedEvar(EVid),
1362 Query(QueryErr),
1363}
1364
1365impl From<QueryErr> for InferErr {
1366 fn from(v: QueryErr) -> Self {
1367 Self::Query(v)
1368 }
1369}
1370
1371mod pretty {
1372 use std::fmt;
1373
1374 use flux_middle::pretty::*;
1375
1376 use super::*;
1377
1378 impl Pretty for Tag {
1379 fn fmt(&self, cx: &PrettyCx, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1380 w!(cx, f, "{:?} at {:?}", ^self.reason, self.src_span)?;
1381 if let Some(dst_span) = self.dst_span {
1382 w!(cx, f, " ({:?})", ^dst_span)?;
1383 }
1384 Ok(())
1385 }
1386 }
1387
1388 impl_debug_with_default_cx!(Tag);
1389}