flux_refineck/
checker.rs

1use std::{collections::hash_map::Entry, iter, vec};
2
3use flux_common::{
4    bug, dbg, dbg::SpanTrace, index::IndexVec, iter::IterExt, span_bug, tracked_span_bug,
5    tracked_span_dbg_assert_eq,
6};
7use flux_config::{self as config, InferOpts};
8use flux_infer::{
9    infer::{
10        ConstrReason, GlobalEnvExt as _, InferCtxt, InferCtxtRoot, InferResult, SubtypeReason,
11    },
12    projections::NormalizeExt as _,
13    refine_tree::{Marker, RefineCtxtTrace},
14};
15use flux_middle::{
16    global_env::GlobalEnv,
17    queries::{QueryResult, try_query},
18    query_bug,
19    rty::{
20        self, AdtDef, BaseTy, Binder, Bool, Clause, CoroutineObligPredicate, EarlyBinder, Expr,
21        FnOutput, FnSig, FnTraitPredicate, GenericArg, GenericArgsExt as _, Int, IntTy, Mutability,
22        Path, PolyFnSig, PtrKind, RefineArgs, RefineArgsExt,
23        Region::ReStatic,
24        Ty, TyKind, Uint, UintTy, VariantIdx,
25        fold::{TypeFoldable, TypeFolder, TypeSuperFoldable},
26        refining::{Refine, Refiner},
27    },
28};
29use flux_rustc_bridge::{
30    self, ToRustc,
31    mir::{
32        self, AggregateKind, AssertKind, BasicBlock, Body, BodyRoot, BorrowKind, CastKind,
33        Constant, Location, NonDivergingIntrinsic, Operand, Place, Rvalue, START_BLOCK, Statement,
34        StatementKind, Terminator, TerminatorKind, UnOp,
35    },
36    ty::{self, GenericArgsExt as _},
37};
38use itertools::{Itertools, izip};
39use rustc_data_structures::{graph::dominators::Dominators, unord::UnordMap};
40use rustc_hash::{FxHashMap, FxHashSet};
41use rustc_hir::{
42    LangItem,
43    def_id::{DefId, LocalDefId},
44};
45use rustc_index::{IndexSlice, bit_set::DenseBitSet};
46use rustc_infer::infer::TyCtxtInferExt;
47use rustc_middle::{
48    mir::{Promoted, SwitchTargets},
49    ty::{TyCtxt, TypeSuperVisitable as _, TypeVisitable as _, TypingMode},
50};
51use rustc_span::{
52    Span,
53    sym::{self},
54};
55
56use self::errors::{CheckerError, ResultExt};
57use crate::{
58    ghost_statements::{CheckerId, GhostStatement, GhostStatements, Point},
59    primops,
60    queue::WorkQueue,
61    rty::Char,
62    type_env::{BasicBlockEnv, BasicBlockEnvShape, PtrToRefBound, TypeEnv, TypeEnvTrace},
63};
64
65type Result<T = ()> = std::result::Result<T, CheckerError>;
66
67pub(crate) struct Checker<'ck, 'genv, 'tcx, M> {
68    genv: GlobalEnv<'genv, 'tcx>,
69    /// [`CheckerId`] of the function-like item being checked.
70    checker_id: CheckerId,
71    inherited: Inherited<'ck, M>,
72    body: &'ck Body<'tcx>,
73    /// The type used for the `resume` argument if we are checking a generator.
74    resume_ty: Option<Ty>,
75    output: Binder<FnOutput>,
76    /// A marker to the node in the refinement tree at the end of the basic block after applying
77    /// the effects of the terminator.
78    markers: IndexVec<BasicBlock, Option<Marker>>,
79    visited: DenseBitSet<BasicBlock>,
80    queue: WorkQueue<'ck>,
81    default_refiner: Refiner<'genv, 'tcx>,
82    /// The templates for the promoted bodies of the current function
83    promoted: &'ck IndexSlice<Promoted, Ty>,
84}
85
86/// Fields shared by the top-level function and its nested closure/generators
87struct Inherited<'ck, M> {
88    /// [`Expr`]s used to instantiate the early bound refinement parameters of the top-level function
89    /// signature
90    ghost_stmts: &'ck UnordMap<CheckerId, GhostStatements>,
91    mode: &'ck mut M,
92
93    /// This map has the "templates" generated for the closures constructed (in [`Checker::check_rvalue_closure`]).
94    /// The [`PolyFnSig`] can have free variables (inside the scope of kvars), so we need to be
95    /// careful and only use it in the correct scope.
96    closures: &'ck mut UnordMap<DefId, PolyFnSig>,
97}
98
99#[derive(Debug)]
100struct ResolvedCall {
101    output: Ty,
102    /// The refine arguments given to the call
103    _early_args: Vec<Expr>,
104    /// The refine arguments given to the call
105    _late_args: Vec<Expr>,
106}
107
108impl<'ck, M: Mode> Inherited<'ck, M> {
109    fn new(
110        mode: &'ck mut M,
111        ghost_stmts: &'ck UnordMap<CheckerId, GhostStatements>,
112        closures: &'ck mut UnordMap<DefId, PolyFnSig>,
113    ) -> Self {
114        Self { ghost_stmts, mode, closures }
115    }
116
117    fn reborrow(&mut self) -> Inherited<'_, M> {
118        Inherited { ghost_stmts: self.ghost_stmts, mode: self.mode, closures: self.closures }
119    }
120}
121
122pub(crate) trait Mode: Sized {
123    const NAME: &str;
124
125    fn enter_basic_block<'ck, 'genv, 'tcx>(
126        ck: &mut Checker<'ck, 'genv, 'tcx, Self>,
127        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
128        bb: BasicBlock,
129    ) -> TypeEnv<'ck>;
130
131    fn check_goto_join_point<'genv, 'tcx>(
132        ck: &mut Checker<'_, 'genv, 'tcx, Self>,
133        infcx: InferCtxt<'_, 'genv, 'tcx>,
134        env: TypeEnv,
135        terminator_span: Span,
136        target: BasicBlock,
137    ) -> Result<bool>;
138
139    fn clear(ck: &mut Checker<Self>, bb: BasicBlock);
140}
141
142pub(crate) struct ShapeMode {
143    bb_envs: FxHashMap<CheckerId, FxHashMap<BasicBlock, BasicBlockEnvShape>>,
144}
145
146pub(crate) struct RefineMode {
147    bb_envs: FxHashMap<CheckerId, FxHashMap<BasicBlock, BasicBlockEnv>>,
148}
149
150/// The result of running the shape phase.
151pub(crate) struct ShapeResult(FxHashMap<CheckerId, FxHashMap<BasicBlock, BasicBlockEnvShape>>);
152
153/// A `Guard` describes extra "control" information that holds at the start of a successor basic block
154#[derive(Debug)]
155enum Guard {
156    /// No extra information holds, e.g., for a plain goto.
157    None,
158    /// A predicate that can be assumed, e.g., in the branches of an if-then-else.
159    Pred(Expr),
160    /// The corresponding place was found to be of a particular variant.
161    Match(Place, VariantIdx),
162}
163
164impl<'genv, 'tcx> Checker<'_, 'genv, 'tcx, ShapeMode> {
165    pub(crate) fn run_in_shape_mode<'ck>(
166        genv: GlobalEnv<'genv, 'tcx>,
167        local_id: LocalDefId,
168        ghost_stmts: &'ck UnordMap<CheckerId, GhostStatements>,
169        closures: &'ck mut UnordMap<DefId, PolyFnSig>,
170        opts: InferOpts,
171    ) -> Result<ShapeResult> {
172        let def_id = local_id.to_def_id();
173        dbg::shape_mode_span!(genv.tcx(), local_id).in_scope(|| {
174            let span = genv.tcx().def_span(local_id);
175            let mut mode = ShapeMode { bb_envs: FxHashMap::default() };
176
177            let body = genv.mir(local_id).with_span(span)?;
178
179            // In shape mode we don't care about kvars
180            let mut root_ctxt = try_query(|| {
181                genv.infcx_root(&body.infcx, opts)
182                    .with_dummy_kvars()
183                    .identity_for_item(def_id)?
184                    .build()
185            })
186            .with_span(span)?;
187
188            let inherited = Inherited::new(&mut mode, ghost_stmts, closures);
189
190            let infcx = root_ctxt.infcx(def_id, &body.infcx);
191            let poly_sig = genv
192                .fn_sig(local_id)
193                .with_span(span)?
194                .instantiate_identity();
195            Checker::run(infcx, local_id, inherited, poly_sig)?;
196
197            Ok(ShapeResult(mode.bb_envs))
198        })
199    }
200}
201
202impl<'genv, 'tcx> Checker<'_, 'genv, 'tcx, RefineMode> {
203    pub(crate) fn run_in_refine_mode<'ck>(
204        genv: GlobalEnv<'genv, 'tcx>,
205        local_id: LocalDefId,
206        ghost_stmts: &'ck UnordMap<CheckerId, GhostStatements>,
207        closures: &'ck mut UnordMap<DefId, PolyFnSig>,
208        bb_env_shapes: ShapeResult,
209        opts: InferOpts,
210    ) -> Result<InferCtxtRoot<'genv, 'tcx>> {
211        let def_id = local_id.to_def_id();
212        let span = genv.tcx().def_span(def_id);
213
214        let body = genv.mir(local_id).with_span(span)?;
215        let mut root_ctxt = try_query(|| {
216            genv.infcx_root(&body.infcx, opts)
217                .identity_for_item(def_id)?
218                .build()
219        })
220        .with_span(span)?;
221        let bb_envs = bb_env_shapes.into_bb_envs(&mut root_ctxt);
222
223        dbg::refine_mode_span!(genv.tcx(), def_id, bb_envs).in_scope(|| {
224            // Check the body of the function def_id against its signature
225            let mut mode = RefineMode { bb_envs };
226            let inherited = Inherited::new(&mut mode, ghost_stmts, closures);
227            let infcx = root_ctxt.infcx(def_id, &body.infcx);
228            let poly_sig = genv.fn_sig(def_id).with_span(span)?;
229            let poly_sig = poly_sig.instantiate_identity();
230            Checker::run(infcx, local_id, inherited, poly_sig)?;
231
232            Ok(root_ctxt)
233        })
234    }
235}
236
237/// `SubFn` lets us reuse _most_ of the same code for `check_fn_subtyping` for both the case where
238/// we have an early-bound function signature (e.g., for a trait method???) and versions without,
239/// e.g. a plain closure against its FnTraitPredicate obligation.
240#[derive(Debug)]
241pub enum SubFn {
242    Poly(DefId, EarlyBinder<rty::PolyFnSig>, rty::GenericArgs),
243    Mono(rty::PolyFnSig),
244}
245
246impl SubFn {
247    pub fn as_ref(&self) -> &rty::PolyFnSig {
248        match self {
249            SubFn::Poly(_, sig, _) => sig.skip_binder_ref(),
250            SubFn::Mono(sig) => sig,
251        }
252    }
253}
254
255/// The function `check_fn_subtyping` does a function subtyping check between
256/// the sub-type (T_f) corresponding to the type of `def_id` @ `args` and the
257/// super-type (T_g) corresponding to the `oblig_sig`. This subtyping is handled
258/// as akin to the code
259///
260///   T_f := (S1,...,Sn) -> S
261///   T_g := (T1,...,Tn) -> T
262///   T_f <: T_g
263///
264///  fn g(x1:T1,...,xn:Tn) -> T {
265///      f(x1,...,xn)
266///  }
267fn check_fn_subtyping(
268    infcx: &mut InferCtxt,
269    sub_sig: SubFn,
270    super_sig: &rty::PolyFnSig,
271    span: Span,
272) -> InferResult {
273    let mut infcx = infcx.branch();
274    let mut infcx = infcx.at(span);
275    let tcx = infcx.genv.tcx();
276
277    let super_sig = super_sig
278        .replace_bound_vars(|_| rty::ReErased, |sort, _| Expr::fvar(infcx.define_var(sort)))
279        .deeply_normalize(&mut infcx)?;
280
281    // 1. Unpack `T_g` input types
282    let actuals = super_sig
283        .inputs()
284        .iter()
285        .map(|ty| infcx.unpack(ty))
286        .collect_vec();
287
288    let mut env = TypeEnv::empty();
289    let actuals = unfold_local_ptrs(&mut infcx, &mut env, sub_sig.as_ref(), &actuals)?;
290    let actuals = infer_under_mut_ref_hack(&mut infcx, &actuals[..], sub_sig.as_ref());
291
292    let output = infcx.ensure_resolved_evars(|infcx| {
293        // 2. Fresh names for `T_f` refine-params / Instantiate fn_def_sig and normalize it
294        // in subtyping_mono, skip next two steps...
295        let sub_sig = match sub_sig {
296            SubFn::Poly(def_id, early_sig, sub_args) => {
297                let refine_args = infcx.instantiate_refine_args(def_id, &sub_args)?;
298                early_sig.instantiate(tcx, &sub_args, &refine_args)
299            }
300            SubFn::Mono(sig) => sig,
301        };
302        // ... jump right here.
303        let sub_sig = sub_sig
304            .replace_bound_vars(|_| rty::ReErased, |sort, mode| infcx.fresh_infer_var(sort, mode))
305            .deeply_normalize(infcx)?;
306
307        // 3. INPUT subtyping (g-input <: f-input)
308        for requires in super_sig.requires() {
309            infcx.assume_pred(requires);
310        }
311        for (actual, formal) in iter::zip(actuals, sub_sig.inputs()) {
312            let reason = ConstrReason::Subtype(SubtypeReason::Input);
313            infcx.subtyping_with_env(&mut env, &actual, formal, reason)?;
314        }
315        // we check the requires AFTER the actual-formal subtyping as the above may unfold stuff in
316        // the actuals
317        for requires in sub_sig.requires() {
318            let reason = ConstrReason::Subtype(SubtypeReason::Requires);
319            infcx.check_pred(requires, reason);
320        }
321
322        Ok(sub_sig.output())
323    })?;
324
325    let output = infcx
326        .fully_resolve_evars(&output)
327        .replace_bound_refts_with(|sort, _, _| Expr::fvar(infcx.define_var(sort)));
328
329    // 4. OUTPUT subtyping (f_out <: g_out)
330    infcx.ensure_resolved_evars(|infcx| {
331        let super_output = super_sig
332            .output()
333            .replace_bound_refts_with(|sort, mode, _| infcx.fresh_infer_var(sort, mode));
334        let reason = ConstrReason::Subtype(SubtypeReason::Output);
335        infcx.subtyping(&output.ret, &super_output.ret, reason)?;
336
337        // 6. Update state with Output "ensures" and check super ensures
338        env.assume_ensures(infcx, &output.ensures, span);
339        fold_local_ptrs(infcx, &mut env, span)?;
340        env.check_ensures(
341            infcx,
342            &super_output.ensures,
343            ConstrReason::Subtype(SubtypeReason::Ensures),
344        )
345    })
346}
347
348/// Trait subtyping check, which makes sure that the type for an impl method (def_id)
349/// is a subtype of the corresponding trait method.
350pub(crate) fn trait_impl_subtyping<'genv, 'tcx>(
351    genv: GlobalEnv<'genv, 'tcx>,
352    def_id: LocalDefId,
353    opts: InferOpts,
354    span: Span,
355) -> InferResult<Option<InferCtxtRoot<'genv, 'tcx>>> {
356    let tcx = genv.tcx();
357
358    // Skip the check if this is not an impl method
359    let Some((impl_trait_ref, trait_method_id)) = find_trait_item(genv, def_id)? else {
360        return Ok(None);
361    };
362    let impl_method_id = def_id.to_def_id();
363    // Skip the check if either the trait-method or the impl-method are marked as `trusted_impl`
364    if genv.has_trusted_impl(trait_method_id) || genv.has_trusted_impl(impl_method_id) {
365        return Ok(None);
366    }
367
368    let impl_id = tcx.impl_of_assoc(impl_method_id).unwrap();
369    let impl_method_args = GenericArg::identity_for_item(genv, impl_method_id)?;
370    let trait_method_args = impl_method_args.rebase_onto(&tcx, impl_id, &impl_trait_ref.args);
371    let trait_refine_args = RefineArgs::identity_for_item(genv, trait_method_id)?;
372
373    let rustc_infcx = genv
374        .tcx()
375        .infer_ctxt()
376        .with_next_trait_solver(true)
377        .build(TypingMode::non_body_analysis());
378
379    let mut root_ctxt = genv
380        .infcx_root(&rustc_infcx, opts)
381        .with_const_generics(impl_id)?
382        .with_refinement_generics(trait_method_id, &trait_method_args)?
383        .build()?;
384
385    let mut infcx = root_ctxt.infcx(impl_method_id, &rustc_infcx);
386
387    let trait_fn_sig =
388        genv.fn_sig(trait_method_id)?
389            .instantiate(tcx, &trait_method_args, &trait_refine_args);
390    let impl_sig = genv.fn_sig(impl_method_id)?;
391    let sub_sig = SubFn::Poly(impl_method_id, impl_sig, impl_method_args);
392
393    check_fn_subtyping(&mut infcx, sub_sig, &trait_fn_sig, span)?;
394    Ok(Some(root_ctxt))
395}
396
397fn find_trait_item(
398    genv: GlobalEnv<'_, '_>,
399    def_id: LocalDefId,
400) -> QueryResult<Option<(rty::TraitRef, DefId)>> {
401    let tcx = genv.tcx();
402    let def_id = def_id.to_def_id();
403    if let Some(impl_id) = tcx.impl_of_assoc(def_id)
404        && let Some(impl_trait_ref) = genv.impl_trait_ref(impl_id)?
405    {
406        let impl_trait_ref = impl_trait_ref.instantiate_identity();
407        let trait_item_id = tcx.associated_item(def_id).trait_item_def_id().unwrap();
408        return Ok(Some((impl_trait_ref, trait_item_id)));
409    }
410    Ok(None)
411}
412
413/// Temporarily (around a function call) convert an `&mut` to an `&strg` to allow for the call to be
414/// checked. This is done by unfolding the `&mut` into a local pointer at the call-site and then
415/// folding the pointer back into the `&mut` upon return.
416/// See also [`fold_local_ptrs`].
417///
418/// ```text
419///             unpack(T) = T'
420/// ---------------------------------------[local-unfold]
421/// Γ ; &mut T => Γ, l:[<: T] T' ; ptr(l)
422/// ```
423fn unfold_local_ptrs(
424    infcx: &mut InferCtxt,
425    env: &mut TypeEnv,
426    fn_sig: &PolyFnSig,
427    actuals: &[Ty],
428) -> InferResult<Vec<Ty>> {
429    // We *only* need to know whether each input is a &strg or not
430    let fn_sig = fn_sig.skip_binder_ref();
431    let mut tys = vec![];
432    for (actual, input) in izip!(actuals, fn_sig.inputs()) {
433        let actual = if let (
434            TyKind::Indexed(BaseTy::Ref(re, bound, Mutability::Mut), _),
435            TyKind::StrgRef(_, _, _),
436        ) = (actual.kind(), input.kind())
437        {
438            let loc = env.unfold_local_ptr(infcx, bound)?;
439            let path1 = Path::new(loc, rty::List::empty());
440            Ty::ptr(PtrKind::Mut(*re), path1)
441        } else {
442            actual.clone()
443        };
444        tys.push(actual);
445    }
446    Ok(tys)
447}
448
449/// Fold local pointers implements roughly a rule like the following (for all local pointers)
450/// that converts the local pointers created via [`unfold_local_ptrs`] back into `&mut`.
451///
452/// ```text
453///       T1 <: T2
454/// --------------------- [local-fold]
455/// Γ, l:[<: T2] T1 => Γ
456/// ```
457fn fold_local_ptrs(infcx: &mut InferCtxt, env: &mut TypeEnv, span: Span) -> InferResult {
458    let mut at = infcx.at(span);
459    env.fold_local_ptrs(&mut at)
460}
461
462fn promoted_fn_sig(ty: &Ty) -> PolyFnSig {
463    let safety = rustc_hir::Safety::Safe;
464    let abi = rustc_abi::ExternAbi::Rust;
465    let requires = rty::List::empty();
466    let inputs = rty::List::empty();
467    let output =
468        Binder::bind_with_vars(FnOutput::new(ty.clone(), rty::List::empty()), rty::List::empty());
469    let fn_sig = crate::rty::FnSig::new(safety, abi, requires, inputs, output);
470    PolyFnSig::bind_with_vars(fn_sig, crate::rty::List::empty())
471}
472
473impl<'ck, 'genv, 'tcx, M: Mode> Checker<'ck, 'genv, 'tcx, M> {
474    fn new(
475        genv: GlobalEnv<'genv, 'tcx>,
476        checker_id: CheckerId,
477        inherited: Inherited<'ck, M>,
478        body: &'ck Body<'tcx>,
479        fn_sig: FnSig,
480        promoted: &'ck IndexSlice<Promoted, Ty>,
481    ) -> QueryResult<Self> {
482        let root_id = checker_id.root_id();
483
484        let resume_ty = if let CheckerId::DefId(def_id) = checker_id
485            && genv.tcx().is_coroutine(def_id.to_def_id())
486        {
487            Some(fn_sig.inputs()[1].clone())
488        } else {
489            None
490        };
491
492        let bb_len = body.basic_blocks.len();
493        Ok(Self {
494            checker_id,
495            genv,
496            inherited,
497            body,
498            resume_ty,
499            visited: DenseBitSet::new_empty(bb_len),
500            output: fn_sig.output().clone(),
501            markers: IndexVec::from_fn_n(|_| None, bb_len),
502            queue: WorkQueue::empty(bb_len, &body.dominator_order_rank),
503            default_refiner: Refiner::default_for_item(genv, root_id.to_def_id())?,
504            promoted,
505        })
506    }
507
508    fn check_body(
509        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
510        checker_id: CheckerId,
511        inherited: Inherited<'ck, M>,
512        body: &'ck Body<'tcx>,
513        poly_sig: PolyFnSig,
514        promoted: &'ck IndexSlice<Promoted, Ty>,
515    ) -> Result {
516        let span = body.span();
517
518        let fn_sig = poly_sig
519            .replace_bound_vars(|_| rty::ReErased, |sort, _| Expr::fvar(infcx.define_var(sort)))
520            .deeply_normalize(&mut infcx.at(span))
521            .with_span(span)?;
522
523        let mut env = TypeEnv::new(infcx, body, &fn_sig);
524
525        let mut ck = Checker::new(infcx.genv, checker_id, inherited, body, fn_sig, promoted)
526            .with_span(span)?;
527        ck.check_ghost_statements_at(infcx, &mut env, Point::FunEntry, span)?;
528
529        ck.check_goto(infcx.branch(), env, body.span(), START_BLOCK)?;
530
531        while let Some(bb) = ck.queue.pop() {
532            let visited = ck.visited.contains(bb);
533
534            if visited {
535                M::clear(&mut ck, bb);
536            }
537
538            let marker = ck.marker_at_dominator(bb);
539            let mut infcx = infcx.move_to(marker, visited);
540            let mut env = M::enter_basic_block(&mut ck, &mut infcx, bb);
541            env.unpack(&mut infcx);
542            ck.check_basic_block(infcx, env, bb)?;
543        }
544        Ok(())
545    }
546
547    /// Assign a template with fresh kvars to each promoted constant in `body_root`.
548    fn promoted_tys(
549        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
550        def_id: LocalDefId,
551        body_root: &BodyRoot<'tcx>,
552    ) -> QueryResult<IndexVec<Promoted, Ty>> {
553        let hole_refiner = Refiner::with_holes(infcx.genv, def_id.into())?;
554
555        body_root
556            .promoted
557            .iter()
558            .map(|body| {
559                Ok(body
560                    .return_ty()
561                    .refine(&hole_refiner)?
562                    .replace_holes(|binders, kind| infcx.fresh_infer_var_for_hole(binders, kind)))
563            })
564            .collect()
565    }
566
567    fn run(
568        mut infcx: InferCtxt<'_, 'genv, 'tcx>,
569        def_id: LocalDefId,
570        mut inherited: Inherited<'_, M>,
571        poly_sig: PolyFnSig,
572    ) -> Result {
573        let genv = infcx.genv;
574        let span = genv.tcx().def_span(def_id);
575        let body_root = genv.mir(def_id).with_span(span)?;
576
577        // 1. Generate templates for promoted consts
578        let promoted_tys = Self::promoted_tys(&mut infcx, def_id, &body_root).with_span(span)?;
579
580        // 2. Check the body of all promoted
581        for (promoted, ty) in promoted_tys.iter_enumerated() {
582            let body = &body_root.promoted[promoted];
583            let poly_sig = promoted_fn_sig(ty);
584            Checker::check_body(
585                &mut infcx,
586                CheckerId::Promoted(def_id, promoted),
587                inherited.reborrow(),
588                body,
589                poly_sig,
590                &promoted_tys,
591            )?;
592        }
593
594        // 3. Check the main body
595        Checker::check_body(
596            &mut infcx,
597            CheckerId::DefId(def_id),
598            inherited,
599            &body_root.body,
600            poly_sig,
601            &promoted_tys,
602        )
603    }
604
605    fn check_basic_block(
606        &mut self,
607        mut infcx: InferCtxt<'_, 'genv, 'tcx>,
608        mut env: TypeEnv,
609        bb: BasicBlock,
610    ) -> Result {
611        dbg::basic_block_start!(bb, infcx, env);
612
613        self.visited.insert(bb);
614        let data = &self.body.basic_blocks[bb];
615        let mut last_stmt_span = None;
616        let mut location = Location { block: bb, statement_index: 0 };
617        for stmt in &data.statements {
618            let span = stmt.source_info.span;
619            self.check_ghost_statements_at(
620                &mut infcx,
621                &mut env,
622                Point::BeforeLocation(location),
623                span,
624            )?;
625            bug::track_span(span, || {
626                dbg::statement!("start", stmt, &infcx, &env, span, &self);
627                self.check_statement(&mut infcx, &mut env, stmt)?;
628                dbg::statement!("end", stmt, &infcx, &env, span, &self);
629                Ok(())
630            })?;
631            if !stmt.is_nop() {
632                last_stmt_span = Some(span);
633            }
634            location = location.successor_within_block();
635        }
636
637        if let Some(terminator) = &data.terminator {
638            let span = terminator.source_info.span;
639            self.check_ghost_statements_at(
640                &mut infcx,
641                &mut env,
642                Point::BeforeLocation(location),
643                span,
644            )?;
645
646            bug::track_span(span, || {
647                dbg::terminator!("start", terminator, infcx, env);
648
649                let successors =
650                    self.check_terminator(&mut infcx, &mut env, terminator, last_stmt_span)?;
651                dbg::terminator!("end", terminator, infcx, env);
652
653                self.markers[bb] = Some(infcx.marker());
654                let term_span = last_stmt_span.unwrap_or(span);
655                self.check_successors(infcx, env, bb, term_span, successors)
656            })?;
657        }
658        Ok(())
659    }
660
661    fn check_assign_ty(
662        &mut self,
663        infcx: &mut InferCtxt,
664        env: &mut TypeEnv,
665        place: &Place,
666        ty: Ty,
667        span: Span,
668    ) -> InferResult {
669        let ty = infcx.hoister(true).hoist(&ty);
670        env.assign(&mut infcx.at(span), place, ty)
671    }
672
673    fn check_statement(
674        &mut self,
675        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
676        env: &mut TypeEnv,
677        stmt: &Statement,
678    ) -> Result {
679        let stmt_span = stmt.source_info.span;
680        match &stmt.kind {
681            StatementKind::Assign(place, rvalue) => {
682                let ty = self.check_rvalue(infcx, env, stmt_span, rvalue)?;
683                self.check_assign_ty(infcx, env, place, ty, stmt_span)
684                    .with_span(stmt_span)?;
685            }
686            StatementKind::SetDiscriminant { .. } => {
687                // TODO(nilehmann) double check here that the place is unfolded to
688                // the correct variant. This should be guaranteed by rustc
689            }
690            StatementKind::FakeRead(_) => {
691                // TODO(nilehmann) fake reads should be folding points
692            }
693            StatementKind::AscribeUserType(_, _) => {
694                // User ascriptions affect nll, but no refinement type checking.
695                // Maybe we can use this to associate refinement type to locals.
696            }
697            StatementKind::PlaceMention(_) => {
698                // Place mentions are a no-op used to detect uses of unsafe that would
699                // otherwise be optimized away.
700            }
701            StatementKind::Nop => {}
702            StatementKind::Intrinsic(NonDivergingIntrinsic::Assume(op)) => {
703                // Currently, we only have the `assume` intrinsic, which if we're to trust rustc should be a NOP.
704                // TODO: There may be a use-case to actually "assume" the bool index associated with the operand,
705                // i.e. to strengthen the `rcx` / `env` with the assumption that the bool-index is in fact `true`...
706                let _ = self
707                    .check_operand(infcx, env, stmt_span, op)
708                    .with_span(stmt_span)?;
709            }
710        }
711        Ok(())
712    }
713
714    fn is_exit_block(&self, bb: BasicBlock) -> bool {
715        let data = &self.body.basic_blocks[bb];
716        let is_no_op = data.statements.iter().all(Statement::is_nop);
717        let is_ret = match &data.terminator {
718            None => false,
719            Some(term) => term.is_return(),
720        };
721        is_no_op && is_ret
722    }
723
724    /// For `check_terminator`, the output `Vec<BasicBlock, Guard>` denotes,
725    /// - `BasicBlock` "successors" of the current terminator, and
726    /// - `Guard` are extra control information from, e.g. the `SwitchInt` (or `Assert`) you can assume when checking the corresponding successor.
727    fn check_terminator(
728        &mut self,
729        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
730        env: &mut TypeEnv,
731        terminator: &Terminator<'tcx>,
732        last_stmt_span: Option<Span>,
733    ) -> Result<Vec<(BasicBlock, Guard)>> {
734        let source_info = terminator.source_info;
735        let terminator_span = source_info.span;
736        match &terminator.kind {
737            TerminatorKind::Return => {
738                self.check_ret(infcx, env, last_stmt_span.unwrap_or(terminator_span))?;
739                Ok(vec![])
740            }
741            TerminatorKind::Unreachable => Ok(vec![]),
742            TerminatorKind::CoroutineDrop => Ok(vec![]),
743            TerminatorKind::Goto { target } => Ok(vec![(*target, Guard::None)]),
744            TerminatorKind::Yield { resume, resume_arg, .. } => {
745                if let Some(resume_ty) = self.resume_ty.clone() {
746                    self.check_assign_ty(infcx, env, resume_arg, resume_ty, terminator_span)
747                        .with_span(terminator_span)?;
748                } else {
749                    bug!("yield in non-generator function");
750                }
751                Ok(vec![(*resume, Guard::None)])
752            }
753            TerminatorKind::SwitchInt { discr, targets } => {
754                let discr_ty = self
755                    .check_operand(infcx, env, terminator_span, discr)
756                    .with_span(terminator_span)?;
757                if discr_ty.is_integral() || discr_ty.is_bool() || discr_ty.is_char() {
758                    Ok(Self::check_if(&discr_ty, targets))
759                } else {
760                    Ok(Self::check_match(infcx, env, &discr_ty, targets, terminator_span))
761                }
762            }
763            TerminatorKind::Call { kind, args, destination, target, .. } => {
764                let actuals = self
765                    .check_operands(infcx, env, terminator_span, args)
766                    .with_span(terminator_span)?;
767                let ret = match kind {
768                    mir::CallKind::FnDef { resolved_id, resolved_args, .. } => {
769                        let fn_sig = self.genv.fn_sig(*resolved_id).with_span(terminator_span)?;
770                        let generic_args = instantiate_args_for_fun_call(
771                            self.genv,
772                            self.checker_id.root_id().to_def_id(),
773                            *resolved_id,
774                            &resolved_args.lowered,
775                        )
776                        .with_span(terminator_span)?;
777                        self.check_call(
778                            infcx,
779                            env,
780                            terminator_span,
781                            Some(*resolved_id),
782                            fn_sig,
783                            &generic_args,
784                            &actuals,
785                        )?
786                        .output
787                    }
788                    mir::CallKind::FnPtr { operand, .. } => {
789                        let ty = self
790                            .check_operand(infcx, env, terminator_span, operand)
791                            .with_span(terminator_span)?;
792                        if let TyKind::Indexed(BaseTy::FnPtr(fn_sig), _) = infcx.unpack(&ty).kind()
793                        {
794                            self.check_call(
795                                infcx,
796                                env,
797                                terminator_span,
798                                None,
799                                EarlyBinder(fn_sig.clone()),
800                                &[],
801                                &actuals,
802                            )?
803                            .output
804                        } else {
805                            bug!("TODO: fnptr call {ty:?}")
806                        }
807                    }
808                };
809
810                let ret = infcx.unpack(&ret);
811                infcx.assume_invariants(&ret);
812
813                env.assign(&mut infcx.at(terminator_span), destination, ret)
814                    .with_span(terminator_span)?;
815
816                if let Some(target) = target {
817                    Ok(vec![(*target, Guard::None)])
818                } else {
819                    Ok(vec![])
820                }
821            }
822            TerminatorKind::Assert { cond, expected, target, msg } => {
823                Ok(vec![(
824                    *target,
825                    self.check_assert(infcx, env, terminator_span, cond, *expected, msg)
826                        .with_span(terminator_span)?,
827                )])
828            }
829            TerminatorKind::Drop { place, target, .. } => {
830                let _ = env.move_place(&mut infcx.at(terminator_span), place);
831                Ok(vec![(*target, Guard::None)])
832            }
833            TerminatorKind::FalseEdge { real_target, .. } => Ok(vec![(*real_target, Guard::None)]),
834            TerminatorKind::FalseUnwind { real_target, .. } => {
835                Ok(vec![(*real_target, Guard::None)])
836            }
837            TerminatorKind::UnwindResume => bug!("TODO: implement checking of cleanup code"),
838        }
839    }
840
841    fn check_ret(
842        &mut self,
843        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
844        env: &mut TypeEnv,
845        span: Span,
846    ) -> Result {
847        let obligations = infcx
848            .at(span)
849            .ensure_resolved_evars(|infcx| {
850                let ret_place_ty = env.lookup_place(infcx, Place::RETURN)?;
851                let output = self
852                    .output
853                    .replace_bound_refts_with(|sort, mode, _| infcx.fresh_infer_var(sort, mode));
854                let obligations = infcx.subtyping(&ret_place_ty, &output.ret, ConstrReason::Ret)?;
855
856                env.check_ensures(infcx, &output.ensures, ConstrReason::Ret)?;
857
858                Ok(obligations)
859            })
860            .with_span(span)?;
861
862        self.check_coroutine_obligations(infcx, obligations)
863    }
864
865    #[expect(clippy::too_many_arguments)]
866    fn check_call(
867        &mut self,
868        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
869        env: &mut TypeEnv,
870        span: Span,
871        callee_def_id: Option<DefId>,
872        fn_sig: EarlyBinder<PolyFnSig>,
873        generic_args: &[GenericArg],
874        actuals: &[Ty],
875    ) -> Result<ResolvedCall> {
876        let genv = self.genv;
877        let tcx = genv.tcx();
878
879        if M::NAME == "refine" {
880            let no_panic = genv.no_panic(self.checker_id.root_id());
881
882            if no_panic
883                && let Some(callee_def_id) = callee_def_id
884                && genv.def_kind(callee_def_id).is_fn_like()
885            {
886                let callee_no_panic = genv.no_panic(callee_def_id);
887                if !callee_no_panic {
888                    genv.sess().emit_err(errors::PanicError { span });
889                }
890            }
891        }
892
893        let actuals =
894            unfold_local_ptrs(infcx, env, fn_sig.skip_binder_ref(), actuals).with_span(span)?;
895        let actuals = infer_under_mut_ref_hack(infcx, &actuals, fn_sig.skip_binder_ref());
896        infcx.push_evar_scope();
897
898        // Replace holes in generic arguments with fresh inference variables
899        let generic_args = infcx.instantiate_generic_args(generic_args);
900
901        // Generate fresh inference variables for refinement arguments
902        let early_refine_args = match callee_def_id {
903            Some(callee_def_id) => {
904                infcx
905                    .instantiate_refine_args(callee_def_id, &generic_args)
906                    .with_span(span)?
907            }
908            None => rty::List::empty(),
909        };
910
911        let clauses = match callee_def_id {
912            Some(callee_def_id) => {
913                genv.predicates_of(callee_def_id)
914                    .with_span(span)?
915                    .predicates()
916                    .instantiate(tcx, &generic_args, &early_refine_args)
917            }
918            None => crate::rty::List::empty(),
919        };
920
921        let (clauses, fn_clauses) = Clause::split_off_fn_trait_clauses(self.genv, &clauses);
922        infcx
923            .at(span)
924            .check_non_closure_clauses(&clauses, ConstrReason::Call)
925            .with_span(span)?;
926
927        for fn_trait_pred in fn_clauses {
928            self.check_fn_trait_clause(infcx, &fn_trait_pred, span)?;
929        }
930
931        // Instantiate function signature and normalize it
932        let late_refine_args = vec![];
933        let fn_sig = fn_sig
934            .instantiate(tcx, &generic_args, &early_refine_args)
935            .replace_bound_vars(|_| rty::ReErased, |sort, mode| infcx.fresh_infer_var(sort, mode));
936
937        let fn_sig = fn_sig
938            .deeply_normalize(&mut infcx.at(span))
939            .with_span(span)?;
940
941        let mut at = infcx.at(span);
942
943        // Check requires predicates
944        for requires in fn_sig.requires() {
945            at.check_pred(requires, ConstrReason::Call);
946        }
947
948        // Check arguments
949        for (actual, formal) in iter::zip(actuals, fn_sig.inputs()) {
950            at.subtyping_with_env(env, &actual, formal, ConstrReason::Call)
951                .with_span(span)?;
952        }
953
954        infcx.pop_evar_scope().with_span(span)?;
955        env.fully_resolve_evars(infcx);
956
957        let output = infcx
958            .fully_resolve_evars(&fn_sig.output)
959            .replace_bound_refts_with(|sort, _, _| Expr::fvar(infcx.define_var(sort)));
960
961        env.assume_ensures(infcx, &output.ensures, span);
962        fold_local_ptrs(infcx, env, span).with_span(span)?;
963
964        Ok(ResolvedCall {
965            output: output.ret,
966            _early_args: early_refine_args
967                .into_iter()
968                .map(|arg| infcx.fully_resolve_evars(arg))
969                .collect(),
970            _late_args: late_refine_args
971                .into_iter()
972                .map(|arg| infcx.fully_resolve_evars(&arg))
973                .collect(),
974        })
975    }
976
977    fn check_coroutine_obligations(
978        &mut self,
979        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
980        obligs: Vec<Binder<CoroutineObligPredicate>>,
981    ) -> Result {
982        for oblig in obligs {
983            // FIXME(nilehmann) we shouldn't be skipping this binder
984            let oblig = oblig.skip_binder();
985
986            #[expect(clippy::disallowed_methods, reason = "coroutines cannot be extern speced")]
987            let def_id = oblig.def_id.expect_local();
988            let span = self.genv.tcx().def_span(def_id);
989            let body = self.genv.mir(def_id).with_span(span)?;
990            Checker::run(
991                infcx.change_item(def_id, &body.infcx),
992                def_id,
993                self.inherited.reborrow(),
994                oblig.to_poly_fn_sig(),
995            )?;
996        }
997        Ok(())
998    }
999
1000    fn find_self_ty_fn_sig(
1001        &self,
1002        self_ty: rustc_middle::ty::Ty<'tcx>,
1003        span: Span,
1004    ) -> Result<PolyFnSig> {
1005        let tcx = self.genv.tcx();
1006        let mut def_id = Some(self.checker_id.root_id().to_def_id());
1007        while let Some(did) = def_id {
1008            let generic_predicates = self
1009                .genv
1010                .predicates_of(did)
1011                .with_span(span)?
1012                .instantiate_identity();
1013            let predicates = generic_predicates.predicates;
1014
1015            for poly_fn_trait_pred in Clause::split_off_fn_trait_clauses(self.genv, &predicates).1 {
1016                if poly_fn_trait_pred.skip_binder_ref().self_ty.to_rustc(tcx) == self_ty {
1017                    return Ok(poly_fn_trait_pred.map(|fn_trait_pred| fn_trait_pred.fndef_sig()));
1018                }
1019            }
1020            // Continue to the parent if we didn't find a match
1021            def_id = generic_predicates.parent;
1022        }
1023
1024        span_bug!(
1025            span,
1026            "cannot find self_ty_fn_sig for {:?} with self_ty = {self_ty:?}",
1027            self.checker_id
1028        );
1029    }
1030
1031    fn check_fn_trait_clause(
1032        &mut self,
1033        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1034        poly_fn_trait_pred: &Binder<FnTraitPredicate>,
1035        span: Span,
1036    ) -> Result {
1037        let self_ty = poly_fn_trait_pred
1038            .skip_binder_ref()
1039            .self_ty
1040            .as_bty_skipping_existentials();
1041        let oblig_sig = poly_fn_trait_pred.map_ref(|fn_trait_pred| fn_trait_pred.fndef_sig());
1042        match self_ty {
1043            Some(BaseTy::Closure(def_id, _, _)) => {
1044                let Some(poly_sig) = self.inherited.closures.get(def_id).cloned() else {
1045                    span_bug!(span, "missing template for closure {def_id:?}");
1046                };
1047                check_fn_subtyping(infcx, SubFn::Mono(poly_sig.clone()), &oblig_sig, span)
1048                    .with_span(span)?;
1049            }
1050            Some(BaseTy::FnDef(def_id, args)) => {
1051                // Generates "function subtyping" obligations between the (super-type) `oblig_sig` in the `fn_trait_pred`
1052                // and the (sub-type) corresponding to the signature of `def_id + args`.
1053                // See `tests/neg/surface/fndef00.rs`
1054                let sub_sig = self.genv.fn_sig(def_id).with_span(span)?;
1055                check_fn_subtyping(
1056                    infcx,
1057                    SubFn::Poly(*def_id, sub_sig, args.clone()),
1058                    &oblig_sig,
1059                    span,
1060                )
1061                .with_span(span)?;
1062            }
1063            Some(BaseTy::FnPtr(sub_sig)) => {
1064                check_fn_subtyping(infcx, SubFn::Mono(sub_sig.clone()), &oblig_sig, span)
1065                    .with_span(span)?;
1066            }
1067
1068            // Some(self_ty) => {
1069            Some(self_ty @ BaseTy::Param(_)) => {
1070                // Step 1. Find matching clause and turn it into a FnSig
1071                let tcx = self.genv.tcx();
1072                let self_ty = self_ty.to_rustc(tcx);
1073                let sub_sig = self.find_self_ty_fn_sig(self_ty, span)?;
1074                // Step 2. Issue the subtyping
1075                check_fn_subtyping(infcx, SubFn::Mono(sub_sig), &oblig_sig, span)
1076                    .with_span(span)?;
1077            }
1078            _ => {}
1079        }
1080        Ok(())
1081    }
1082
1083    fn check_assert(
1084        &mut self,
1085        infcx: &mut InferCtxt,
1086        env: &mut TypeEnv,
1087        terminator_span: Span,
1088        cond: &Operand,
1089        expected: bool,
1090        msg: &AssertKind,
1091    ) -> InferResult<Guard> {
1092        let ty = self.check_operand(infcx, env, terminator_span, cond)?;
1093        let TyKind::Indexed(BaseTy::Bool, idx) = ty.kind() else {
1094            tracked_span_bug!("unexpected ty `{ty:?}`");
1095        };
1096        let pred = if expected { idx.clone() } else { idx.not() };
1097
1098        let msg = match msg {
1099            AssertKind::DivisionByZero => "possible division by zero",
1100            AssertKind::BoundsCheck => "possible out-of-bounds access",
1101            AssertKind::RemainderByZero => "possible remainder with a divisor of zero",
1102            AssertKind::Overflow(mir::BinOp::Div) => "possible division with overflow",
1103            AssertKind::Overflow(mir::BinOp::Rem) => "possible reminder with overflow",
1104            AssertKind::Overflow(_) => return Ok(Guard::Pred(pred)),
1105        };
1106        infcx
1107            .at(terminator_span)
1108            .check_pred(&pred, ConstrReason::Assert(msg));
1109        Ok(Guard::Pred(pred))
1110    }
1111
1112    /// Checks conditional branching as in a `match` statement. [`SwitchTargets`](https://doc.rust-lang.org/nightly/nightly-rustc/stable_mir/mir/struct.SwitchTargets.html) contains a list of branches - the exact bit value which is being compared and the block to jump to. Using the conditionals, each branch can be checked using the new control flow information.
1113    /// See <https://github.com/flux-rs/flux/pull/840#discussion_r1786543174>
1114    fn check_if(discr_ty: &Ty, targets: &SwitchTargets) -> Vec<(BasicBlock, Guard)> {
1115        let mk = |bits| {
1116            match discr_ty.kind() {
1117                TyKind::Indexed(BaseTy::Bool, idx) => {
1118                    if bits == 0 {
1119                        idx.not()
1120                    } else {
1121                        idx.clone()
1122                    }
1123                }
1124                TyKind::Indexed(bty @ (BaseTy::Int(_) | BaseTy::Uint(_) | BaseTy::Char), idx) => {
1125                    Expr::eq(idx.clone(), Expr::from_bits(bty, bits))
1126                }
1127                _ => tracked_span_bug!("unexpected discr_ty {:?}", discr_ty),
1128            }
1129        };
1130
1131        let mut successors = vec![];
1132
1133        for (bits, bb) in targets.iter() {
1134            successors.push((bb, Guard::Pred(mk(bits))));
1135        }
1136        let otherwise = Expr::and_from_iter(targets.iter().map(|(bits, _)| mk(bits).not()));
1137        successors.push((targets.otherwise(), Guard::Pred(otherwise)));
1138
1139        successors
1140    }
1141
1142    fn check_match(
1143        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1144        env: &mut TypeEnv,
1145        discr_ty: &Ty,
1146        targets: &SwitchTargets,
1147        span: Span,
1148    ) -> Vec<(BasicBlock, Guard)> {
1149        let (adt_def, place) = discr_ty.expect_discr();
1150        let idx = if let Ok(ty) = env.lookup_place(&mut infcx.at(span), place)
1151            && let TyKind::Indexed(_, idx) = ty.kind()
1152        {
1153            Some(idx.clone())
1154        } else {
1155            None
1156        };
1157
1158        let mut successors = vec![];
1159        let mut remaining: FxHashMap<u128, VariantIdx> = adt_def
1160            .discriminants()
1161            .map(|(idx, discr)| (discr, idx))
1162            .collect();
1163        for (bits, bb) in targets.iter() {
1164            let variant_idx = remaining
1165                .remove(&bits)
1166                .expect("value doesn't correspond to any variant");
1167            successors.push((bb, Guard::Match(place.clone(), variant_idx)));
1168        }
1169        let guard = if remaining.len() == 1 {
1170            // If there's only one variant left, we know for sure that this is the one, so can force an unfold
1171            let (_, variant_idx) = remaining
1172                .into_iter()
1173                .next()
1174                .unwrap_or_else(|| tracked_span_bug!());
1175            Guard::Match(place.clone(), variant_idx)
1176        } else if adt_def.sort_def().is_reflected()
1177            && let Some(idx) = idx
1178        {
1179            // If there's more than one variant left, we can only assume the `is_ctor` holds for one of them
1180            let mut cases = vec![];
1181            for (_, variant_idx) in remaining {
1182                let did = adt_def.did();
1183                cases.push(rty::Expr::is_ctor(did, variant_idx, idx.clone()));
1184            }
1185            Guard::Pred(Expr::or_from_iter(cases))
1186        } else {
1187            Guard::None
1188        };
1189        successors.push((targets.otherwise(), guard));
1190
1191        successors
1192    }
1193
1194    fn check_successors(
1195        &mut self,
1196        mut infcx: InferCtxt<'_, 'genv, 'tcx>,
1197        env: TypeEnv,
1198        from: BasicBlock,
1199        terminator_span: Span,
1200        successors: Vec<(BasicBlock, Guard)>,
1201    ) -> Result {
1202        for (target, guard) in successors {
1203            let mut infcx = infcx.branch();
1204            let mut env = env.clone();
1205            match guard {
1206                Guard::None => {}
1207                Guard::Pred(expr) => {
1208                    infcx.assume_pred(&expr);
1209                }
1210                Guard::Match(place, variant_idx) => {
1211                    env.downcast(&mut infcx.at(terminator_span), &place, variant_idx)
1212                        .with_span(terminator_span)?;
1213                }
1214            }
1215            self.check_ghost_statements_at(
1216                &mut infcx,
1217                &mut env,
1218                Point::Edge(from, target),
1219                terminator_span,
1220            )?;
1221            self.check_goto(infcx, env, terminator_span, target)?;
1222        }
1223        Ok(())
1224    }
1225
1226    fn check_goto(
1227        &mut self,
1228        mut infcx: InferCtxt<'_, 'genv, 'tcx>,
1229        mut env: TypeEnv,
1230        span: Span,
1231        target: BasicBlock,
1232    ) -> Result {
1233        if self.is_exit_block(target) {
1234            let location = self.body.terminator_loc(target);
1235            self.check_ghost_statements_at(
1236                &mut infcx,
1237                &mut env,
1238                Point::BeforeLocation(location),
1239                span,
1240            )?;
1241            self.check_ret(&mut infcx, &mut env, span)
1242        } else if self.body.is_join_point(target) {
1243            if M::check_goto_join_point(self, infcx, env, span, target)? {
1244                self.queue.insert(target);
1245            }
1246            Ok(())
1247        } else {
1248            self.check_basic_block(infcx, env, target)
1249        }
1250    }
1251
1252    fn closure_template(
1253        &mut self,
1254        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1255        env: &mut TypeEnv,
1256        stmt_span: Span,
1257        args: &flux_rustc_bridge::ty::GenericArgs,
1258        operands: &[Operand],
1259    ) -> InferResult<(Vec<Ty>, PolyFnSig)> {
1260        let upvar_tys = self
1261            .check_operands(infcx, env, stmt_span, operands)?
1262            .into_iter()
1263            .map(|ty| {
1264                if let TyKind::Ptr(PtrKind::Mut(re), path) = ty.kind() {
1265                    env.ptr_to_ref(
1266                        &mut infcx.at(stmt_span),
1267                        ConstrReason::Other,
1268                        *re,
1269                        path,
1270                        PtrToRefBound::Infer,
1271                    )
1272                } else {
1273                    Ok(ty.clone())
1274                }
1275            })
1276            .try_collect_vec()?;
1277
1278        let closure_args = args.as_closure();
1279        let ty = closure_args.sig_as_fn_ptr_ty();
1280
1281        if let flux_rustc_bridge::ty::TyKind::FnPtr(poly_sig) = ty.kind() {
1282            let poly_sig = poly_sig.unpack_closure_sig();
1283            let poly_sig = self.refine_with_holes(&poly_sig)?;
1284            let poly_sig = poly_sig.hoist_input_binders();
1285            let poly_sig = poly_sig
1286                .replace_holes(|binders, kind| infcx.fresh_infer_var_for_hole(binders, kind));
1287
1288            Ok((upvar_tys, poly_sig))
1289        } else {
1290            bug!("check_rvalue: closure: expected fn_ptr ty, found {ty:?} in {args:?}");
1291        }
1292    }
1293
1294    fn check_closure_body(
1295        &mut self,
1296        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1297        did: &DefId,
1298        upvar_tys: &[Ty],
1299        args: &flux_rustc_bridge::ty::GenericArgs,
1300        poly_sig: &PolyFnSig,
1301    ) -> Result {
1302        let genv = self.genv;
1303        let tcx = genv.tcx();
1304        #[expect(clippy::disallowed_methods, reason = "closures cannot be extern speced")]
1305        let closure_id = did.expect_local();
1306        let span = tcx.def_span(closure_id);
1307        let body = genv.mir(closure_id).with_span(span)?;
1308        let closure_sig = rty::to_closure_sig(tcx, closure_id, upvar_tys, args, poly_sig);
1309        Checker::run(
1310            infcx.change_item(closure_id, &body.infcx),
1311            closure_id,
1312            self.inherited.reborrow(),
1313            closure_sig,
1314        )
1315    }
1316
1317    fn check_rvalue_closure(
1318        &mut self,
1319        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1320        env: &mut TypeEnv,
1321        stmt_span: Span,
1322        did: &DefId,
1323        args: &flux_rustc_bridge::ty::GenericArgs,
1324        operands: &[Operand],
1325    ) -> Result<Ty> {
1326        // (1) Create the closure template
1327        let (upvar_tys, poly_sig) = self
1328            .closure_template(infcx, env, stmt_span, args, operands)
1329            .with_span(stmt_span)?;
1330        // (2) Check the closure body against the template
1331        self.check_closure_body(infcx, did, &upvar_tys, args, &poly_sig)?;
1332        // (3) "Save" the closure type in the `closures` map
1333        self.inherited.closures.insert(*did, poly_sig);
1334        // (4) Return the closure type
1335        Ok(Ty::closure(*did, upvar_tys, args))
1336    }
1337
1338    fn check_rvalue(
1339        &mut self,
1340        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1341        env: &mut TypeEnv,
1342        stmt_span: Span,
1343        rvalue: &Rvalue,
1344    ) -> Result<Ty> {
1345        let genv = self.genv;
1346        match rvalue {
1347            Rvalue::Use(operand) => {
1348                self.check_operand(infcx, env, stmt_span, operand)
1349                    .with_span(stmt_span)
1350            }
1351            Rvalue::Repeat(operand, c) => {
1352                let ty = self
1353                    .check_operand(infcx, env, stmt_span, operand)
1354                    .with_span(stmt_span)?;
1355                Ok(Ty::array(ty, c.clone()))
1356            }
1357            Rvalue::Ref(r, BorrowKind::Mut { .. }, place) => {
1358                env.borrow(&mut infcx.at(stmt_span), *r, Mutability::Mut, place)
1359                    .with_span(stmt_span)
1360            }
1361            Rvalue::Ref(r, BorrowKind::Shared | BorrowKind::Fake(..), place) => {
1362                env.borrow(&mut infcx.at(stmt_span), *r, Mutability::Not, place)
1363                    .with_span(stmt_span)
1364            }
1365
1366            Rvalue::RawPtr(mir::RawPtrKind::FakeForPtrMetadata, place) => {
1367                // see tests/tests/neg/surface/slice02.rs for what happens without unfolding here.
1368                env.unfold(infcx, place, stmt_span).with_span(stmt_span)?;
1369                let ty = env
1370                    .lookup_place(&mut infcx.at(stmt_span), place)
1371                    .with_span(stmt_span)?;
1372                let ty = BaseTy::RawPtrMetadata(ty).to_ty();
1373                Ok(ty)
1374            }
1375            Rvalue::RawPtr(kind, place) => {
1376                // ignore any refinements on the type stored at place
1377                let ty = &env.lookup_rust_ty(genv, place).with_span(stmt_span)?;
1378                let ty = self.refine_default(ty).with_span(stmt_span)?;
1379                let ty = BaseTy::RawPtr(ty, kind.to_mutbl_lossy()).to_ty();
1380                Ok(ty)
1381            }
1382            Rvalue::Cast(kind, op, to) => {
1383                let from = self
1384                    .check_operand(infcx, env, stmt_span, op)
1385                    .with_span(stmt_span)?;
1386                self.check_cast(infcx, env, stmt_span, *kind, &from, to)
1387                    .with_span(stmt_span)
1388            }
1389            Rvalue::BinaryOp(bin_op, op1, op2) => {
1390                self.check_binary_op(infcx, env, stmt_span, *bin_op, op1, op2)
1391                    .with_span(stmt_span)
1392            }
1393            Rvalue::NullaryOp(null_op, ty) => Ok(self.check_nullary_op(*null_op, ty)),
1394            Rvalue::UnaryOp(UnOp::PtrMetadata, Operand::Copy(place))
1395            | Rvalue::UnaryOp(UnOp::PtrMetadata, Operand::Move(place)) => {
1396                self.check_raw_ptr_metadata(infcx, env, stmt_span, place)
1397            }
1398            Rvalue::UnaryOp(un_op, op) => {
1399                self.check_unary_op(infcx, env, stmt_span, *un_op, op)
1400                    .with_span(stmt_span)
1401            }
1402            Rvalue::Discriminant(place) => {
1403                let ty = env
1404                    .lookup_place(&mut infcx.at(stmt_span), place)
1405                    .with_span(stmt_span)?;
1406                // HACK(nilehmann, mut-ref-unfolding) place should be unfolded here.
1407                let (adt_def, ..) = ty
1408                    .as_bty_skipping_existentials()
1409                    .unwrap_or_else(|| tracked_span_bug!())
1410                    .expect_adt();
1411                Ok(Ty::discr(adt_def.clone(), place.clone()))
1412            }
1413            Rvalue::Aggregate(
1414                AggregateKind::Adt(def_id, variant_idx, args, _, field_idx),
1415                operands,
1416            ) => {
1417                let actuals = self
1418                    .check_operands(infcx, env, stmt_span, operands)
1419                    .with_span(stmt_span)?;
1420                let sig = genv
1421                    .variant_sig(*def_id, *variant_idx)
1422                    .with_span(stmt_span)?
1423                    .ok_or_query_err(*def_id)
1424                    .with_span(stmt_span)?
1425                    .to_poly_fn_sig(*field_idx);
1426
1427                let args = instantiate_args_for_constructor(
1428                    genv,
1429                    self.checker_id.root_id().to_def_id(),
1430                    *def_id,
1431                    args,
1432                )
1433                .with_span(stmt_span)?;
1434                self.check_call(infcx, env, stmt_span, Some(*def_id), sig, &args, &actuals)
1435                    .map(|resolved_call| resolved_call.output)
1436            }
1437            Rvalue::Aggregate(AggregateKind::Array(arr_ty), operands) => {
1438                let args = self
1439                    .check_operands(infcx, env, stmt_span, operands)
1440                    .with_span(stmt_span)?;
1441                let arr_ty = self.refine_with_holes(arr_ty).with_span(stmt_span)?;
1442                self.check_mk_array(infcx, env, stmt_span, &args, arr_ty)
1443                    .with_span(stmt_span)
1444            }
1445            Rvalue::Aggregate(AggregateKind::Tuple, args) => {
1446                let tys = self
1447                    .check_operands(infcx, env, stmt_span, args)
1448                    .with_span(stmt_span)?;
1449                Ok(Ty::tuple(tys))
1450            }
1451            Rvalue::Aggregate(AggregateKind::Closure(did, args), operands) => {
1452                self.check_rvalue_closure(infcx, env, stmt_span, did, args, operands)
1453            }
1454            Rvalue::Aggregate(AggregateKind::Coroutine(did, args), ops) => {
1455                let args = args.as_coroutine();
1456                let resume_ty = self.refine_default(args.resume_ty()).with_span(stmt_span)?;
1457                let upvar_tys = self
1458                    .check_operands(infcx, env, stmt_span, ops)
1459                    .with_span(stmt_span)?;
1460                Ok(Ty::coroutine(*did, resume_ty, upvar_tys.into()))
1461            }
1462            Rvalue::ShallowInitBox(operand, _) => {
1463                self.check_operand(infcx, env, stmt_span, operand)
1464                    .with_span(stmt_span)?;
1465                Ty::mk_box_with_default_alloc(self.genv, Ty::uninit()).with_span(stmt_span)
1466            }
1467        }
1468    }
1469
1470    fn check_raw_ptr_metadata(
1471        &mut self,
1472        infcx: &mut InferCtxt,
1473        env: &mut TypeEnv,
1474        stmt_span: Span,
1475        place: &Place,
1476    ) -> Result<Ty> {
1477        let ty = env
1478            .lookup_place(&mut infcx.at(stmt_span), place)
1479            .with_span(stmt_span)?;
1480        let ty = match ty.kind() {
1481            TyKind::Indexed(BaseTy::RawPtrMetadata(ty), _)
1482            | TyKind::Indexed(BaseTy::Ref(_, ty, _), _) => ty,
1483            _ => tracked_span_bug!("check_metadata: bug! unexpected type `{ty:?}`"),
1484        };
1485        match ty.kind() {
1486            TyKind::Indexed(BaseTy::Array(_, len), _) => {
1487                let idx = Expr::from_const(self.genv.tcx(), len);
1488                Ok(Ty::indexed(BaseTy::Uint(UintTy::Usize), idx))
1489            }
1490            TyKind::Indexed(BaseTy::Slice(_), len) => {
1491                Ok(Ty::indexed(BaseTy::Uint(UintTy::Usize), len.clone()))
1492            }
1493            _ => Ok(Ty::unit()),
1494        }
1495    }
1496
1497    fn check_binary_op(
1498        &mut self,
1499        infcx: &mut InferCtxt,
1500        env: &mut TypeEnv,
1501        stmt_span: Span,
1502        bin_op: mir::BinOp,
1503        op1: &Operand,
1504        op2: &Operand,
1505    ) -> InferResult<Ty> {
1506        let ty1 = self.check_operand(infcx, env, stmt_span, op1)?;
1507        let ty2 = self.check_operand(infcx, env, stmt_span, op2)?;
1508
1509        match (ty1.kind(), ty2.kind()) {
1510            (TyKind::Indexed(bty1, idx1), TyKind::Indexed(bty2, idx2)) => {
1511                let rule =
1512                    primops::match_bin_op(bin_op, bty1, idx1, bty2, idx2, infcx.check_overflow);
1513                if let Some(pre) = rule.precondition {
1514                    infcx.at(stmt_span).check_pred(pre.pred, pre.reason);
1515                }
1516
1517                Ok(rule.output_type)
1518            }
1519            _ => tracked_span_bug!("incompatible types: `{ty1:?}` `{ty2:?}`"),
1520        }
1521    }
1522
1523    fn check_nullary_op(&self, null_op: mir::NullOp, _ty: &ty::Ty) -> Ty {
1524        match null_op {
1525            mir::NullOp::SizeOf | mir::NullOp::AlignOf => {
1526                // We could try to get the layout of type to index this with the actual value, but
1527                // this enough for now. Revisit if we ever need the precision.
1528                Ty::uint(UintTy::Usize)
1529            }
1530        }
1531    }
1532
1533    fn check_unary_op(
1534        &mut self,
1535        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1536        env: &mut TypeEnv,
1537        stmt_span: Span,
1538        un_op: mir::UnOp,
1539        op: &Operand,
1540    ) -> InferResult<Ty> {
1541        let ty = self.check_operand(infcx, env, stmt_span, op)?;
1542        match ty.kind() {
1543            TyKind::Indexed(bty, idx) => {
1544                let rule = primops::match_un_op(un_op, bty, idx, infcx.check_overflow);
1545                if let Some(pre) = rule.precondition {
1546                    infcx.at(stmt_span).check_pred(pre.pred, pre.reason);
1547                }
1548                Ok(rule.output_type)
1549            }
1550            _ => tracked_span_bug!("invalid type for unary operator `{un_op:?}` `{ty:?}`"),
1551        }
1552    }
1553
1554    fn check_mk_array(
1555        &mut self,
1556        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1557        env: &mut TypeEnv,
1558        stmt_span: Span,
1559        args: &[Ty],
1560        arr_ty: Ty,
1561    ) -> InferResult<Ty> {
1562        let arr_ty = infcx.ensure_resolved_evars(|infcx| {
1563            let arr_ty =
1564                arr_ty.replace_holes(|binders, kind| infcx.fresh_infer_var_for_hole(binders, kind));
1565
1566            let (arr_ty, pred) = arr_ty.unconstr();
1567            let mut at = infcx.at(stmt_span);
1568            at.check_pred(&pred, ConstrReason::Other);
1569            for ty in args {
1570                at.subtyping_with_env(env, ty, &arr_ty, ConstrReason::Other)?;
1571            }
1572            Ok(arr_ty)
1573        })?;
1574        let arr_ty = infcx.fully_resolve_evars(&arr_ty);
1575
1576        Ok(Ty::array(arr_ty, rty::Const::from_usize(self.genv.tcx(), args.len())))
1577    }
1578
1579    fn check_cast(
1580        &self,
1581        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1582        env: &mut TypeEnv,
1583        stmt_span: Span,
1584        kind: CastKind,
1585        from: &Ty,
1586        to: &ty::Ty,
1587    ) -> InferResult<Ty> {
1588        use ty::TyKind as RustTy;
1589        let ty = match kind {
1590            CastKind::PointerExposeProvenance => {
1591                match to.kind() {
1592                    RustTy::Int(int_ty) => Ty::int(*int_ty),
1593                    RustTy::Uint(uint_ty) => Ty::uint(*uint_ty),
1594                    _ => tracked_span_bug!("unsupported PointerExposeProvenance cast"),
1595                }
1596            }
1597            CastKind::IntToInt => {
1598                match (from.kind(), to.kind()) {
1599                    (Bool!(idx), RustTy::Int(int_ty)) => bool_int_cast(idx, *int_ty),
1600                    (Bool!(idx), RustTy::Uint(uint_ty)) => bool_uint_cast(idx, *uint_ty),
1601                    (Int!(int_ty1, idx), RustTy::Int(int_ty2)) => {
1602                        int_int_cast(idx, *int_ty1, *int_ty2)
1603                    }
1604                    (Uint!(uint_ty1, idx), RustTy::Uint(uint_ty2)) => {
1605                        uint_uint_cast(idx, *uint_ty1, *uint_ty2)
1606                    }
1607                    (Uint!(uint_ty, idx), RustTy::Int(int_ty)) => {
1608                        uint_int_cast(idx, *uint_ty, *int_ty)
1609                    }
1610                    (Int!(_, _), RustTy::Uint(uint_ty)) => Ty::uint(*uint_ty),
1611                    (TyKind::Discr(adt_def, _), RustTy::Int(int_ty)) => {
1612                        Self::discr_to_int_cast(adt_def, BaseTy::Int(*int_ty))
1613                    }
1614                    (TyKind::Discr(adt_def, _place), RustTy::Uint(uint_ty)) => {
1615                        Self::discr_to_int_cast(adt_def, BaseTy::Uint(*uint_ty))
1616                    }
1617                    (Char!(idx), RustTy::Uint(uint_ty)) => char_uint_cast(idx, *uint_ty),
1618                    (Uint!(_, idx), RustTy::Char) => uint_char_cast(idx),
1619                    _ => {
1620                        tracked_span_bug!("invalid int to int cast {from:?} --> {to:?}")
1621                    }
1622                }
1623            }
1624            CastKind::PointerCoercion(mir::PointerCast::Unsize) => {
1625                self.check_unsize_cast(infcx, env, stmt_span, from, to)?
1626            }
1627            CastKind::FloatToInt
1628            | CastKind::IntToFloat
1629            | CastKind::PtrToPtr
1630            | CastKind::PointerCoercion(mir::PointerCast::MutToConstPointer)
1631            | CastKind::PointerCoercion(mir::PointerCast::ClosureFnPointer)
1632            | CastKind::PointerWithExposedProvenance => self.refine_default(to)?,
1633            CastKind::PointerCoercion(mir::PointerCast::ReifyFnPointer) => {
1634                let to = self.refine_default(to)?;
1635                if let TyKind::Indexed(rty::BaseTy::FnDef(def_id, args), _) = from.kind()
1636                    && let TyKind::Indexed(BaseTy::FnPtr(super_sig), _) = to.kind()
1637                {
1638                    let current_did = infcx.def_id;
1639                    let sub_sig =
1640                        SubFn::Poly(current_did, infcx.genv.fn_sig(*def_id)?, args.clone());
1641                    // TODO:CLOSURE:2 TODO(RJ) dicey maneuver? assumes that sig_b is unrefined?
1642                    check_fn_subtyping(infcx, sub_sig, super_sig, stmt_span)?;
1643                    to
1644                } else {
1645                    tracked_span_bug!("invalid cast from `{from:?}` to `{to:?}`")
1646                }
1647            }
1648        };
1649        Ok(ty)
1650    }
1651
1652    fn discr_to_int_cast(adt_def: &AdtDef, bty: BaseTy) -> Ty {
1653        // TODO: This could be a giant disjunction, maybe better (if less precise) to use the interval?
1654        let vals = adt_def
1655            .discriminants()
1656            .map(|(_, idx)| Expr::eq(Expr::nu(), Expr::from_bits(&bty, idx)))
1657            .collect_vec();
1658        Ty::exists_with_constr(bty, Expr::or_from_iter(vals))
1659    }
1660
1661    fn check_unsize_cast(
1662        &self,
1663        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1664        env: &mut TypeEnv,
1665        span: Span,
1666        src: &Ty,
1667        dst: &ty::Ty,
1668    ) -> InferResult<Ty> {
1669        // Convert `ptr` to `&mut`
1670        let src = if let TyKind::Ptr(PtrKind::Mut(re), path) = src.kind() {
1671            env.ptr_to_ref(
1672                &mut infcx.at(span),
1673                ConstrReason::Other,
1674                *re,
1675                path,
1676                PtrToRefBound::Identity,
1677            )?
1678        } else {
1679            src.clone()
1680        };
1681
1682        if let ty::TyKind::Ref(_, deref_ty, _) = dst.kind()
1683            && let ty::TyKind::Dynamic(..) = deref_ty.kind()
1684        {
1685            return Ok(self.refine_default(dst)?);
1686        }
1687
1688        // `&mut [T; n] -> &mut [T]` or `&[T; n] -> &[T]`
1689        if let TyKind::Indexed(BaseTy::Ref(_, deref_ty, _), _) = src.kind()
1690            && let TyKind::Indexed(BaseTy::Array(arr_ty, arr_len), _) = deref_ty.kind()
1691            && let ty::TyKind::Ref(re, _, mutbl) = dst.kind()
1692        {
1693            let idx = Expr::from_const(self.genv.tcx(), arr_len);
1694            Ok(Ty::mk_ref(*re, Ty::indexed(BaseTy::Slice(arr_ty.clone()), idx), *mutbl))
1695
1696        // `Box<[T; n]> -> Box<[T]>`
1697        } else if let TyKind::Indexed(BaseTy::Adt(adt_def, args), _) = src.kind()
1698            && adt_def.is_box()
1699            && let (deref_ty, alloc_ty) = args.box_args()
1700            && let TyKind::Indexed(BaseTy::Array(arr_ty, arr_len), _) = deref_ty.kind()
1701        {
1702            let idx = Expr::from_const(self.genv.tcx(), arr_len);
1703            Ok(Ty::mk_box(
1704                self.genv,
1705                Ty::indexed(BaseTy::Slice(arr_ty.clone()), idx),
1706                alloc_ty.clone(),
1707            )?)
1708        } else {
1709            Err(query_bug!("unsupported unsize cast from `{src:?}` to `{dst:?}`"))?
1710        }
1711    }
1712
1713    fn check_operands(
1714        &mut self,
1715        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1716        env: &mut TypeEnv,
1717        span: Span,
1718        operands: &[Operand],
1719    ) -> InferResult<Vec<Ty>> {
1720        operands
1721            .iter()
1722            .map(|op| self.check_operand(infcx, env, span, op))
1723            .try_collect()
1724    }
1725
1726    fn check_operand(
1727        &mut self,
1728        infcx: &mut InferCtxt,
1729        env: &mut TypeEnv,
1730        span: Span,
1731        operand: &Operand,
1732    ) -> InferResult<Ty> {
1733        let ty = match operand {
1734            Operand::Copy(p) => env.lookup_place(&mut infcx.at(span), p)?,
1735            Operand::Move(p) => env.move_place(&mut infcx.at(span), p)?,
1736            Operand::Constant(c) => self.check_constant(c)?,
1737        };
1738        Ok(infcx.hoister(true).hoist(&ty))
1739    }
1740
1741    fn check_constant(&mut self, c: &Constant) -> QueryResult<Ty> {
1742        match c {
1743            Constant::Int(n, int_ty) => {
1744                let idx = Expr::constant(rty::Constant::from(*n));
1745                Ok(Ty::indexed(BaseTy::Int(*int_ty), idx))
1746            }
1747            Constant::Uint(n, uint_ty) => {
1748                let idx = Expr::constant(rty::Constant::from(*n));
1749                Ok(Ty::indexed(BaseTy::Uint(*uint_ty), idx))
1750            }
1751            Constant::Bool(b) => {
1752                let idx = Expr::constant(rty::Constant::from(*b));
1753                Ok(Ty::indexed(BaseTy::Bool, idx))
1754            }
1755            Constant::Float(_, float_ty) => Ok(Ty::float(*float_ty)),
1756            Constant::Unit => Ok(Ty::unit()),
1757            Constant::Str(s) => {
1758                let idx = Expr::constant(rty::Constant::from(*s));
1759                Ok(Ty::mk_ref(ReStatic, Ty::indexed(BaseTy::Str, idx), Mutability::Not))
1760            }
1761            Constant::Char(c) => {
1762                let idx = Expr::constant(rty::Constant::from(*c));
1763                Ok(Ty::indexed(BaseTy::Char, idx))
1764            }
1765            Constant::Param(param_const, ty) => {
1766                let idx = Expr::const_generic(*param_const);
1767                let ctor = self.default_refiner.refine_ty_or_base(ty)?.expect_base();
1768                Ok(ctor.replace_bound_reft(&idx).to_ty())
1769            }
1770            Constant::Opaque(ty) => self.refine_default(ty),
1771            Constant::Unevaluated(ty, uneval) => {
1772                // Use template for promoted constants, if applicable
1773                if let Some(promoted) = uneval.promoted
1774                    && let Some(ty) = self.promoted.get(promoted)
1775                {
1776                    return Ok(ty.clone());
1777                }
1778                let ty = self.refine_default(ty)?;
1779                let info = self.genv.constant_info(uneval.def)?;
1780                // else, use constant index if applicable
1781                if let Some(bty) = ty.as_bty_skipping_existentials()
1782                    && let rty::ConstantInfo::Interpreted(idx, _) = info
1783                {
1784                    return Ok(Ty::indexed(bty.clone(), idx));
1785                }
1786                // else use default unrefined type
1787                Ok(ty)
1788            }
1789        }
1790    }
1791
1792    fn check_ghost_statements_at(
1793        &mut self,
1794        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1795        env: &mut TypeEnv,
1796        point: Point,
1797        span: Span,
1798    ) -> Result {
1799        bug::track_span(span, || {
1800            for stmt in self.ghost_stmts().statements_at(point) {
1801                self.check_ghost_statement(infcx, env, stmt, span)
1802                    .with_span(span)?;
1803            }
1804            Ok(())
1805        })
1806    }
1807
1808    fn check_ghost_statement(
1809        &mut self,
1810        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
1811        env: &mut TypeEnv,
1812        stmt: &GhostStatement,
1813        span: Span,
1814    ) -> InferResult {
1815        dbg::statement!("start", stmt, infcx, env, span, &self);
1816        match stmt {
1817            GhostStatement::Fold(place) => {
1818                env.fold(&mut infcx.at(span), place)?;
1819            }
1820            GhostStatement::Unfold(place) => {
1821                env.unfold(infcx, place, span)?;
1822            }
1823            GhostStatement::Unblock(place) => env.unblock(infcx, place),
1824            GhostStatement::PtrToRef(place) => {
1825                env.ptr_to_ref_at_place(&mut infcx.at(span), place)?;
1826            }
1827        }
1828        dbg::statement!("end", stmt, infcx, env, span, &self);
1829        Ok(())
1830    }
1831
1832    #[track_caller]
1833    fn marker_at_dominator(&self, bb: BasicBlock) -> &Marker {
1834        marker_at_dominator(self.body, &self.markers, bb)
1835    }
1836
1837    fn dominators(&self) -> &'ck Dominators<BasicBlock> {
1838        self.body.dominators()
1839    }
1840
1841    fn ghost_stmts(&self) -> &'ck GhostStatements {
1842        &self.inherited.ghost_stmts[&self.checker_id]
1843    }
1844
1845    fn refine_default<T: Refine>(&self, ty: &T) -> QueryResult<T::Output> {
1846        ty.refine(&self.default_refiner)
1847    }
1848
1849    fn refine_with_holes<T: Refine>(&self, ty: &T) -> QueryResult<<T as Refine>::Output> {
1850        ty.refine(&Refiner::with_holes(self.genv, self.checker_id.root_id().to_def_id())?)
1851    }
1852}
1853
1854fn instantiate_args_for_fun_call(
1855    genv: GlobalEnv,
1856    caller_id: DefId,
1857    callee_id: DefId,
1858    args: &ty::GenericArgs,
1859) -> QueryResult<Vec<rty::GenericArg>> {
1860    let params_in_clauses = collect_params_in_clauses(genv, callee_id);
1861
1862    let hole_refiner = Refiner::new_for_item(genv, caller_id, |bty| {
1863        let sort = bty.sort();
1864        let bty = bty.shift_in_escaping(1);
1865        let constr = if !sort.is_unit() {
1866            rty::SubsetTy::new(bty, Expr::nu(), Expr::hole(rty::HoleKind::Pred))
1867        } else {
1868            rty::SubsetTy::trivial(bty, Expr::nu())
1869        };
1870        Binder::bind_with_sort(constr, sort)
1871    })?;
1872    let default_refiner = Refiner::default_for_item(genv, caller_id)?;
1873
1874    let callee_generics = genv.generics_of(callee_id)?;
1875    args.iter()
1876        .enumerate()
1877        .map(|(idx, arg)| {
1878            let param = callee_generics.param_at(idx, genv)?;
1879            let refiner =
1880                if params_in_clauses.contains(&idx) { &default_refiner } else { &hole_refiner };
1881            refiner.refine_generic_arg(&param, arg)
1882        })
1883        .collect()
1884}
1885
1886fn instantiate_args_for_constructor(
1887    genv: GlobalEnv,
1888    caller_id: DefId,
1889    adt_id: DefId,
1890    args: &ty::GenericArgs,
1891) -> QueryResult<Vec<rty::GenericArg>> {
1892    let params_in_clauses = collect_params_in_clauses(genv, adt_id);
1893
1894    let adt_generics = genv.generics_of(adt_id)?;
1895    let hole_refiner = Refiner::with_holes(genv, caller_id)?;
1896    let default_refiner = Refiner::default_for_item(genv, caller_id)?;
1897    args.iter()
1898        .enumerate()
1899        .map(|(idx, arg)| {
1900            let param = adt_generics.param_at(idx, genv)?;
1901            let refiner =
1902                if params_in_clauses.contains(&idx) { &default_refiner } else { &hole_refiner };
1903            refiner.refine_generic_arg(&param, arg)
1904        })
1905        .collect()
1906}
1907
1908fn collect_params_in_clauses(genv: GlobalEnv, def_id: DefId) -> FxHashSet<usize> {
1909    let tcx = genv.tcx();
1910    struct Collector {
1911        params: FxHashSet<usize>,
1912    }
1913
1914    impl rustc_middle::ty::TypeVisitor<TyCtxt<'_>> for Collector {
1915        fn visit_ty(&mut self, t: rustc_middle::ty::Ty) {
1916            if let rustc_middle::ty::Param(param_ty) = t.kind() {
1917                self.params.insert(param_ty.index as usize);
1918            }
1919            t.super_visit_with(self);
1920        }
1921    }
1922    let mut vis = Collector { params: Default::default() };
1923
1924    let span = genv.tcx().def_span(def_id);
1925    for (clause, _) in all_predicates_of(tcx, def_id) {
1926        if let Some(trait_pred) = clause.as_trait_clause() {
1927            let trait_id = trait_pred.def_id();
1928            let ignore = [
1929                LangItem::MetaSized,
1930                LangItem::Sized,
1931                LangItem::Tuple,
1932                LangItem::Copy,
1933                LangItem::Destruct,
1934            ];
1935            if ignore
1936                .iter()
1937                .any(|lang_item| tcx.require_lang_item(*lang_item, span) == trait_id)
1938            {
1939                continue;
1940            }
1941
1942            if tcx.fn_trait_kind_from_def_id(trait_id).is_some() {
1943                continue;
1944            }
1945            if tcx.get_diagnostic_item(sym::Hash) == Some(trait_id) {
1946                continue;
1947            }
1948            if tcx.get_diagnostic_item(sym::Eq) == Some(trait_id) {
1949                continue;
1950            }
1951        }
1952        if let Some(proj_pred) = clause.as_projection_clause() {
1953            let assoc_id = proj_pred.item_def_id();
1954            if genv.is_fn_output(assoc_id) {
1955                continue;
1956            }
1957        }
1958        if let Some(outlives_pred) = clause.as_type_outlives_clause() {
1959            // We skip outlives bounds if they are not 'static. A 'static bound means the type
1960            // implements `Any` which makes it unsound to instantiate the argument with refinements.
1961            if outlives_pred.skip_binder().1 != tcx.lifetimes.re_static {
1962                continue;
1963            }
1964        }
1965        clause.visit_with(&mut vis);
1966    }
1967    vis.params
1968}
1969
1970fn all_predicates_of(
1971    tcx: TyCtxt<'_>,
1972    id: DefId,
1973) -> impl Iterator<Item = &(rustc_middle::ty::Clause<'_>, Span)> {
1974    let mut next_id = Some(id);
1975    iter::from_fn(move || {
1976        next_id.take().map(|id| {
1977            let preds = tcx.predicates_of(id);
1978            next_id = preds.parent;
1979            preds.predicates.iter()
1980        })
1981    })
1982    .flatten()
1983}
1984
1985struct SkipConstr;
1986
1987impl TypeFolder for SkipConstr {
1988    fn fold_ty(&mut self, ty: &rty::Ty) -> rty::Ty {
1989        if let rty::TyKind::Constr(_, inner_ty) = ty.kind() {
1990            inner_ty.fold_with(self)
1991        } else {
1992            ty.super_fold_with(self)
1993        }
1994    }
1995}
1996
1997fn is_indexed_mut_skipping_constr(ty: &Ty) -> bool {
1998    let ty = SkipConstr.fold_ty(ty);
1999    if let rty::Ref!(_, inner_ty, Mutability::Mut) = ty.kind()
2000        && let TyKind::Indexed(..) = inner_ty.kind()
2001    {
2002        true
2003    } else {
2004        false
2005    }
2006}
2007
2008/// HACK(nilehmann) This let us infer parameters under mutable references for the simple case
2009/// where the formal argument is of the form `&mut B[@n]`, e.g., the type of the first argument
2010/// to `RVec::get_mut` is `&mut RVec<T>[@n]`. We should remove this after we implement opening of
2011/// mutable references.
2012fn infer_under_mut_ref_hack(rcx: &mut InferCtxt, actuals: &[Ty], fn_sig: &PolyFnSig) -> Vec<Ty> {
2013    iter::zip(actuals, fn_sig.skip_binder_ref().inputs())
2014        .map(|(actual, formal)| {
2015            if let rty::Ref!(re, deref_ty, Mutability::Mut) = actual.kind()
2016                && is_indexed_mut_skipping_constr(formal)
2017            {
2018                rty::Ty::mk_ref(*re, rcx.unpack(deref_ty), Mutability::Mut)
2019            } else {
2020                actual.clone()
2021            }
2022        })
2023        .collect()
2024}
2025
2026impl Mode for ShapeMode {
2027    const NAME: &str = "shape";
2028
2029    fn enter_basic_block<'ck, 'genv, 'tcx>(
2030        ck: &mut Checker<'ck, 'genv, 'tcx, ShapeMode>,
2031        _infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
2032        bb: BasicBlock,
2033    ) -> TypeEnv<'ck> {
2034        ck.inherited.mode.bb_envs[&ck.checker_id][&bb].enter(&ck.body.local_decls)
2035    }
2036
2037    fn check_goto_join_point<'genv, 'tcx>(
2038        ck: &mut Checker<'_, 'genv, 'tcx, ShapeMode>,
2039        _: InferCtxt<'_, 'genv, 'tcx>,
2040        env: TypeEnv,
2041        span: Span,
2042        target: BasicBlock,
2043    ) -> Result<bool> {
2044        let bb_envs = &mut ck.inherited.mode.bb_envs;
2045        let target_bb_env = bb_envs.entry(ck.checker_id).or_default().get(&target);
2046        dbg::shape_goto_enter!(target, env, target_bb_env);
2047
2048        let modified = match bb_envs.entry(ck.checker_id).or_default().entry(target) {
2049            Entry::Occupied(mut entry) => entry.get_mut().join(env, span),
2050            Entry::Vacant(entry) => {
2051                let scope = marker_at_dominator(ck.body, &ck.markers, target)
2052                    .scope()
2053                    .unwrap_or_else(|| tracked_span_bug!());
2054                entry.insert(env.into_infer(scope));
2055                true
2056            }
2057        };
2058
2059        dbg::shape_goto_exit!(target, bb_envs[&ck.checker_id].get(&target));
2060        Ok(modified)
2061    }
2062
2063    fn clear(ck: &mut Checker<ShapeMode>, root: BasicBlock) {
2064        ck.visited.remove(root);
2065        for bb in ck.body.basic_blocks.indices() {
2066            if bb != root && ck.dominators().dominates(root, bb) {
2067                ck.inherited
2068                    .mode
2069                    .bb_envs
2070                    .entry(ck.checker_id)
2071                    .or_default()
2072                    .remove(&bb);
2073                ck.visited.remove(bb);
2074            }
2075        }
2076    }
2077}
2078
2079impl Mode for RefineMode {
2080    const NAME: &str = "refine";
2081
2082    fn enter_basic_block<'ck, 'genv, 'tcx>(
2083        ck: &mut Checker<'ck, 'genv, 'tcx, RefineMode>,
2084        infcx: &mut InferCtxt<'_, 'genv, 'tcx>,
2085        bb: BasicBlock,
2086    ) -> TypeEnv<'ck> {
2087        ck.inherited.mode.bb_envs[&ck.checker_id][&bb].enter(infcx, &ck.body.local_decls)
2088    }
2089
2090    fn check_goto_join_point(
2091        ck: &mut Checker<RefineMode>,
2092        mut infcx: InferCtxt,
2093        env: TypeEnv,
2094        terminator_span: Span,
2095        target: BasicBlock,
2096    ) -> Result<bool> {
2097        let bb_env = &ck.inherited.mode.bb_envs[&ck.checker_id][&target];
2098        tracked_span_dbg_assert_eq!(
2099            &ck.marker_at_dominator(target)
2100                .scope()
2101                .unwrap_or_else(|| tracked_span_bug!()),
2102            bb_env.scope()
2103        );
2104
2105        dbg::refine_goto!(target, infcx, env, bb_env);
2106
2107        env.check_goto(&mut infcx.at(terminator_span), bb_env, target)
2108            .with_span(terminator_span)?;
2109
2110        Ok(!ck.visited.contains(target))
2111    }
2112
2113    fn clear(_ck: &mut Checker<RefineMode>, _bb: BasicBlock) {
2114        bug!();
2115    }
2116}
2117
2118fn bool_int_cast(b: &Expr, int_ty: IntTy) -> Ty {
2119    let idx = Expr::ite(b, 1, 0);
2120    Ty::indexed(BaseTy::Int(int_ty), idx)
2121}
2122
2123/// Unlike [`char_uint_cast`] rust only allows `u8` to `char` casts, which are
2124/// non-lossy, so we can use indexed type directly.
2125fn uint_char_cast(idx: &Expr) -> Ty {
2126    let idx = Expr::cast(rty::Sort::Int, rty::Sort::Char, idx.clone());
2127    Ty::indexed(BaseTy::Char, idx)
2128}
2129
2130fn char_uint_cast(idx: &Expr, uint_ty: UintTy) -> Ty {
2131    let idx = Expr::cast(rty::Sort::Char, rty::Sort::Int, idx.clone());
2132    if uint_bit_width(uint_ty) >= 32 {
2133        // non-lossy cast: uint[cast(idx)]
2134        Ty::indexed(BaseTy::Uint(uint_ty), idx)
2135    } else {
2136        // lossy-cast: uint{v: cast(idx) <= max_value => v == cast(idx) }
2137        guarded_uint_ty(&idx, uint_ty)
2138    }
2139}
2140
2141fn bool_uint_cast(b: &Expr, uint_ty: UintTy) -> Ty {
2142    let idx = Expr::ite(b, 1, 0);
2143    Ty::indexed(BaseTy::Uint(uint_ty), idx)
2144}
2145
2146fn int_int_cast(idx: &Expr, int_ty1: IntTy, int_ty2: IntTy) -> Ty {
2147    if int_bit_width(int_ty1) <= int_bit_width(int_ty2) {
2148        Ty::indexed(BaseTy::Int(int_ty2), idx.clone())
2149    } else {
2150        Ty::int(int_ty2)
2151    }
2152}
2153
2154fn uint_int_cast(idx: &Expr, uint_ty: UintTy, int_ty: IntTy) -> Ty {
2155    if uint_bit_width(uint_ty) < int_bit_width(int_ty) {
2156        Ty::indexed(BaseTy::Int(int_ty), idx.clone())
2157    } else {
2158        Ty::int(int_ty)
2159    }
2160}
2161
2162fn guarded_uint_ty(idx: &Expr, uint_ty: UintTy) -> Ty {
2163    // uint_ty2{v: idx <= max_value => v == idx }
2164    let max_value = Expr::uint_max(uint_ty);
2165    let guard = Expr::le(idx.clone(), max_value);
2166    let eq = Expr::eq(Expr::nu(), idx.clone());
2167    Ty::exists_with_constr(BaseTy::Uint(uint_ty), Expr::implies(guard, eq))
2168}
2169
2170fn uint_uint_cast(idx: &Expr, uint_ty1: UintTy, uint_ty2: UintTy) -> Ty {
2171    if uint_bit_width(uint_ty1) <= uint_bit_width(uint_ty2) {
2172        Ty::indexed(BaseTy::Uint(uint_ty2), idx.clone())
2173    } else {
2174        guarded_uint_ty(idx, uint_ty2)
2175    }
2176}
2177
2178fn uint_bit_width(uint_ty: UintTy) -> u64 {
2179    uint_ty
2180        .bit_width()
2181        .unwrap_or(config::pointer_width().bits())
2182}
2183
2184fn int_bit_width(int_ty: IntTy) -> u64 {
2185    int_ty.bit_width().unwrap_or(config::pointer_width().bits())
2186}
2187
2188impl ShapeResult {
2189    fn into_bb_envs(
2190        self,
2191        infcx: &mut InferCtxtRoot,
2192    ) -> FxHashMap<CheckerId, FxHashMap<BasicBlock, BasicBlockEnv>> {
2193        self.0
2194            .into_iter()
2195            .map(|(checker_id, shapes)| {
2196                let bb_envs = shapes
2197                    .into_iter()
2198                    .map(|(bb, shape)| (bb, shape.into_bb_env(infcx)))
2199                    .collect();
2200                (checker_id, bb_envs)
2201            })
2202            .collect()
2203    }
2204}
2205
2206fn marker_at_dominator<'a>(
2207    body: &Body,
2208    markers: &'a IndexVec<BasicBlock, Option<Marker>>,
2209    bb: BasicBlock,
2210) -> &'a Marker {
2211    let dominator = body
2212        .dominators()
2213        .immediate_dominator(bb)
2214        .unwrap_or_else(|| tracked_span_bug!());
2215    markers[dominator]
2216        .as_ref()
2217        .unwrap_or_else(|| tracked_span_bug!())
2218}
2219
2220pub(crate) mod errors {
2221    use flux_errors::{E0999, ErrorGuaranteed};
2222    use flux_infer::infer::InferErr;
2223    use flux_macros::Diagnostic;
2224    use flux_middle::{global_env::GlobalEnv, queries::ErrCtxt};
2225    use rustc_errors::Diagnostic;
2226    use rustc_hir::def_id::LocalDefId;
2227    use rustc_span::Span;
2228
2229    use crate::fluent_generated as fluent;
2230
2231    #[derive(Diagnostic)]
2232    #[diag(refineck_panic_error, code = E0999)]
2233    pub(super) struct PanicError {
2234        #[primary_span]
2235        pub(super) span: Span,
2236    }
2237
2238    #[derive(Debug)]
2239    pub struct CheckerError {
2240        kind: InferErr,
2241        span: Span,
2242    }
2243
2244    impl CheckerError {
2245        pub fn emit(self, genv: GlobalEnv, fn_def_id: LocalDefId) -> ErrorGuaranteed {
2246            let dcx = genv.sess().dcx().handle();
2247            match self.kind {
2248                InferErr::UnsolvedEvar(_) => {
2249                    let mut diag =
2250                        dcx.struct_span_err(self.span, fluent::refineck_param_inference_error);
2251                    diag.code(E0999);
2252                    diag.emit()
2253                }
2254                InferErr::Query(err) => {
2255                    let level = rustc_errors::Level::Error;
2256                    err.at(ErrCtxt::FnCheck(self.span, fn_def_id))
2257                        .into_diag(dcx, level)
2258                        .emit()
2259                }
2260            }
2261        }
2262    }
2263
2264    pub trait ResultExt<T> {
2265        fn with_span(self, span: Span) -> Result<T, CheckerError>;
2266    }
2267
2268    impl<T, E> ResultExt<T> for Result<T, E>
2269    where
2270        E: Into<InferErr>,
2271    {
2272        fn with_span(self, span: Span) -> Result<T, CheckerError> {
2273            self.map_err(|err| CheckerError { kind: err.into(), span })
2274        }
2275    }
2276}