flux_rustc_bridge/
lowering.rs

1use flux_arc_interner::List;
2use flux_common::result::ResultExt;
3use flux_errors::FluxSession;
4use itertools::Itertools;
5use rustc_borrowck::consumers::BodyWithBorrowckFacts;
6use rustc_errors::ErrorGuaranteed;
7use rustc_hir::def_id::{DefId, LocalDefId};
8use rustc_infer::{
9    infer::{InferCtxt, TyCtxtInferExt},
10    traits::Obligation,
11};
12use rustc_macros::{Decodable, Encodable};
13use rustc_middle::{
14    mir::{self as rustc_mir},
15    traits::{ImplSource, ObligationCause},
16    ty::{
17        self as rustc_ty, GenericArgKind, ParamConst, ParamEnv, TyCtxt, TypingMode,
18        adjustment as rustc_adjustment,
19    },
20};
21use rustc_span::Span;
22use rustc_trait_selection::traits::SelectionContext;
23
24use super::{
25    mir::{
26        AggregateKind, AssertKind, BasicBlockData, BinOp, Body, CallArgs, CastKind, LocalDecl,
27        NonDivergingIntrinsic, Operand, Place, PlaceElem, PointerCast, Rvalue, Statement,
28        StatementKind, Terminator, TerminatorKind,
29    },
30    ty::{
31        AdtDef, AdtDefData, AliasKind, Binder, BoundRegion, BoundVariableKind, Clause, ClauseKind,
32        Const, ConstKind, ExistentialPredicate, ExistentialProjection, FieldDef, FnSig, GenericArg,
33        GenericParamDef, GenericParamDefKind, GenericPredicates, Generics, OutlivesPredicate,
34        TraitPredicate, TraitRef, Ty, TypeOutlivesPredicate, UnevaluatedConst, VariantDef,
35    },
36};
37use crate::{
38    mir::{BodyRoot, CallKind, ConstOperand},
39    ty::{
40        AliasTy, ExistentialTraitRef, GenericArgs, ProjectionPredicate, Region,
41        RegionOutlivesPredicate,
42    },
43};
44
45pub trait Lower<'tcx> {
46    type R;
47
48    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R;
49}
50
51pub struct MirLoweringCtxt<'a, 'sess, 'tcx> {
52    tcx: TyCtxt<'tcx>,
53    param_env: ParamEnv<'tcx>,
54    selcx: SelectionContext<'a, 'tcx>,
55    sess: &'sess FluxSession,
56    rustc_mir: &'a rustc_mir::Body<'tcx>,
57}
58
59#[derive(Debug, Clone)]
60pub struct UnsupportedReason {
61    pub(crate) descr: String,
62}
63
64impl UnsupportedReason {
65    fn new(reason: impl ToString) -> Self {
66        UnsupportedReason { descr: reason.to_string() }
67    }
68
69    pub fn into_err(self) -> UnsupportedErr {
70        UnsupportedErr { descr: self.descr, span: None }
71    }
72}
73
74#[derive(Debug, Clone, Encodable, Decodable)]
75pub struct UnsupportedErr {
76    pub descr: String,
77    pub span: Option<Span>,
78}
79
80impl UnsupportedErr {
81    pub fn new(reason: UnsupportedReason) -> Self {
82        UnsupportedErr { descr: reason.descr, span: None }
83    }
84
85    pub fn with_span(mut self, span: Span) -> Self {
86        self.span = Some(span);
87        self
88    }
89}
90
91fn trait_ref_impl_id<'tcx>(
92    tcx: TyCtxt<'tcx>,
93    selcx: &mut SelectionContext<'_, 'tcx>,
94    param_env: ParamEnv<'tcx>,
95    trait_ref: rustc_ty::TraitRef<'tcx>,
96) -> Option<(DefId, rustc_middle::ty::GenericArgsRef<'tcx>)> {
97    let trait_ref = tcx.erase_and_anonymize_regions(trait_ref);
98    let obligation = Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
99    let impl_source = selcx.select(&obligation).ok()??;
100    let impl_source = selcx.infcx.resolve_vars_if_possible(impl_source);
101    // let impl_source = selcx.infcx.fully_resolve(impl_source).ok()?;
102    let ImplSource::UserDefined(impl_data) = impl_source else { return None };
103    Some((impl_data.impl_def_id, impl_data.args))
104}
105
106pub fn resolve_trait_ref_impl_id<'tcx>(
107    tcx: TyCtxt<'tcx>,
108    def_id: DefId,
109    trait_ref: rustc_ty::TraitRef<'tcx>,
110) -> Option<(DefId, rustc_middle::ty::GenericArgsRef<'tcx>)> {
111    let param_env = tcx.param_env(def_id);
112    let infcx = tcx
113        .infer_ctxt()
114        .with_next_trait_solver(true)
115        .build(TypingMode::non_body_analysis());
116    trait_ref_impl_id(tcx, &mut SelectionContext::new(&infcx), param_env, trait_ref)
117}
118
119fn resolve_call_query<'tcx>(
120    tcx: TyCtxt<'tcx>,
121    selcx: &mut SelectionContext<'_, 'tcx>,
122    param_env: ParamEnv<'tcx>,
123    callee_id: DefId,
124    args: rustc_middle::ty::GenericArgsRef<'tcx>,
125) -> Option<(DefId, rustc_middle::ty::GenericArgsRef<'tcx>)> {
126    let trait_id = tcx.trait_of_assoc(callee_id)?;
127    let trait_ref = rustc_ty::TraitRef::from_assoc(tcx, trait_id, args);
128    let (impl_def_id, impl_args) = trait_ref_impl_id(tcx, selcx, param_env, trait_ref)?;
129    let impl_args = args.rebase_onto(tcx, trait_id, impl_args);
130    let assoc_id = tcx.impl_item_implementor_ids(impl_def_id).get(&callee_id)?;
131    let assoc_item = tcx.associated_item(assoc_id);
132    Some((assoc_item.def_id, impl_args))
133}
134
135impl<'sess, 'tcx> MirLoweringCtxt<'_, 'sess, 'tcx> {
136    // We used to call `replicate_infer_ctxt` to compute the `infcx`
137    // which replicated the [`InferCtxt`] used for mir typeck
138    // by generating region variables for every region the body.
139    // HOWEVER,the rustc folks hid access to the region-variables
140    // so instead we have to take care to call `tcx.erase_regions(..)`
141    // before using the `infcx` or `SelectionContext` to do any queries
142    // (eg. see `get_impl_id_of_alias_reft` in projections.rs)
143    pub fn lower_mir_body(
144        tcx: TyCtxt<'tcx>,
145        sess: &'sess FluxSession,
146        def_id: LocalDefId,
147        body_with_facts: BodyWithBorrowckFacts<'tcx>,
148    ) -> Result<BodyRoot<'tcx>, ErrorGuaranteed> {
149        let infcx = tcx
150            .infer_ctxt()
151            .with_next_trait_solver(true)
152            .build(TypingMode::analysis_in_body(tcx, def_id));
153        let rustc_body = body_with_facts.body;
154        let def_id = rustc_body.source.def_id();
155
156        let body = Self::lower_rustc_body(tcx, &infcx, sess, def_id, rustc_body)?;
157        let promoted = body_with_facts
158            .promoted
159            .into_iter()
160            .map(|rustc_promoted| Self::lower_rustc_body(tcx, &infcx, sess, def_id, rustc_promoted))
161            .try_collect()?;
162
163        let body_root = BodyRoot::new(
164            body_with_facts.borrow_set,
165            body_with_facts.region_inference_context,
166            infcx,
167            body,
168            promoted,
169        );
170        Ok(body_root)
171    }
172
173    fn lower_rustc_body(
174        tcx: TyCtxt<'tcx>,
175        infcx: &InferCtxt<'tcx>,
176        sess: &'sess FluxSession,
177        def_id: DefId,
178        body: rustc_mir::Body<'tcx>,
179    ) -> Result<Body<'tcx>, ErrorGuaranteed> {
180        let selcx = SelectionContext::new(infcx);
181        let param_env = tcx.param_env(def_id);
182        let mut lower = MirLoweringCtxt { tcx, selcx, param_env, sess, rustc_mir: &body };
183
184        let basic_blocks = body
185            .basic_blocks
186            .iter()
187            .map(|bb_data| lower.lower_basic_block_data(bb_data))
188            .try_collect()?;
189
190        let local_decls = body
191            .local_decls
192            .iter()
193            .map(|local_decl| lower.lower_local_decl(local_decl))
194            .try_collect()?;
195
196        Ok(Body::new(basic_blocks, local_decls, body))
197    }
198
199    fn lower_basic_block_data(
200        &mut self,
201        data: &rustc_mir::BasicBlockData<'tcx>,
202    ) -> Result<BasicBlockData<'tcx>, ErrorGuaranteed> {
203        let data = BasicBlockData {
204            statements: data
205                .statements
206                .iter()
207                .map(|stmt| self.lower_statement(stmt))
208                .try_collect()?,
209            terminator: data
210                .terminator
211                .as_ref()
212                .map(|terminator| self.lower_terminator(terminator))
213                .transpose()?,
214            is_cleanup: data.is_cleanup,
215        };
216        Ok(data)
217    }
218
219    fn lower_local_decl(
220        &self,
221        local_decl: &rustc_mir::LocalDecl<'tcx>,
222    ) -> Result<LocalDecl, ErrorGuaranteed> {
223        Ok(LocalDecl {
224            ty: local_decl
225                .ty
226                .lower(self.tcx)
227                .map_err(|err| errors::UnsupportedLocalDecl::new(local_decl, err))
228                .emit(self.sess)?,
229            source_info: local_decl.source_info,
230        })
231    }
232
233    fn lower_statement(
234        &self,
235        stmt: &rustc_mir::Statement<'tcx>,
236    ) -> Result<Statement<'tcx>, ErrorGuaranteed> {
237        let span = stmt.source_info.span;
238        let kind = match &stmt.kind {
239            rustc_mir::StatementKind::Assign(box (place, rvalue)) => {
240                StatementKind::Assign(
241                    lower_place(self.tcx, place)
242                        .map_err(|reason| errors::UnsupportedMir::statement(span, reason))
243                        .emit(self.sess)?,
244                    self.lower_rvalue(rvalue)
245                        .map_err(|reason| errors::UnsupportedMir::statement(span, reason))
246                        .emit(self.sess)?,
247                )
248            }
249            rustc_mir::StatementKind::SetDiscriminant { place, variant_index } => {
250                StatementKind::SetDiscriminant(
251                    lower_place(self.tcx, place)
252                        .map_err(|reason| errors::UnsupportedMir::statement(span, reason))
253                        .emit(self.sess)?,
254                    *variant_index,
255                )
256            }
257            rustc_mir::StatementKind::FakeRead(box (cause, place)) => {
258                StatementKind::FakeRead(Box::new((
259                    *cause,
260                    lower_place(self.tcx, place)
261                        .map_err(|reason| errors::UnsupportedMir::statement(span, reason))
262                        .emit(self.sess)?,
263                )))
264            }
265            rustc_mir::StatementKind::PlaceMention(place) => {
266                StatementKind::PlaceMention(
267                    lower_place(self.tcx, place)
268                        .map_err(|reason| errors::UnsupportedMir::statement(span, reason))
269                        .emit(self.sess)?,
270                )
271            }
272            rustc_mir::StatementKind::Nop
273            | rustc_mir::StatementKind::StorageLive(_)
274            | rustc_mir::StatementKind::StorageDead(_) => StatementKind::Nop,
275            rustc_mir::StatementKind::AscribeUserType(
276                box (place, rustc_mir::UserTypeProjection { projs, .. }),
277                variance,
278            ) if projs.is_empty() => {
279                StatementKind::AscribeUserType(
280                    lower_place(self.tcx, place)
281                        .map_err(|reason| errors::UnsupportedMir::statement(span, reason))
282                        .emit(self.sess)?,
283                    *variance,
284                )
285            }
286            rustc_mir::StatementKind::Intrinsic(ndi) => {
287                match ndi.as_ref() {
288                    rustc_mir::NonDivergingIntrinsic::Assume(op) => {
289                        let op = self
290                            .lower_operand(op)
291                            .map_err(|reason| errors::UnsupportedMir::statement(span, reason))
292                            .emit(self.sess)?;
293                        StatementKind::Intrinsic(NonDivergingIntrinsic::Assume(op))
294                    }
295                    rustc_mir::NonDivergingIntrinsic::CopyNonOverlapping(_) => {
296                        return Err(errors::UnsupportedMir::from(stmt)).emit(self.sess);
297                    }
298                }
299            }
300
301            rustc_mir::StatementKind::Retag(_, _)
302            | rustc_mir::StatementKind::AscribeUserType(..)
303            | rustc_mir::StatementKind::Coverage(_)
304            | rustc_mir::StatementKind::ConstEvalCounter
305            | rustc_mir::StatementKind::BackwardIncompatibleDropHint { .. } => {
306                return Err(errors::UnsupportedMir::from(stmt)).emit(self.sess);
307            }
308        };
309        Ok(Statement { kind, source_info: stmt.source_info })
310    }
311
312    fn lower_terminator(
313        &mut self,
314        terminator: &rustc_mir::Terminator<'tcx>,
315    ) -> Result<Terminator<'tcx>, ErrorGuaranteed> {
316        let span = terminator.source_info.span;
317        let kind = match &terminator.kind {
318            rustc_mir::TerminatorKind::Return => TerminatorKind::Return,
319            rustc_mir::TerminatorKind::Call { func, args, destination, target, unwind, .. } => {
320                let kind = {
321                    let func_ty = func.ty(self.rustc_mir, self.tcx);
322                    match func_ty.kind() {
323                        rustc_middle::ty::TyKind::FnDef(fn_def, args) => {
324                            let lowered = args
325                                .lower(self.tcx)
326                                .map_err(|reason| errors::UnsupportedMir::terminator(span, reason))
327                                .emit(self.sess)?;
328                            let def_id = *fn_def;
329                            let generic_args = CallArgs { orig: args, lowered };
330                            let (resolved_id, resolved_args) = self
331                                .resolve_call(def_id, generic_args.orig)
332                                .map_err(|reason| {
333                                    errors::UnsupportedMir::new(span, "terminator call", reason)
334                                })
335                                .emit(self.sess)?;
336                            CallKind::FnDef { def_id, generic_args, resolved_id, resolved_args }
337                        }
338                        rustc_middle::ty::TyKind::FnPtr(fn_sig_tys, header) => {
339                            let fn_sig = fnptr_as_fnsig(fn_sig_tys, header)
340                                .lower(self.tcx)
341                                .map_err(|reason| errors::UnsupportedMir::terminator(span, reason))
342                                .emit(self.sess)?;
343                            let operand = self
344                                .lower_operand(func)
345                                .map_err(|reason| {
346                                    errors::UnsupportedMir::new(
347                                        span,
348                                        "function pointer target",
349                                        reason,
350                                    )
351                                })
352                                .emit(self.sess)?;
353                            CallKind::FnPtr { fn_sig, operand }
354                        }
355                        _ => {
356                            Err(errors::UnsupportedMir::terminator(
357                                span,
358                                UnsupportedReason::new(format!(
359                                    "unsupported callee type `{func_ty:?}`"
360                                )),
361                            ))
362                            .emit(self.sess)?
363                        }
364                    }
365                };
366
367                let destination = lower_place(self.tcx, destination)
368                    .map_err(|reason| {
369                        errors::UnsupportedMir::new(span, "terminator destination", reason)
370                    })
371                    .emit(self.sess)?;
372
373                TerminatorKind::Call {
374                    kind,
375                    destination,
376                    target: *target,
377                    args: args
378                        .iter()
379                        .map(|arg| {
380                            self.lower_operand(&arg.node).map_err(|reason| {
381                                errors::UnsupportedMir::new(span, "terminator args", reason)
382                            })
383                        })
384                        .try_collect()
385                        .emit(self.sess)?,
386                    unwind: *unwind,
387                }
388            }
389            rustc_mir::TerminatorKind::SwitchInt { discr, targets, .. } => {
390                TerminatorKind::SwitchInt {
391                    discr: self
392                        .lower_operand(discr)
393                        .map_err(|reason| errors::UnsupportedMir::terminator(span, reason))
394                        .emit(self.sess)?,
395                    targets: targets.clone(),
396                }
397            }
398            rustc_mir::TerminatorKind::Goto { target } => TerminatorKind::Goto { target: *target },
399            rustc_mir::TerminatorKind::Drop { place, target, unwind, .. } => {
400                TerminatorKind::Drop {
401                    place: lower_place(self.tcx, place)
402                        .map_err(|reason| errors::UnsupportedMir::terminator(span, reason))
403                        .emit(self.sess)?,
404                    target: *target,
405                    unwind: *unwind,
406                }
407            }
408            rustc_mir::TerminatorKind::Assert { cond, target, expected, msg, .. } => {
409                TerminatorKind::Assert {
410                    cond: self
411                        .lower_operand(cond)
412                        .map_err(|reason| errors::UnsupportedMir::terminator(span, reason))
413                        .emit(self.sess)?,
414                    expected: *expected,
415                    target: *target,
416                    msg: self
417                        .lower_assert_msg(msg)
418                        .ok_or_else(|| errors::UnsupportedMir::from(terminator))
419                        .emit(self.sess)?,
420                }
421            }
422            rustc_mir::TerminatorKind::Unreachable => TerminatorKind::Unreachable,
423            rustc_mir::TerminatorKind::FalseEdge { real_target, imaginary_target } => {
424                TerminatorKind::FalseEdge {
425                    real_target: *real_target,
426                    imaginary_target: *imaginary_target,
427                }
428            }
429            rustc_mir::TerminatorKind::FalseUnwind { real_target, unwind } => {
430                TerminatorKind::FalseUnwind { real_target: *real_target, unwind: *unwind }
431            }
432            rustc_mir::TerminatorKind::Yield { value, resume, resume_arg, drop } => {
433                TerminatorKind::Yield {
434                    value: self
435                        .lower_operand(value)
436                        .map_err(|reason| errors::UnsupportedMir::terminator(span, reason))
437                        .emit(self.sess)?,
438                    resume: *resume,
439                    resume_arg: lower_place(self.tcx, resume_arg)
440                        .map_err(|reason| errors::UnsupportedMir::terminator(span, reason))
441                        .emit(self.sess)?,
442                    drop: *drop,
443                }
444            }
445            rustc_mir::TerminatorKind::CoroutineDrop => TerminatorKind::CoroutineDrop,
446            rustc_mir::TerminatorKind::UnwindResume => TerminatorKind::UnwindResume,
447            rustc_mir::TerminatorKind::UnwindTerminate(..)
448            | rustc_mir::TerminatorKind::TailCall { .. }
449            | rustc_mir::TerminatorKind::InlineAsm { .. } => {
450                return Err(errors::UnsupportedMir::from(terminator)).emit(self.sess);
451            }
452        };
453        Ok(Terminator { kind, source_info: terminator.source_info })
454    }
455
456    fn resolve_call(
457        &mut self,
458        callee_id: DefId,
459        args: rustc_middle::ty::GenericArgsRef<'tcx>,
460    ) -> Result<(DefId, CallArgs<'tcx>), UnsupportedReason> {
461        let (resolved_id, resolved_args) =
462            resolve_call_query(self.tcx, &mut self.selcx, self.param_env, callee_id, args)
463                .unwrap_or((callee_id, args));
464        let call_args = CallArgs { lowered: resolved_args.lower(self.tcx)?, orig: resolved_args };
465        Ok((resolved_id, call_args))
466    }
467
468    fn lower_rvalue(
469        &self,
470        rvalue: &rustc_mir::Rvalue<'tcx>,
471    ) -> Result<Rvalue<'tcx>, UnsupportedReason> {
472        match rvalue {
473            rustc_mir::Rvalue::Use(op) => Ok(Rvalue::Use(self.lower_operand(op)?)),
474            rustc_mir::Rvalue::Repeat(op, c) => {
475                let op = self.lower_operand(op)?;
476                let c = c.lower(self.tcx)?;
477                Ok(Rvalue::Repeat(op, c))
478            }
479            rustc_mir::Rvalue::Ref(region, bk, p) => {
480                Ok(Rvalue::Ref(region.lower(self.tcx)?, *bk, lower_place(self.tcx, p)?))
481            }
482            rustc_mir::Rvalue::RawPtr(kind, place) => {
483                Ok(Rvalue::RawPtr(*kind, lower_place(self.tcx, place)?))
484            }
485            rustc_mir::Rvalue::Cast(kind, op, ty) => {
486                let kind = self.lower_cast_kind(*kind).ok_or_else(|| {
487                    UnsupportedReason::new(format!("unsupported cast `{kind:?}`"))
488                })?;
489                let op = self.lower_operand(op)?;
490                let ty = ty.lower(self.tcx)?;
491                Ok(Rvalue::Cast(kind, op, ty))
492            }
493            rustc_mir::Rvalue::BinaryOp(bin_op, box (op1, op2)) => {
494                Ok(Rvalue::BinaryOp(
495                    self.lower_bin_op(*bin_op)?,
496                    self.lower_operand(op1)?,
497                    self.lower_operand(op2)?,
498                ))
499            }
500            rustc_mir::Rvalue::UnaryOp(un_op, op) => {
501                Ok(Rvalue::UnaryOp(*un_op, self.lower_operand(op)?))
502            }
503            rustc_mir::Rvalue::Discriminant(p) => {
504                Ok(Rvalue::Discriminant(lower_place(self.tcx, p)?))
505            }
506            rustc_mir::Rvalue::Aggregate(aggregate_kind, args) => {
507                let aggregate_kind = self.lower_aggregate_kind(aggregate_kind)?;
508                let args = args.iter().map(|op| self.lower_operand(op)).try_collect()?;
509                Ok(Rvalue::Aggregate(aggregate_kind, args))
510            }
511            rustc_mir::Rvalue::ShallowInitBox(op, ty) => {
512                Ok(Rvalue::ShallowInitBox(self.lower_operand(op)?, ty.lower(self.tcx)?))
513            }
514            rustc_mir::Rvalue::ThreadLocalRef(_)
515            | rustc_mir::Rvalue::NullaryOp(..)
516            | rustc_mir::Rvalue::CopyForDeref(_)
517            | rustc_mir::Rvalue::WrapUnsafeBinder(..) => {
518                Err(UnsupportedReason::new(format!("unsupported rvalue `{rvalue:?}`")))
519            }
520        }
521    }
522
523    fn lower_pointer_coercion(
524        &self,
525        coercion: rustc_adjustment::PointerCoercion,
526    ) -> Option<PointerCast> {
527        match coercion {
528            rustc_adjustment::PointerCoercion::MutToConstPointer => {
529                Some(crate::mir::PointerCast::MutToConstPointer)
530            }
531            rustc_adjustment::PointerCoercion::Unsize => Some(crate::mir::PointerCast::Unsize),
532            rustc_adjustment::PointerCoercion::ClosureFnPointer(_) => {
533                Some(crate::mir::PointerCast::ClosureFnPointer)
534            }
535            rustc_adjustment::PointerCoercion::ReifyFnPointer => {
536                Some(crate::mir::PointerCast::ReifyFnPointer)
537            }
538            rustc_adjustment::PointerCoercion::UnsafeFnPointer
539            | rustc_adjustment::PointerCoercion::ArrayToPointer => None,
540        }
541    }
542    fn lower_cast_kind(&self, kind: rustc_mir::CastKind) -> Option<CastKind> {
543        match kind {
544            rustc_mir::CastKind::IntToInt => Some(CastKind::IntToInt),
545            rustc_mir::CastKind::IntToFloat => Some(CastKind::IntToFloat),
546            rustc_mir::CastKind::FloatToInt => Some(CastKind::FloatToInt),
547            rustc_mir::CastKind::PtrToPtr => Some(CastKind::PtrToPtr),
548            rustc_mir::CastKind::PointerCoercion(ptr_coercion, _) => {
549                Some(CastKind::PointerCoercion(self.lower_pointer_coercion(ptr_coercion)?))
550            }
551            rustc_mir::CastKind::PointerExposeProvenance => Some(CastKind::PointerExposeProvenance),
552            rustc_mir::CastKind::PointerWithExposedProvenance => {
553                Some(CastKind::PointerWithExposedProvenance)
554            }
555            _ => None,
556        }
557    }
558
559    fn lower_aggregate_kind(
560        &self,
561        aggregate_kind: &rustc_mir::AggregateKind<'tcx>,
562    ) -> Result<AggregateKind, UnsupportedReason> {
563        match aggregate_kind {
564            rustc_mir::AggregateKind::Adt(
565                def_id,
566                variant_idx,
567                args,
568                user_type_annot_idx,
569                field_idx,
570            ) => {
571                Ok(AggregateKind::Adt(
572                    *def_id,
573                    *variant_idx,
574                    args.lower(self.tcx)?,
575                    *user_type_annot_idx,
576                    *field_idx,
577                ))
578            }
579            rustc_mir::AggregateKind::Array(ty) => Ok(AggregateKind::Array(ty.lower(self.tcx)?)),
580            rustc_mir::AggregateKind::Tuple => Ok(AggregateKind::Tuple),
581            rustc_mir::AggregateKind::Closure(did, args) => {
582                let args = args.lower(self.tcx)?;
583                Ok(AggregateKind::Closure(*did, args))
584            }
585            rustc_mir::AggregateKind::Coroutine(did, args) => {
586                let args = args.lower(self.tcx)?;
587                Ok(AggregateKind::Coroutine(*did, args))
588            }
589            rustc_mir::AggregateKind::RawPtr(_, _)
590            | rustc_mir::AggregateKind::CoroutineClosure(..) => {
591                Err(UnsupportedReason::new(format!(
592                    "unsupported aggregate kind `{aggregate_kind:?}`"
593                )))
594            }
595        }
596    }
597
598    fn lower_bin_op(&self, bin_op: rustc_mir::BinOp) -> Result<BinOp, UnsupportedReason> {
599        match bin_op {
600            rustc_mir::BinOp::Add => Ok(BinOp::Add),
601            rustc_mir::BinOp::Sub => Ok(BinOp::Sub),
602            rustc_mir::BinOp::Gt => Ok(BinOp::Gt),
603            rustc_mir::BinOp::Ge => Ok(BinOp::Ge),
604            rustc_mir::BinOp::Lt => Ok(BinOp::Lt),
605            rustc_mir::BinOp::Le => Ok(BinOp::Le),
606            rustc_mir::BinOp::Eq => Ok(BinOp::Eq),
607            rustc_mir::BinOp::Ne => Ok(BinOp::Ne),
608            rustc_mir::BinOp::Mul => Ok(BinOp::Mul),
609            rustc_mir::BinOp::Div => Ok(BinOp::Div),
610            rustc_mir::BinOp::Rem => Ok(BinOp::Rem),
611            rustc_mir::BinOp::BitAnd => Ok(BinOp::BitAnd),
612            rustc_mir::BinOp::BitOr => Ok(BinOp::BitOr),
613            rustc_mir::BinOp::BitXor => Ok(BinOp::BitXor),
614            rustc_mir::BinOp::Shl => Ok(BinOp::Shl),
615            rustc_mir::BinOp::Shr => Ok(BinOp::Shr),
616            rustc_mir::BinOp::AddUnchecked
617            | rustc_mir::BinOp::SubUnchecked
618            | rustc_mir::BinOp::MulUnchecked
619            | rustc_mir::BinOp::ShlUnchecked
620            | rustc_mir::BinOp::ShrUnchecked
621            | rustc_mir::BinOp::AddWithOverflow
622            | rustc_mir::BinOp::SubWithOverflow
623            | rustc_mir::BinOp::MulWithOverflow
624            | rustc_mir::BinOp::Cmp
625            | rustc_mir::BinOp::Offset => {
626                Err(UnsupportedReason::new(format!("unsupported binary op `{bin_op:?}`")))
627            }
628        }
629    }
630
631    fn lower_operand(
632        &self,
633        op: &rustc_mir::Operand<'tcx>,
634    ) -> Result<Operand<'tcx>, UnsupportedReason> {
635        match op {
636            rustc_mir::Operand::Copy(place) => Ok(Operand::Copy(lower_place(self.tcx, place)?)),
637            rustc_mir::Operand::Move(place) => Ok(Operand::Move(lower_place(self.tcx, place)?)),
638            rustc_mir::Operand::Constant(c) => Ok(Operand::Constant(self.lower_constant(c)?)),
639        }
640    }
641
642    fn lower_constant(
643        &self,
644        constant: &rustc_mir::ConstOperand<'tcx>,
645    ) -> Result<ConstOperand<'tcx>, UnsupportedReason> {
646        Ok(ConstOperand {
647            span: constant.span,
648            ty: constant.const_.ty().lower(self.tcx)?,
649            const_: constant.const_,
650        })
651    }
652
653    fn lower_assert_msg(&self, msg: &rustc_mir::AssertMessage) -> Option<AssertKind> {
654        use rustc_mir::AssertKind::*;
655        match msg {
656            BoundsCheck { .. } => Some(AssertKind::BoundsCheck),
657            DivisionByZero(_) => Some(AssertKind::DivisionByZero),
658            RemainderByZero(_) => Some(AssertKind::RemainderByZero),
659            Overflow(bin_op, ..) => Some(AssertKind::Overflow(self.lower_bin_op(*bin_op).ok()?)),
660            _ => None,
661        }
662    }
663}
664
665pub fn lower_place<'tcx>(
666    _tcx: TyCtxt<'tcx>,
667    place: &rustc_mir::Place<'tcx>,
668) -> Result<Place, UnsupportedReason> {
669    let mut projection = vec![];
670    for elem in place.projection {
671        match elem {
672            rustc_mir::PlaceElem::Deref => projection.push(PlaceElem::Deref),
673            rustc_mir::PlaceElem::Field(field, _) => projection.push(PlaceElem::Field(field)),
674            rustc_mir::PlaceElem::Downcast(name, idx) => {
675                projection.push(PlaceElem::Downcast(name, idx));
676            }
677            rustc_mir::PlaceElem::Index(v) => projection.push(PlaceElem::Index(v)),
678            rustc_mir::ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
679                projection.push(PlaceElem::ConstantIndex { offset, min_length, from_end });
680            }
681            _ => {
682                return Err(UnsupportedReason::new(format!("unsupported place `{place:?}`")));
683            }
684        }
685    }
686    Ok(Place { local: place.local, projection })
687}
688
689impl<'tcx> Lower<'tcx> for rustc_ty::FnSig<'tcx> {
690    type R = Result<FnSig, UnsupportedReason>;
691
692    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
693        let inputs_and_output = List::from_vec(
694            self.inputs_and_output
695                .iter()
696                .map(|ty| ty.lower(tcx))
697                .try_collect()?,
698        );
699        Ok(FnSig { safety: self.safety, abi: self.abi, inputs_and_output })
700    }
701}
702
703impl<'tcx> Lower<'tcx> for &'tcx rustc_ty::List<rustc_ty::BoundVariableKind> {
704    type R = Result<List<BoundVariableKind>, UnsupportedReason>;
705
706    fn lower(self, _tcx: TyCtxt<'tcx>) -> Self::R {
707        let mut vars = vec![];
708        for var in self {
709            match var {
710                rustc_ty::BoundVariableKind::Region(kind) => {
711                    vars.push(BoundVariableKind::Region(kind));
712                }
713                _ => {
714                    return Err(UnsupportedReason {
715                        descr: format!("unsupported bound variable {var:?}"),
716                    });
717                }
718            }
719        }
720        Ok(List::from_vec(vars))
721    }
722}
723
724impl<'tcx> Lower<'tcx> for rustc_ty::ValTree<'tcx> {
725    type R = crate::ty::ValTree;
726
727    fn lower(self, _tcx: TyCtxt<'tcx>) -> Self::R {
728        match &*self {
729            rustc_ty::ValTreeKind::Leaf(scalar_int) => crate::ty::ValTree::Leaf(*scalar_int),
730            rustc_ty::ValTreeKind::Branch(trees) => {
731                let trees = trees.iter().map(|tree| tree.lower(_tcx)).collect();
732                crate::ty::ValTree::Branch(trees)
733            }
734        }
735    }
736}
737
738impl<'tcx> Lower<'tcx> for rustc_ty::Const<'tcx> {
739    type R = Result<Const, UnsupportedReason>;
740
741    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
742        let kind = match self.kind() {
743            rustc_type_ir::ConstKind::Param(param_const) => {
744                ConstKind::Param(ParamConst { name: param_const.name, index: param_const.index })
745            }
746            rustc_type_ir::ConstKind::Value(value) => {
747                ConstKind::Value(value.ty.lower(tcx)?, value.valtree.lower(tcx))
748            }
749            rustc_type_ir::ConstKind::Unevaluated(c) => {
750                // TODO: raise unsupported if c.args is not empty?
751                let args = c.args.lower(tcx)?;
752                ConstKind::Unevaluated(UnevaluatedConst { def: c.def, args, promoted: None })
753            }
754            _ => return Err(UnsupportedReason::new(format!("unsupported const {self:?}"))),
755        };
756        Ok(Const { kind })
757    }
758}
759
760impl<'tcx, T, S> Lower<'tcx> for rustc_ty::Binder<'tcx, T>
761where
762    T: Lower<'tcx, R = Result<S, UnsupportedReason>>,
763{
764    type R = Result<Binder<S>, UnsupportedReason>;
765
766    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
767        let vars = self.bound_vars().lower(tcx)?;
768        Ok(Binder::bind_with_vars(self.skip_binder().lower(tcx)?, vars))
769    }
770}
771
772impl<'tcx> Lower<'tcx> for rustc_ty::Ty<'tcx> {
773    type R = Result<Ty, UnsupportedReason>;
774
775    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
776        match self.kind() {
777            rustc_ty::Ref(region, ty, mutability) => {
778                Ok(Ty::mk_ref(region.lower(tcx)?, ty.lower(tcx)?, *mutability))
779            }
780            rustc_ty::Bool => Ok(Ty::mk_bool()),
781            rustc_ty::Int(int_ty) => Ok(Ty::mk_int(*int_ty)),
782            rustc_ty::Uint(uint_ty) => Ok(Ty::mk_uint(*uint_ty)),
783            rustc_ty::Float(float_ty) => Ok(Ty::mk_float(*float_ty)),
784            rustc_ty::Param(param_ty) => Ok(Ty::mk_param(*param_ty)),
785            rustc_ty::Adt(adt_def, args) => {
786                let args = args.lower(tcx)?;
787                Ok(Ty::mk_adt(adt_def.lower(tcx), args))
788            }
789            rustc_ty::FnDef(def_id, args) => {
790                let args = args.lower(tcx)?;
791                Ok(Ty::mk_fn_def(*def_id, args))
792            }
793            rustc_ty::Never => Ok(Ty::mk_never()),
794            rustc_ty::Str => Ok(Ty::mk_str()),
795            rustc_ty::Char => Ok(Ty::mk_char()),
796            rustc_ty::Tuple(tys) => {
797                let tys = List::from_vec(tys.iter().map(|ty| ty.lower(tcx)).try_collect()?);
798                Ok(Ty::mk_tuple(tys))
799            }
800            rustc_ty::Array(ty, len) => Ok(Ty::mk_array(ty.lower(tcx)?, len.lower(tcx)?)),
801            rustc_ty::Slice(ty) => Ok(Ty::mk_slice(ty.lower(tcx)?)),
802            rustc_ty::RawPtr(ty, mutbl) => {
803                let ty = ty.lower(tcx)?;
804                Ok(Ty::mk_raw_ptr(ty, *mutbl))
805            }
806            rustc_ty::FnPtr(fn_sig_tys, header) => {
807                let fn_sig = fnptr_as_fnsig(fn_sig_tys, header).lower(tcx)?;
808                Ok(Ty::mk_fn_ptr(fn_sig))
809            }
810            rustc_ty::Closure(did, args) => {
811                let args = args.lower(tcx)?;
812                Ok(Ty::mk_closure(*did, args))
813            }
814
815            rustc_ty::Alias(kind, alias_ty) => {
816                let kind = kind.lower(tcx)?;
817                let args = alias_ty.args.lower(tcx)?;
818                Ok(Ty::mk_alias(kind, alias_ty.def_id, args))
819            }
820            rustc_ty::Coroutine(did, args) => {
821                let args = args.lower(tcx)?;
822                Ok(Ty::mk_coroutine(*did, args))
823            }
824            rustc_ty::CoroutineWitness(did, args) => {
825                let args = args.lower(tcx)?;
826                Ok(Ty::mk_generator_witness(*did, args))
827            }
828            rustc_ty::Dynamic(predicates, region) => {
829                let region = region.lower(tcx)?;
830
831                let exi_preds = List::from_vec(
832                    predicates
833                        .iter()
834                        .map(|pred| pred.lower(tcx))
835                        .try_collect()?,
836                );
837
838                Ok(Ty::mk_dynamic(exi_preds, region))
839            }
840            rustc_ty::Foreign(def_id) => Ok(Ty::mk_foreign(*def_id)),
841            rustc_ty::Pat(..) => Ok(Ty::mk_pat()),
842            _ => Err(UnsupportedReason::new(format!("unsupported type `{self:?}`"))),
843        }
844    }
845}
846
847fn fnptr_as_fnsig<'tcx>(
848    fn_sig_tys: &'tcx rustc_ty::Binder<'tcx, rustc_ty::FnSigTys<TyCtxt<'tcx>>>,
849    header: &'tcx rustc_ty::FnHeader<TyCtxt<'tcx>>,
850) -> rustc_ty::Binder<'tcx, rustc_ty::FnSig<'tcx>> {
851    fn_sig_tys.map_bound(|fn_sig_tys| {
852        rustc_ty::FnSig {
853            inputs_and_output: fn_sig_tys.inputs_and_output,
854            c_variadic: header.c_variadic,
855            safety: header.safety,
856            abi: header.abi,
857        }
858    })
859}
860
861impl<'tcx> Lower<'tcx> for rustc_ty::AliasTyKind {
862    type R = Result<AliasKind, UnsupportedReason>;
863
864    fn lower(self, _tcx: TyCtxt<'tcx>) -> Self::R {
865        match self {
866            rustc_type_ir::AliasTyKind::Projection => Ok(AliasKind::Projection),
867            rustc_type_ir::AliasTyKind::Opaque => Ok(AliasKind::Opaque),
868            _ => Err(UnsupportedReason::new(format!("unsupported alias kind `{self:?}`"))),
869        }
870    }
871}
872
873impl<'tcx> Lower<'tcx> for rustc_ty::AdtDef<'tcx> {
874    type R = AdtDef;
875
876    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
877        AdtDef::new(AdtDefData::new(
878            tcx,
879            self,
880            self.variants()
881                .iter()
882                .map(|variant| {
883                    VariantDef {
884                        def_id: variant.def_id,
885                        name: variant.name,
886                        fields: variant
887                            .fields
888                            .iter()
889                            .map(|f| FieldDef { did: f.did, name: f.name })
890                            .collect(),
891                    }
892                })
893                .collect(),
894        ))
895    }
896}
897
898impl<'tcx> Lower<'tcx> for rustc_ty::ExistentialPredicate<'tcx> {
899    type R = Result<ExistentialPredicate, UnsupportedReason>;
900
901    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
902        match self {
903            rustc_type_ir::ExistentialPredicate::Trait(trait_ref) => {
904                Ok(ExistentialPredicate::Trait(ExistentialTraitRef {
905                    def_id: trait_ref.def_id,
906                    args: trait_ref.args.lower(tcx)?,
907                }))
908            }
909            rustc_type_ir::ExistentialPredicate::Projection(proj) => {
910                let Some(term) = proj.term.as_type() else {
911                    return Err(UnsupportedReason::new(format!(
912                        "unsupported existential predicate `{self:?}`"
913                    )));
914                };
915                Ok(ExistentialPredicate::Projection(ExistentialProjection {
916                    def_id: proj.def_id,
917                    args: proj.args.lower(tcx)?,
918                    term: term.lower(tcx)?,
919                }))
920            }
921            rustc_type_ir::ExistentialPredicate::AutoTrait(def_id) => {
922                Ok(ExistentialPredicate::AutoTrait(def_id))
923            }
924        }
925    }
926}
927
928impl<'tcx> Lower<'tcx> for rustc_middle::ty::GenericArgsRef<'tcx> {
929    type R = Result<GenericArgs, UnsupportedReason>;
930
931    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
932        Ok(List::from_vec(self.iter().map(|arg| arg.lower(tcx)).try_collect()?))
933    }
934}
935
936impl<'tcx> Lower<'tcx> for rustc_middle::ty::GenericArg<'tcx> {
937    type R = Result<GenericArg, UnsupportedReason>;
938
939    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
940        match self.kind() {
941            GenericArgKind::Type(ty) => Ok(GenericArg::Ty(ty.lower(tcx)?)),
942            GenericArgKind::Lifetime(region) => Ok(GenericArg::Lifetime(region.lower(tcx)?)),
943            GenericArgKind::Const(c) => Ok(GenericArg::Const(c.lower(tcx)?)),
944        }
945    }
946}
947
948impl<'tcx> Lower<'tcx> for rustc_middle::ty::Region<'tcx> {
949    type R = Result<Region, UnsupportedReason>;
950
951    fn lower(self, _tcx: TyCtxt<'tcx>) -> Self::R {
952        use rustc_middle::ty;
953        match self.kind() {
954            ty::ReVar(rvid) => Ok(Region::ReVar(rvid)),
955            ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), bregion) => {
956                Ok(Region::ReBound(
957                    debruijn,
958                    Ok(BoundRegion { kind: bregion.kind, var: bregion.var })?,
959                ))
960            }
961            ty::ReEarlyParam(bregion) => Ok(Region::ReEarlyParam(bregion)),
962            ty::ReStatic => Ok(Region::ReStatic),
963            ty::ReErased => Ok(Region::ReErased),
964            ty::ReBound(ty::BoundVarIndexKind::Canonical, _)
965            | ty::ReLateParam(_)
966            | ty::RePlaceholder(_)
967            | ty::ReError(_) => {
968                Err(UnsupportedReason::new(format!("unsupported region `{self:?}`")))
969            }
970        }
971    }
972}
973
974impl<'tcx> Lower<'tcx> for &'tcx rustc_middle::ty::Generics {
975    type R = Generics<'tcx>;
976
977    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
978        let params = List::from_vec(
979            self.own_params
980                .iter()
981                .map(|param| param.lower(tcx))
982                .collect(),
983        );
984        Generics { params, orig: self }
985    }
986}
987
988impl<'tcx> Lower<'tcx> for &rustc_middle::ty::GenericParamDef {
989    type R = GenericParamDef;
990
991    fn lower(self, _tcx: TyCtxt<'tcx>) -> Self::R {
992        let kind = match self.kind {
993            rustc_ty::GenericParamDefKind::Type { has_default, .. } => {
994                GenericParamDefKind::Type { has_default }
995            }
996            rustc_ty::GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
997            rustc_ty::GenericParamDefKind::Const { has_default, .. } => {
998                GenericParamDefKind::Const { has_default }
999            }
1000        };
1001        GenericParamDef { def_id: self.def_id, index: self.index, name: self.name, kind }
1002    }
1003}
1004
1005impl<'tcx> Lower<'tcx> for rustc_ty::GenericPredicates<'tcx> {
1006    type R = Result<GenericPredicates, UnsupportedErr>;
1007
1008    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
1009        let predicates = self
1010            .predicates
1011            .iter()
1012            .map(|(clause, span)| {
1013                clause
1014                    .lower(tcx)
1015                    .map_err(|reason| UnsupportedErr::new(reason).with_span(*span))
1016            })
1017            .try_collect()?;
1018        Ok(GenericPredicates { parent: self.parent, predicates })
1019    }
1020}
1021
1022impl<'tcx> Lower<'tcx> for rustc_ty::Clauses<'tcx> {
1023    type R = Result<List<Clause>, UnsupportedErr>;
1024
1025    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
1026        self.iter()
1027            .map(|clause| clause.lower(tcx).map_err(UnsupportedErr::new))
1028            .try_collect()
1029    }
1030}
1031
1032impl<'tcx> Lower<'tcx> for rustc_ty::ClauseKind<'tcx> {
1033    type R = Result<ClauseKind, UnsupportedReason>;
1034
1035    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
1036        let kind = match self {
1037            rustc_ty::ClauseKind::Trait(trait_pred) => {
1038                ClauseKind::Trait(TraitPredicate { trait_ref: trait_pred.trait_ref.lower(tcx)? })
1039            }
1040            rustc_ty::ClauseKind::Projection(proj_pred) => {
1041                let Some(term) = proj_pred.term.as_type() else {
1042                    return Err(UnsupportedReason::new(format!(
1043                        "unsupported projection predicate `{proj_pred:?}`"
1044                    )));
1045                };
1046                let proj_ty = proj_pred.projection_term;
1047                let args = proj_ty.args.lower(tcx)?;
1048
1049                let projection_ty = AliasTy { args, def_id: proj_ty.def_id };
1050                let term = term.lower(tcx)?;
1051                ClauseKind::Projection(ProjectionPredicate { projection_ty, term })
1052            }
1053            rustc_ty::ClauseKind::RegionOutlives(outlives) => {
1054                ClauseKind::RegionOutlives(outlives.lower(tcx)?)
1055            }
1056            rustc_ty::ClauseKind::TypeOutlives(outlives) => {
1057                ClauseKind::TypeOutlives(outlives.lower(tcx)?)
1058            }
1059            rustc_ty::ClauseKind::ConstArgHasType(const_, ty) => {
1060                ClauseKind::ConstArgHasType(const_.lower(tcx)?, ty.lower(tcx)?)
1061            }
1062            _ => {
1063                return Err(UnsupportedReason::new(format!("unsupported clause kind `{self:?}`")));
1064            }
1065        };
1066        Ok(kind)
1067    }
1068}
1069
1070impl<'tcx> Lower<'tcx> for rustc_ty::Clause<'tcx> {
1071    type R = Result<Clause, UnsupportedReason>;
1072
1073    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
1074        Ok(Clause::new(self.kind().lower(tcx)?))
1075    }
1076}
1077
1078impl<'tcx> Lower<'tcx> for rustc_ty::TraitRef<'tcx> {
1079    type R = Result<TraitRef, UnsupportedReason>;
1080
1081    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
1082        Ok(TraitRef { def_id: self.def_id, args: self.args.lower(tcx)? })
1083    }
1084}
1085
1086impl<'tcx> Lower<'tcx> for rustc_ty::TypeOutlivesPredicate<'tcx> {
1087    type R = Result<TypeOutlivesPredicate, UnsupportedReason>;
1088
1089    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
1090        Ok(OutlivesPredicate(self.0.lower(tcx)?, self.1.lower(tcx)?))
1091    }
1092}
1093
1094impl<'tcx> Lower<'tcx> for rustc_ty::RegionOutlivesPredicate<'tcx> {
1095    type R = Result<RegionOutlivesPredicate, UnsupportedReason>;
1096
1097    fn lower(self, tcx: TyCtxt<'tcx>) -> Self::R {
1098        Ok(OutlivesPredicate(self.0.lower(tcx)?, self.1.lower(tcx)?))
1099    }
1100}
1101
1102mod errors {
1103    use std::path::PathBuf;
1104
1105    use flux_errors::E0999;
1106    use flux_macros::Diagnostic;
1107    use rustc_middle::mir as rustc_mir;
1108    use rustc_span::Span;
1109
1110    use super::UnsupportedReason;
1111
1112    #[derive(Diagnostic)]
1113    #[diag(rustc_bridge_unsupported_local_decl, code = E0999)]
1114    pub(super) struct UnsupportedLocalDecl<'tcx> {
1115        #[primary_span]
1116        #[label]
1117        span: Span,
1118        ty: rustc_middle::ty::Ty<'tcx>,
1119    }
1120
1121    impl<'tcx> UnsupportedLocalDecl<'tcx> {
1122        pub(super) fn new(
1123            local_decl: &rustc_mir::LocalDecl<'tcx>,
1124            _err: UnsupportedReason,
1125        ) -> Self {
1126            Self { span: local_decl.source_info.span, ty: local_decl.ty }
1127        }
1128    }
1129
1130    #[derive(Diagnostic)]
1131    #[diag(rustc_bridge_unsupported_mir, code = E0999)]
1132    #[note]
1133    pub(super) struct UnsupportedMir {
1134        #[primary_span]
1135        span: Span,
1136        kind: &'static str,
1137        reason: UnsupportedReason,
1138    }
1139
1140    impl rustc_errors::IntoDiagArg for UnsupportedReason {
1141        fn into_diag_arg(self, _path: &mut Option<PathBuf>) -> rustc_errors::DiagArgValue {
1142            rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(self.descr))
1143        }
1144    }
1145
1146    impl UnsupportedMir {
1147        pub(super) fn new(span: Span, kind: &'static str, reason: UnsupportedReason) -> Self {
1148            Self { span, kind, reason }
1149        }
1150
1151        pub(super) fn terminator(span: Span, reason: UnsupportedReason) -> Self {
1152            Self { span, kind: "terminator", reason }
1153        }
1154
1155        pub(super) fn statement(span: Span, reason: UnsupportedReason) -> Self {
1156            Self { span, kind: "statement", reason }
1157        }
1158    }
1159
1160    impl<'a, 'tcx> From<&'a rustc_mir::Terminator<'tcx>> for UnsupportedMir {
1161        fn from(terminator: &'a rustc_mir::Terminator<'tcx>) -> Self {
1162            Self::terminator(
1163                terminator.source_info.span,
1164                UnsupportedReason::new(format!("{terminator:?}",)),
1165            )
1166        }
1167    }
1168
1169    impl<'a, 'tcx> From<&'a rustc_mir::Statement<'tcx>> for UnsupportedMir {
1170        fn from(statement: &'a rustc_mir::Statement<'tcx>) -> Self {
1171            Self::statement(
1172                statement.source_info.span,
1173                UnsupportedReason::new(format!("{statement:?}")),
1174            )
1175        }
1176    }
1177}