1pub mod visit;
17
18use std::{borrow::Cow, fmt};
19
20use flux_common::{bug, span_bug};
21use flux_config::PartialInferOpts;
22use flux_rustc_bridge::def_id_to_string;
23pub use flux_syntax::surface::{BinOp, UnOp};
24use flux_syntax::surface::{Ignored, ParamMode, Trusted};
25use itertools::Itertools;
26use rustc_abi;
27pub use rustc_abi::VariantIdx;
28use rustc_ast::TraitObjectSyntax;
29use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
30use rustc_hash::FxHashMap;
31pub use rustc_hir::PrimTy;
32use rustc_hir::{
33 FnHeader, OwnerId, ParamName, Safety,
34 def::{DefKind, Namespace},
35 def_id::{DefId, LocalDefId},
36};
37use rustc_index::newtype_index;
38use rustc_macros::{Decodable, Encodable};
39pub use rustc_middle::mir::Mutability;
40use rustc_middle::{middle::resolve_bound_vars::ResolvedArg, ty::TyCtxt};
41use rustc_span::{ErrorGuaranteed, Span, Symbol, symbol::Ident};
42
43use crate::def_id::{FluxDefId, FluxLocalDefId, MaybeExternId};
44
45pub enum Attr {
46 Trusted(Trusted),
47 TrustedImpl(Trusted),
48 Ignore(Ignored),
49 ProvenExternally,
50 ShouldFail,
51 InferOpts(PartialInferOpts),
52}
53
54#[derive(Clone, Copy, Default)]
55pub struct AttrMap<'fhir> {
56 pub attrs: &'fhir [Attr],
57 pub qualifiers: &'fhir [FluxLocalDefId],
58 pub reveals: &'fhir [FluxDefId],
59}
60
61impl AttrMap<'_> {
62 pub(crate) fn proven_externally(&self) -> bool {
63 self.attrs
64 .iter()
65 .any(|attr| matches!(attr, Attr::ProvenExternally))
66 }
67
68 pub(crate) fn ignored(&self) -> Option<Ignored> {
69 self.attrs
70 .iter()
71 .find_map(|attr| if let Attr::Ignore(ignored) = *attr { Some(ignored) } else { None })
72 }
73
74 pub(crate) fn trusted(&self) -> Option<Trusted> {
75 self.attrs
76 .iter()
77 .find_map(|attr| if let Attr::Trusted(trusted) = *attr { Some(trusted) } else { None })
78 }
79
80 pub(crate) fn trusted_impl(&self) -> Option<Trusted> {
81 self.attrs.iter().find_map(|attr| {
82 if let Attr::TrustedImpl(trusted) = *attr { Some(trusted) } else { None }
83 })
84 }
85
86 pub(crate) fn should_fail(&self) -> bool {
87 self.attrs
88 .iter()
89 .any(|attr| matches!(attr, Attr::ShouldFail))
90 }
91
92 pub(crate) fn infer_opts(&self) -> Option<PartialInferOpts> {
93 self.attrs
94 .iter()
95 .find_map(|attr| if let Attr::InferOpts(opts) = *attr { Some(opts) } else { None })
96 }
97}
98
99#[derive(Debug, Clone, Copy)]
100pub struct Generics<'fhir> {
101 pub params: &'fhir [GenericParam<'fhir>],
102 pub refinement_params: &'fhir [RefineParam<'fhir>],
103 pub predicates: Option<&'fhir [WhereBoundPredicate<'fhir>]>,
104}
105
106#[derive(Debug, Clone, Copy)]
107pub struct GenericParam<'fhir> {
108 pub def_id: MaybeExternId,
109 pub name: ParamName,
110 pub kind: GenericParamKind<'fhir>,
111}
112
113#[derive(Debug, Clone, Copy)]
114pub enum GenericParamKind<'fhir> {
115 Type { default: Option<Ty<'fhir>> },
116 Lifetime,
117 Const { ty: Ty<'fhir> },
118}
119
120#[derive(Debug)]
121pub struct Qualifier<'fhir> {
122 pub def_id: FluxLocalDefId,
123 pub args: &'fhir [RefineParam<'fhir>],
124 pub expr: Expr<'fhir>,
125 pub global: bool,
126}
127
128#[derive(Clone, Copy, Debug)]
129pub enum Node<'fhir> {
130 Item(&'fhir Item<'fhir>),
131 TraitItem(&'fhir TraitItem<'fhir>),
132 ImplItem(&'fhir ImplItem<'fhir>),
133 OpaqueTy(&'fhir OpaqueTy<'fhir>),
134 ForeignItem(&'fhir ForeignItem<'fhir>),
135 Ctor,
136 AnonConst,
137 Expr,
138}
139
140impl<'fhir> Node<'fhir> {
141 pub fn as_owner(self) -> Option<OwnerNode<'fhir>> {
142 match self {
143 Node::Item(item) => Some(OwnerNode::Item(item)),
144 Node::TraitItem(trait_item) => Some(OwnerNode::TraitItem(trait_item)),
145 Node::ImplItem(impl_item) => Some(OwnerNode::ImplItem(impl_item)),
146 Node::ForeignItem(foreign_item) => Some(OwnerNode::ForeignItem(foreign_item)),
147 Node::OpaqueTy(_) => None,
148 Node::AnonConst => None,
149 Node::Expr => None,
150 Node::Ctor => None,
151 }
152 }
153
154 pub fn expect_opaque_ty(&self) -> &'fhir OpaqueTy<'fhir> {
155 if let Node::OpaqueTy(opaque_ty) = &self { opaque_ty } else { bug!("expected opaque type") }
156 }
157}
158
159#[derive(Clone, Copy, Debug)]
160pub enum OwnerNode<'fhir> {
161 Item(&'fhir Item<'fhir>),
162 TraitItem(&'fhir TraitItem<'fhir>),
163 ImplItem(&'fhir ImplItem<'fhir>),
164 ForeignItem(&'fhir ForeignItem<'fhir>),
165}
166
167impl<'fhir> OwnerNode<'fhir> {
168 pub fn fn_sig(&self) -> Option<&'fhir FnSig<'fhir>> {
169 match self {
170 OwnerNode::Item(Item { kind: ItemKind::Fn(fn_sig, ..), .. })
171 | OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig), .. })
172 | OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig), .. })
173 | OwnerNode::ForeignItem(ForeignItem {
174 kind: ForeignItemKind::Fn(fn_sig, ..), ..
175 }) => Some(fn_sig),
176 _ => None,
177 }
178 }
179
180 pub fn generics(self) -> &'fhir Generics<'fhir> {
181 match self {
182 OwnerNode::Item(item) => &item.generics,
183 OwnerNode::TraitItem(trait_item) => &trait_item.generics,
184 OwnerNode::ImplItem(impl_item) => &impl_item.generics,
185 OwnerNode::ForeignItem(foreign_item) => {
186 match foreign_item.kind {
187 ForeignItemKind::Fn(_, generics) => generics,
188 }
189 }
190 }
191 }
192
193 pub fn owner_id(&self) -> MaybeExternId<OwnerId> {
194 match self {
195 OwnerNode::Item(item) => item.owner_id,
196 OwnerNode::TraitItem(trait_item) => trait_item.owner_id,
197 OwnerNode::ImplItem(impl_item) => impl_item.owner_id,
198 OwnerNode::ForeignItem(foreign_item) => foreign_item.owner_id,
199 }
200 }
201}
202
203#[derive(Debug)]
204pub struct Item<'fhir> {
205 pub owner_id: MaybeExternId<OwnerId>,
206 pub generics: Generics<'fhir>,
207 pub kind: ItemKind<'fhir>,
208}
209
210impl<'fhir> Item<'fhir> {
211 pub fn expect_enum(&self) -> &EnumDef<'fhir> {
212 if let ItemKind::Enum(enum_def) = &self.kind { enum_def } else { bug!("expected enum") }
213 }
214
215 pub fn expect_struct(&self) -> &StructDef<'fhir> {
216 if let ItemKind::Struct(struct_def) = &self.kind {
217 struct_def
218 } else {
219 bug!("expected struct")
220 }
221 }
222
223 pub fn expect_type_alias(&self) -> &TyAlias<'fhir> {
224 if let ItemKind::TyAlias(ty_alias) = &self.kind {
225 ty_alias
226 } else {
227 bug!("expected type alias")
228 }
229 }
230
231 pub fn expect_impl(&self) -> &Impl<'fhir> {
232 if let ItemKind::Impl(impl_) = &self.kind { impl_ } else { bug!("expected impl") }
233 }
234
235 pub fn expect_trait(&self) -> &Trait<'fhir> {
236 if let ItemKind::Trait(trait_) = &self.kind { trait_ } else { bug!("expected trait") }
237 }
238}
239
240#[derive(Debug)]
241pub enum ItemKind<'fhir> {
242 Enum(EnumDef<'fhir>),
243 Struct(StructDef<'fhir>),
244 TyAlias(&'fhir TyAlias<'fhir>),
245 Trait(Trait<'fhir>),
246 Impl(Impl<'fhir>),
247 Fn(FnSig<'fhir>),
248 Const(Option<Expr<'fhir>>),
249}
250
251#[derive(Debug)]
252pub struct TraitItem<'fhir> {
253 pub owner_id: MaybeExternId<OwnerId>,
254 pub generics: Generics<'fhir>,
255 pub kind: TraitItemKind<'fhir>,
256}
257
258#[derive(Debug)]
259pub enum TraitItemKind<'fhir> {
260 Fn(FnSig<'fhir>),
261 Const,
262 Type,
263}
264
265#[derive(Debug)]
266pub struct ImplItem<'fhir> {
267 pub owner_id: MaybeExternId<OwnerId>,
268 pub kind: ImplItemKind<'fhir>,
269 pub generics: Generics<'fhir>,
270}
271
272#[derive(Debug)]
273pub enum ImplItemKind<'fhir> {
274 Fn(FnSig<'fhir>),
275 Const,
276 Type,
277}
278
279#[derive(Copy, Clone, Debug)]
280pub enum FluxItem<'fhir> {
281 Qualifier(&'fhir Qualifier<'fhir>),
282 Func(&'fhir SpecFunc<'fhir>),
283 PrimOpProp(&'fhir PrimOpProp<'fhir>),
284 SortDecl(&'fhir SortDecl),
285}
286
287impl FluxItem<'_> {
288 pub fn def_id(self) -> FluxLocalDefId {
289 match self {
290 FluxItem::Qualifier(qualifier) => qualifier.def_id,
291 FluxItem::Func(func) => func.def_id,
292 FluxItem::PrimOpProp(prop) => prop.def_id,
293 FluxItem::SortDecl(sort_decl) => sort_decl.def_id,
294 }
295 }
296}
297
298#[derive(Debug)]
299pub struct ForeignItem<'fhir> {
300 pub ident: Ident,
301 pub kind: ForeignItemKind<'fhir>,
302 pub owner_id: MaybeExternId<OwnerId>,
303 pub span: Span,
304}
305
306#[derive(Debug)]
307pub enum ForeignItemKind<'fhir> {
308 Fn(FnSig<'fhir>, &'fhir Generics<'fhir>),
309}
310
311#[derive(Debug, Clone, Copy)]
312pub struct SortDecl {
313 pub def_id: FluxLocalDefId,
314 pub params: usize,
315 pub span: Span,
316}
317
318pub type SortDecls = FxHashMap<Symbol, SortDecl>;
319
320#[derive(Debug, Clone, Copy)]
321pub struct WhereBoundPredicate<'fhir> {
322 pub span: Span,
323 pub bounded_ty: Ty<'fhir>,
324 pub bounds: GenericBounds<'fhir>,
325}
326
327pub type GenericBounds<'fhir> = &'fhir [GenericBound<'fhir>];
328
329#[derive(Debug, Clone, Copy)]
330pub enum GenericBound<'fhir> {
331 Trait(PolyTraitRef<'fhir>),
332 Outlives(Lifetime),
333}
334
335#[derive(Debug, Clone, Copy)]
336pub struct PolyTraitRef<'fhir> {
337 pub bound_generic_params: &'fhir [GenericParam<'fhir>],
338 pub refine_params: &'fhir [RefineParam<'fhir>],
340 pub modifiers: TraitBoundModifier,
341 pub trait_ref: Path<'fhir>,
342 pub span: Span,
343}
344
345#[derive(Debug, Copy, Clone)]
346pub enum TraitBoundModifier {
347 None,
348 Maybe,
349}
350
351#[derive(Debug)]
352pub struct Trait<'fhir> {
353 pub assoc_refinements: &'fhir [TraitAssocReft<'fhir>],
354}
355
356impl<'fhir> Trait<'fhir> {
357 pub fn find_assoc_reft(&self, name: Symbol) -> Option<&'fhir TraitAssocReft<'fhir>> {
358 self.assoc_refinements
359 .iter()
360 .find(|assoc_reft| assoc_reft.name == name)
361 }
362}
363
364#[derive(Debug, Clone, Copy)]
365pub struct TraitAssocReft<'fhir> {
366 pub name: Symbol,
367 pub params: &'fhir [RefineParam<'fhir>],
368 pub output: Sort<'fhir>,
369 pub body: Option<Expr<'fhir>>,
370 pub span: Span,
371 pub final_: bool,
372}
373
374#[derive(Debug)]
375pub struct Impl<'fhir> {
376 pub assoc_refinements: &'fhir [ImplAssocReft<'fhir>],
377}
378
379impl<'fhir> Impl<'fhir> {
380 pub fn find_assoc_reft(&self, name: Symbol) -> Option<&'fhir ImplAssocReft<'fhir>> {
381 self.assoc_refinements
382 .iter()
383 .find(|assoc_reft| assoc_reft.name == name)
384 }
385}
386
387#[derive(Clone, Copy, Debug)]
388pub struct ImplAssocReft<'fhir> {
389 pub name: Symbol,
390 pub params: &'fhir [RefineParam<'fhir>],
391 pub output: Sort<'fhir>,
392 pub body: Expr<'fhir>,
393 pub span: Span,
394}
395
396#[derive(Debug)]
397pub struct OpaqueTy<'fhir> {
398 pub def_id: MaybeExternId,
399 pub bounds: GenericBounds<'fhir>,
400}
401
402pub type Arena = bumpalo::Bump;
403
404#[derive(Default)]
409pub struct FluxItems<'fhir> {
410 pub items: FxIndexMap<FluxLocalDefId, FluxItem<'fhir>>,
411}
412
413impl FluxItems<'_> {
414 pub fn new() -> Self {
415 Self { items: Default::default() }
416 }
417}
418
419#[derive(Debug)]
420pub struct TyAlias<'fhir> {
421 pub index: Option<RefineParam<'fhir>>,
422 pub ty: Ty<'fhir>,
423 pub span: Span,
424 pub lifted: bool,
426}
427
428#[derive(Debug, Clone, Copy)]
429pub struct StructDef<'fhir> {
430 pub refinement: &'fhir RefinementKind<'fhir>,
431 pub params: &'fhir [RefineParam<'fhir>],
432 pub kind: StructKind<'fhir>,
433 pub invariants: &'fhir [Expr<'fhir>],
434}
435
436#[derive(Debug, Clone, Copy)]
437pub enum StructKind<'fhir> {
438 Transparent { fields: &'fhir [FieldDef<'fhir>] },
439 Opaque,
440}
441
442#[derive(Debug, Clone, Copy)]
443pub struct FieldDef<'fhir> {
444 pub ty: Ty<'fhir>,
445 pub lifted: bool,
447}
448
449#[derive(Debug)]
450pub enum RefinementKind<'fhir> {
451 Refined(RefinedBy<'fhir>),
453 Reflected,
455}
456
457impl RefinementKind<'_> {
458 pub fn is_reflected(&self) -> bool {
459 matches!(self, RefinementKind::Reflected)
460 }
461}
462
463#[derive(Debug)]
464pub struct EnumDef<'fhir> {
465 pub refinement: &'fhir RefinementKind<'fhir>,
466 pub params: &'fhir [RefineParam<'fhir>],
467 pub variants: &'fhir [VariantDef<'fhir>],
468 pub invariants: &'fhir [Expr<'fhir>],
469}
470
471#[derive(Debug, Clone, Copy)]
472pub struct VariantDef<'fhir> {
473 pub def_id: LocalDefId,
474 pub params: &'fhir [RefineParam<'fhir>],
475 pub fields: &'fhir [FieldDef<'fhir>],
476 pub ret: VariantRet<'fhir>,
477 pub span: Span,
478 pub lifted: bool,
480}
481
482#[derive(Debug, Clone, Copy)]
483pub struct VariantRet<'fhir> {
484 pub enum_id: DefId,
485 pub idx: Expr<'fhir>,
486}
487
488#[derive(Clone, Copy)]
489pub struct FnDecl<'fhir> {
490 pub requires: &'fhir [Requires<'fhir>],
491 pub inputs: &'fhir [Ty<'fhir>],
492 pub output: FnOutput<'fhir>,
493 pub span: Span,
494 pub lifted: bool,
496}
497
498#[derive(Clone, Copy)]
500pub struct Requires<'fhir> {
501 pub params: &'fhir [RefineParam<'fhir>],
503 pub pred: Expr<'fhir>,
504}
505
506#[derive(Clone, Copy)]
507pub struct FnSig<'fhir> {
508 pub header: FnHeader,
509 pub decl: &'fhir FnDecl<'fhir>,
510}
511
512#[derive(Clone, Copy)]
513pub struct FnOutput<'fhir> {
514 pub params: &'fhir [RefineParam<'fhir>],
515 pub ret: Ty<'fhir>,
516 pub ensures: &'fhir [Ensures<'fhir>],
517}
518
519#[derive(Clone, Copy)]
520pub enum Ensures<'fhir> {
521 Type(PathExpr<'fhir>, &'fhir Ty<'fhir>),
523 Pred(Expr<'fhir>),
525}
526
527#[derive(Clone, Copy)]
528pub struct Ty<'fhir> {
529 pub kind: TyKind<'fhir>,
530 pub span: Span,
531}
532
533#[derive(Clone, Copy)]
534pub enum TyKind<'fhir> {
535 BaseTy(BaseTy<'fhir>),
544 Indexed(BaseTy<'fhir>, Expr<'fhir>),
545 Exists(&'fhir [RefineParam<'fhir>], &'fhir Ty<'fhir>),
546 Constr(Expr<'fhir>, &'fhir Ty<'fhir>),
549 StrgRef(Lifetime, &'fhir PathExpr<'fhir>, &'fhir Ty<'fhir>),
550 Ref(Lifetime, MutTy<'fhir>),
551 BareFn(&'fhir BareFnTy<'fhir>),
552 Tuple(&'fhir [Ty<'fhir>]),
553 Array(&'fhir Ty<'fhir>, ConstArg),
554 RawPtr(&'fhir Ty<'fhir>, Mutability),
555 OpaqueDef(&'fhir OpaqueTy<'fhir>),
556 TraitObject(&'fhir [PolyTraitRef<'fhir>], Lifetime, TraitObjectSyntax),
557 Never,
558 Infer,
559 Err(ErrorGuaranteed),
560}
561
562pub struct BareFnTy<'fhir> {
563 pub safety: Safety,
564 pub abi: rustc_abi::ExternAbi,
565 pub generic_params: &'fhir [GenericParam<'fhir>],
566 pub decl: &'fhir FnDecl<'fhir>,
567 pub param_idents: &'fhir [Option<Ident>],
568}
569
570#[derive(Clone, Copy)]
571pub struct MutTy<'fhir> {
572 pub ty: &'fhir Ty<'fhir>,
573 pub mutbl: Mutability,
574}
575
576#[derive(Copy, Clone, PartialEq, Eq)]
579pub enum Lifetime {
580 Hole(FhirId),
582 Resolved(ResolvedArg),
584}
585
586#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encodable, Decodable)]
588pub enum FluxOwnerId {
589 Flux(FluxLocalDefId),
590 Rust(OwnerId),
591}
592
593impl FluxOwnerId {
594 pub fn def_id(self) -> Option<LocalDefId> {
595 match self {
596 FluxOwnerId::Flux(_) => None,
597 FluxOwnerId::Rust(owner_id) => Some(owner_id.def_id),
598 }
599 }
600}
601
602#[derive(Debug, Hash, PartialEq, Eq, Copy, Clone, Encodable, Decodable)]
610pub struct FhirId {
611 pub owner: FluxOwnerId,
612 pub local_id: ItemLocalId,
613}
614
615newtype_index! {
616 #[encodable]
618 pub struct ItemLocalId {}
619}
620
621#[derive(Clone, Copy)]
623pub struct BaseTy<'fhir> {
624 pub kind: BaseTyKind<'fhir>,
625 pub fhir_id: FhirId,
626 pub span: Span,
627}
628
629impl<'fhir> BaseTy<'fhir> {
630 pub fn from_qpath(qpath: QPath<'fhir>, fhir_id: FhirId) -> Self {
631 let span = qpath.span();
632 Self { kind: BaseTyKind::Path(qpath), fhir_id, span }
633 }
634
635 fn as_path(&self) -> Option<Path<'fhir>> {
636 match self.kind {
637 BaseTyKind::Path(QPath::Resolved(None, path)) => Some(path),
638 _ => None,
639 }
640 }
641}
642
643#[derive(Clone, Copy)]
644pub enum BaseTyKind<'fhir> {
645 Path(QPath<'fhir>),
646 Slice(&'fhir Ty<'fhir>),
647 Err(ErrorGuaranteed),
648}
649
650#[derive(Clone, Copy)]
651pub enum QPath<'fhir> {
652 Resolved(Option<&'fhir Ty<'fhir>>, Path<'fhir>),
653 TypeRelative(&'fhir Ty<'fhir>, &'fhir PathSegment<'fhir>),
654}
655
656#[derive(Clone, Copy)]
657pub struct Path<'fhir> {
658 pub res: Res,
659 pub fhir_id: FhirId,
660 pub segments: &'fhir [PathSegment<'fhir>],
661 pub refine: &'fhir [Expr<'fhir>],
662 pub span: Span,
663}
664
665impl<'fhir> Path<'fhir> {
666 pub fn last_segment(&self) -> &'fhir PathSegment<'fhir> {
667 self.segments.last().unwrap()
668 }
669}
670
671#[derive(Clone, Copy)]
672pub struct PathSegment<'fhir> {
673 pub ident: Ident,
674 pub res: Res,
675 pub args: &'fhir [GenericArg<'fhir>],
676 pub constraints: &'fhir [AssocItemConstraint<'fhir>],
677}
678
679#[derive(Clone, Copy)]
680pub struct AssocItemConstraint<'fhir> {
681 pub ident: Ident,
682 pub kind: AssocItemConstraintKind<'fhir>,
683}
684
685#[derive(Clone, Copy)]
686pub enum AssocItemConstraintKind<'fhir> {
687 Equality { term: Ty<'fhir> },
688}
689
690#[derive(Clone, Copy)]
691pub enum GenericArg<'fhir> {
692 Lifetime(Lifetime),
693 Type(&'fhir Ty<'fhir>),
694 Const(ConstArg),
695 Infer,
696}
697
698impl<'fhir> GenericArg<'fhir> {
699 pub fn expect_type(&self) -> &'fhir Ty<'fhir> {
700 if let GenericArg::Type(ty) = self { ty } else { bug!("expected `GenericArg::Type`") }
701 }
702}
703
704#[derive(PartialEq, Eq, Clone, Copy)]
705pub struct ConstArg {
706 pub kind: ConstArgKind,
707 pub span: Span,
708}
709
710#[derive(PartialEq, Eq, Clone, Copy)]
711pub enum ConstArgKind {
712 Lit(usize),
713 Param(DefId),
714 Infer,
715}
716
717#[derive(Eq, PartialEq, Debug, Copy, Clone)]
722pub enum Res<Id = !> {
723 Def(DefKind, DefId),
725 PrimTy(PrimTy),
727 SelfTyAlias {
729 alias_to: DefId,
730 is_trait_impl: bool,
731 },
732 SelfTyParam {
734 trait_: DefId,
735 },
736 Param(ParamKind, Id),
738 GlobalFunc(SpecFuncKind),
740 Err,
741}
742
743#[derive(Copy, Clone, Debug)]
745pub struct PartialRes<Id = !> {
746 base_res: Res<Id>,
747 unresolved_segments: usize,
748}
749
750impl<Id: Copy> PartialRes<Id> {
751 pub fn new(base_res: Res<Id>) -> Self {
752 Self { base_res, unresolved_segments: 0 }
753 }
754
755 pub fn with_unresolved_segments(base_res: Res<Id>, unresolved_segments: usize) -> Self {
756 Self { base_res, unresolved_segments }
757 }
758
759 #[inline]
760 pub fn base_res(&self) -> Res<Id> {
761 self.base_res
762 }
763
764 pub fn unresolved_segments(&self) -> usize {
765 self.unresolved_segments
766 }
767
768 #[inline]
769 pub fn full_res(&self) -> Option<Res<Id>> {
770 (self.unresolved_segments == 0).then_some(self.base_res)
771 }
772
773 #[inline]
774 pub fn expect_full_res(&self) -> Res<Id> {
775 self.full_res().unwrap_or_else(|| bug!("expected full res"))
776 }
777
778 pub fn is_box(&self, tcx: TyCtxt) -> bool {
779 self.full_res().is_some_and(|res| res.is_box(tcx))
780 }
781
782 pub fn map_param_id<R>(&self, f: impl FnOnce(Id) -> R) -> PartialRes<R> {
783 PartialRes {
784 base_res: self.base_res.map_param_id(f),
785 unresolved_segments: self.unresolved_segments,
786 }
787 }
788}
789
790#[derive(Debug, Clone, Copy)]
791pub struct RefineParam<'fhir> {
792 pub id: ParamId,
793 pub name: Symbol,
794 pub span: Span,
795 pub sort: Sort<'fhir>,
796 pub kind: ParamKind,
797 pub fhir_id: FhirId,
798}
799
800#[derive(PartialEq, Eq, Debug, Clone, Copy)]
802pub enum ParamKind {
803 Explicit(Option<ParamMode>),
805 At,
807 Pound,
809 Colon,
811 Loc,
813 Error,
824}
825
826impl ParamKind {
827 pub fn is_loc(&self) -> bool {
828 matches!(self, ParamKind::Loc)
829 }
830}
831
832#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable)]
834pub enum InferMode {
835 EVar,
839 KVar,
843}
844
845impl InferMode {
846 pub fn from_param_kind(kind: ParamKind) -> InferMode {
847 if let ParamKind::Explicit(Some(ParamMode::Horn)) = kind {
848 InferMode::KVar
849 } else {
850 InferMode::EVar
851 }
852 }
853
854 pub fn prefix_str(self) -> &'static str {
855 match self {
856 InferMode::EVar => "?",
857 InferMode::KVar => "$",
858 }
859 }
860}
861
862#[derive(Clone, Copy)]
863pub enum PrimSort {
864 Int,
865 Bool,
866 Char,
867 Real,
868 Set,
869 Map,
870 Str,
871}
872
873impl PrimSort {
874 pub fn name_str(self) -> &'static str {
875 match self {
876 PrimSort::Int => "int",
877 PrimSort::Str => "str",
878 PrimSort::Bool => "bool",
879 PrimSort::Char => "char",
880 PrimSort::Real => "real",
881 PrimSort::Set => "Set",
882 PrimSort::Map => "Map",
883 }
884 }
885
886 pub fn generics(self) -> usize {
888 match self {
889 PrimSort::Int | PrimSort::Bool | PrimSort::Real | PrimSort::Char | PrimSort::Str => 0,
890 PrimSort::Set => 1,
891 PrimSort::Map => 2,
892 }
893 }
894}
895
896#[derive(Clone, Copy)]
897pub enum SortRes {
898 PrimSort(PrimSort),
900 User(FluxDefId),
902 SortParam(usize),
904 TyParam(DefId),
906 SelfParam {
908 trait_id: DefId,
910 },
911 SelfAlias {
913 alias_to: DefId,
915 },
916 SelfParamAssoc { trait_id: DefId, ident: Ident },
925 Adt(DefId),
927}
928
929#[derive(Clone, Copy)]
930pub enum Sort<'fhir> {
931 Path(SortPath<'fhir>),
932 Loc,
934 BitVec(u32),
936 Func(PolyFuncSort<'fhir>),
938 SortOf(BaseTy<'fhir>),
941 Infer,
943 Err(ErrorGuaranteed),
944}
945
946#[derive(Clone, Copy)]
948pub struct SortPath<'fhir> {
949 pub res: SortRes,
950 pub segments: &'fhir [Ident],
951 pub args: &'fhir [Sort<'fhir>],
952}
953
954#[derive(Clone, Copy)]
955pub struct FuncSort<'fhir> {
956 pub inputs_and_output: &'fhir [Sort<'fhir>],
958}
959
960#[derive(Clone, Copy)]
961pub struct PolyFuncSort<'fhir> {
962 pub params: usize,
963 pub fsort: FuncSort<'fhir>,
964}
965
966impl<'fhir> PolyFuncSort<'fhir> {
967 pub fn new(params: usize, inputs_and_output: &'fhir [Sort]) -> Self {
968 let fsort = FuncSort { inputs_and_output };
969 Self { params, fsort }
970 }
971}
972
973#[derive(Clone, Copy)]
975pub enum AliasReft<'fhir> {
976 Qualified { qself: &'fhir Ty<'fhir>, trait_: Path<'fhir>, name: Ident },
978 TypeRelative { qself: &'fhir Ty<'fhir>, name: Ident },
982}
983
984#[derive(Debug, Clone, Copy)]
985pub struct FieldExpr<'fhir> {
986 pub ident: Ident,
987 pub expr: Expr<'fhir>,
988 pub fhir_id: FhirId,
989 pub span: Span,
990}
991
992#[derive(Debug, Clone, Copy)]
993pub struct Spread<'fhir> {
994 pub expr: Expr<'fhir>,
995 pub span: Span,
996 pub fhir_id: FhirId,
997}
998
999#[derive(Clone, Copy)]
1000pub struct Expr<'fhir> {
1001 pub kind: ExprKind<'fhir>,
1002 pub fhir_id: FhirId,
1003 pub span: Span,
1004}
1005
1006#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable)]
1007pub enum QuantKind {
1008 Forall,
1009 Exists,
1010}
1011
1012#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
1013pub struct Range {
1014 pub start: usize,
1015 pub end: usize,
1016}
1017
1018#[derive(Clone, Copy)]
1019pub enum ExprKind<'fhir> {
1020 Var(QPathExpr<'fhir>),
1021 Dot(&'fhir Expr<'fhir>, Ident),
1022 Literal(Lit),
1023 BinaryOp(BinOp, &'fhir Expr<'fhir>, &'fhir Expr<'fhir>),
1024 UnaryOp(UnOp, &'fhir Expr<'fhir>),
1025 App(PathExpr<'fhir>, &'fhir [Expr<'fhir>]),
1026 PrimApp(BinOp, &'fhir Expr<'fhir>, &'fhir Expr<'fhir>),
1028 Alias(AliasReft<'fhir>, &'fhir [Expr<'fhir>]),
1029 IfThenElse(&'fhir Expr<'fhir>, &'fhir Expr<'fhir>, &'fhir Expr<'fhir>),
1030 Abs(&'fhir [RefineParam<'fhir>], &'fhir Expr<'fhir>),
1031 BoundedQuant(QuantKind, RefineParam<'fhir>, Range, &'fhir Expr<'fhir>),
1032 Record(&'fhir [Expr<'fhir>]),
1033 Constructor(Option<PathExpr<'fhir>>, &'fhir [FieldExpr<'fhir>], Option<&'fhir Spread<'fhir>>),
1034 Block(&'fhir [LetDecl<'fhir>], &'fhir Expr<'fhir>),
1035 Err(ErrorGuaranteed),
1036}
1037
1038#[derive(Clone, Copy)]
1039pub enum QPathExpr<'fhir> {
1040 Resolved(PathExpr<'fhir>, Option<ParamKind>),
1041 TypeRelative(&'fhir Ty<'fhir>, Ident),
1042}
1043
1044#[derive(Clone, Copy)]
1045pub struct LetDecl<'fhir> {
1046 pub param: RefineParam<'fhir>,
1047 pub init: Expr<'fhir>,
1048}
1049
1050#[derive(Clone, Copy)]
1051pub enum NumLitKind {
1052 Int,
1053 Real,
1054}
1055
1056#[derive(Clone, Copy)]
1057pub enum Lit {
1058 Int(u128, Option<NumLitKind>),
1059 Bool(bool),
1060 Str(Symbol),
1061 Char(char),
1062}
1063
1064#[derive(Clone, Copy)]
1065pub struct PathExpr<'fhir> {
1066 pub segments: &'fhir [Ident],
1067 pub res: Res<ParamId>,
1068 pub fhir_id: FhirId,
1069 pub span: Span,
1070}
1071
1072newtype_index! {
1073 #[debug_format = "a{}"]
1074 pub struct ParamId {}
1075}
1076
1077impl PolyTraitRef<'_> {
1078 pub fn trait_def_id(&self) -> DefId {
1079 let path = &self.trait_ref;
1080 if let Res::Def(DefKind::Trait, did) = path.res {
1081 did
1082 } else {
1083 span_bug!(path.span, "unexpected resolution {:?}", path.res);
1084 }
1085 }
1086}
1087
1088impl From<OwnerId> for FluxOwnerId {
1089 fn from(owner_id: OwnerId) -> Self {
1090 FluxOwnerId::Rust(owner_id)
1091 }
1092}
1093
1094impl<'fhir> Ty<'fhir> {
1095 pub fn as_path(&self) -> Option<Path<'fhir>> {
1096 match &self.kind {
1097 TyKind::BaseTy(bty) => bty.as_path(),
1098 _ => None,
1099 }
1100 }
1101}
1102
1103impl<Id> Res<Id> {
1104 pub fn descr(&self) -> &'static str {
1105 match self {
1106 Res::PrimTy(_) => "builtin type",
1107 Res::Def(kind, def_id) => kind.descr(*def_id),
1108 Res::SelfTyAlias { .. } | Res::SelfTyParam { .. } => "self type",
1109 Res::Param(..) => "refinement parameter",
1110 Res::GlobalFunc(..) => "refinement function",
1111 Res::Err => "unresolved item",
1112 }
1113 }
1114
1115 pub fn is_box(&self, tcx: TyCtxt) -> bool {
1116 if let Res::Def(DefKind::Struct, def_id) = self {
1117 tcx.adt_def(def_id).is_box()
1118 } else {
1119 false
1120 }
1121 }
1122
1123 pub fn ns(&self) -> Option<Namespace> {
1125 match self {
1126 Res::Def(kind, ..) => kind.ns(),
1127 Res::PrimTy(..) | Res::SelfTyAlias { .. } | Res::SelfTyParam { .. } => {
1128 Some(Namespace::TypeNS)
1129 }
1130 Res::Param(..) | Res::GlobalFunc(..) => Some(Namespace::ValueNS),
1131 Res::Err => None,
1132 }
1133 }
1134
1135 pub fn matches_ns(&self, ns: Namespace) -> bool {
1137 self.ns().is_none_or(|actual_ns| actual_ns == ns)
1138 }
1139
1140 pub fn map_param_id<R>(self, f: impl FnOnce(Id) -> R) -> Res<R> {
1141 match self {
1142 Res::Param(kind, param_id) => Res::Param(kind, f(param_id)),
1143 Res::Def(def_kind, def_id) => Res::Def(def_kind, def_id),
1144 Res::PrimTy(prim_ty) => Res::PrimTy(prim_ty),
1145 Res::SelfTyAlias { alias_to, is_trait_impl } => {
1146 Res::SelfTyAlias { alias_to, is_trait_impl }
1147 }
1148 Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
1149 Res::GlobalFunc(spec_func_kind) => Res::GlobalFunc(spec_func_kind),
1150 Res::Err => Res::Err,
1151 }
1152 }
1153
1154 pub fn expect_param(self) -> (ParamKind, Id) {
1155 if let Res::Param(kind, id) = self { (kind, id) } else { bug!("expected param") }
1156 }
1157}
1158
1159impl<Id1, Id2> TryFrom<rustc_hir::def::Res<Id1>> for Res<Id2> {
1160 type Error = ();
1161
1162 fn try_from(res: rustc_hir::def::Res<Id1>) -> Result<Self, Self::Error> {
1163 match res {
1164 rustc_hir::def::Res::Def(kind, did) => Ok(Res::Def(kind, did)),
1165 rustc_hir::def::Res::PrimTy(prim_ty) => Ok(Res::PrimTy(prim_ty)),
1166 rustc_hir::def::Res::SelfTyAlias { alias_to, forbid_generic: false, is_trait_impl } => {
1167 Ok(Res::SelfTyAlias { alias_to, is_trait_impl })
1168 }
1169 rustc_hir::def::Res::SelfTyParam { trait_ } => Ok(Res::SelfTyParam { trait_ }),
1170 rustc_hir::def::Res::Err => Ok(Res::Err),
1171 _ => Err(()),
1172 }
1173 }
1174}
1175
1176impl QPath<'_> {
1177 pub fn span(&self) -> Span {
1178 match self {
1179 QPath::Resolved(_, path) => path.span,
1180 QPath::TypeRelative(qself, assoc) => qself.span.to(assoc.ident.span),
1181 }
1182 }
1183}
1184
1185impl Lit {
1186 pub const TRUE: Lit = Lit::Bool(true);
1187}
1188
1189#[derive(Clone, Debug)]
1191pub struct RefinedBy<'fhir> {
1192 pub sort_params: FxIndexSet<DefId>,
1207 pub fields: FxIndexMap<Symbol, Sort<'fhir>>,
1209}
1210
1211#[derive(Debug)]
1212pub struct SpecFunc<'fhir> {
1213 pub def_id: FluxLocalDefId,
1214 pub params: usize,
1215 pub args: &'fhir [RefineParam<'fhir>],
1216 pub sort: Sort<'fhir>,
1217 pub body: Option<Expr<'fhir>>,
1218 pub hide: bool,
1219 pub ident_span: Span,
1220}
1221#[derive(Debug)]
1222pub struct PrimOpProp<'fhir> {
1223 pub def_id: FluxLocalDefId,
1224 pub op: BinOp,
1225 pub args: &'fhir [RefineParam<'fhir>],
1226 pub body: Expr<'fhir>,
1227 pub span: Span,
1228}
1229
1230#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1231pub enum SpecFuncKind {
1232 Thy(liquid_fixpoint::ThyFunc),
1234 Uif(FluxDefId),
1236 Def(FluxDefId),
1238 Cast,
1240}
1241
1242impl SpecFuncKind {
1243 pub fn def_id(&self) -> Option<FluxDefId> {
1244 match self {
1245 SpecFuncKind::Uif(flux_id) | SpecFuncKind::Def(flux_id) => Some(*flux_id),
1246 _ => None,
1247 }
1248 }
1249}
1250
1251impl<'fhir> Generics<'fhir> {
1252 pub fn get_param(&self, def_id: LocalDefId) -> &'fhir GenericParam<'fhir> {
1253 self.params
1254 .iter()
1255 .find(|p| p.def_id.local_id() == def_id)
1256 .unwrap()
1257 }
1258}
1259
1260impl<'fhir> RefinedBy<'fhir> {
1261 pub fn new(fields: FxIndexMap<Symbol, Sort<'fhir>>, sort_params: FxIndexSet<DefId>) -> Self {
1262 RefinedBy { sort_params, fields }
1263 }
1264
1265 pub fn trivial() -> Self {
1266 RefinedBy { sort_params: Default::default(), fields: Default::default() }
1267 }
1268}
1269
1270impl<'fhir> From<PolyFuncSort<'fhir>> for Sort<'fhir> {
1271 fn from(fsort: PolyFuncSort<'fhir>) -> Self {
1272 Self::Func(fsort)
1273 }
1274}
1275
1276impl FuncSort<'_> {
1277 pub fn inputs(&self) -> &[Sort<'_>] {
1278 &self.inputs_and_output[..self.inputs_and_output.len() - 1]
1279 }
1280
1281 pub fn output(&self) -> &Sort<'_> {
1282 &self.inputs_and_output[self.inputs_and_output.len() - 1]
1283 }
1284}
1285
1286impl rustc_errors::IntoDiagArg for Ty<'_> {
1287 fn into_diag_arg(self, _path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
1288 rustc_errors::DiagArgValue::Str(Cow::Owned(format!("{self:?}")))
1289 }
1290}
1291
1292impl rustc_errors::IntoDiagArg for Path<'_> {
1293 fn into_diag_arg(self, _path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
1294 rustc_errors::DiagArgValue::Str(Cow::Owned(format!("{self:?}")))
1295 }
1296}
1297
1298impl StructDef<'_> {
1299 pub fn is_opaque(&self) -> bool {
1300 matches!(self.kind, StructKind::Opaque)
1301 }
1302}
1303
1304impl fmt::Debug for FnSig<'_> {
1305 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1306 write!(f, "{:?}", self.decl)
1307 }
1308}
1309
1310impl fmt::Debug for FnDecl<'_> {
1311 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1312 if !self.requires.is_empty() {
1313 write!(f, "[{:?}] ", self.requires.iter().format(", "))?;
1314 }
1315 write!(f, "fn({:?}) -> {:?}", self.inputs.iter().format(", "), self.output)
1316 }
1317}
1318
1319impl fmt::Debug for FnOutput<'_> {
1320 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1321 if !self.params.is_empty() {
1322 write!(
1323 f,
1324 "exists<{}> ",
1325 self.params.iter().format_with(", ", |param, f| {
1326 f(&format_args!("{}: {:?}", param.name, param.sort))
1327 })
1328 )?;
1329 }
1330 write!(f, "{:?}", self.ret)?;
1331 if !self.ensures.is_empty() {
1332 write!(f, "; [{:?}]", self.ensures.iter().format(", "))?;
1333 }
1334
1335 Ok(())
1336 }
1337}
1338
1339impl fmt::Debug for Requires<'_> {
1340 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1341 if !self.params.is_empty() {
1342 write!(
1343 f,
1344 "forall {}.",
1345 self.params.iter().format_with(",", |param, f| {
1346 f(&format_args!("{}:{:?}", param.name, param.sort))
1347 })
1348 )?;
1349 }
1350 write!(f, "{:?}", self.pred)
1351 }
1352}
1353
1354impl fmt::Debug for Ensures<'_> {
1355 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1356 match self {
1357 Ensures::Type(loc, ty) => write!(f, "{loc:?}: {ty:?}"),
1358 Ensures::Pred(e) => write!(f, "{e:?}"),
1359 }
1360 }
1361}
1362
1363impl fmt::Debug for Ty<'_> {
1364 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1365 match &self.kind {
1366 TyKind::BaseTy(bty) => write!(f, "{bty:?}"),
1367 TyKind::Indexed(bty, idx) => write!(f, "{bty:?}[{idx:?}]"),
1368 TyKind::Exists(params, ty) => {
1369 write!(f, "{{")?;
1370 write!(
1371 f,
1372 "{}",
1373 params.iter().format_with(",", |param, f| {
1374 f(&format_args!("{}:{:?}", param.name, param.sort))
1375 })
1376 )?;
1377 if let TyKind::Constr(pred, ty) = &ty.kind {
1378 write!(f, ". {ty:?} | {pred:?}}}")
1379 } else {
1380 write!(f, ". {ty:?}}}")
1381 }
1382 }
1383 TyKind::StrgRef(_lft, loc, ty) => write!(f, "&strg <{loc:?}: {ty:?}>"),
1384 TyKind::Ref(_lft, mut_ty) => {
1385 write!(f, "&{}{:?}", mut_ty.mutbl.prefix_str(), mut_ty.ty)
1386 }
1387 TyKind::BareFn(bare_fn_ty) => {
1388 write!(f, "{bare_fn_ty:?}")
1389 }
1390 TyKind::Tuple(tys) => write!(f, "({:?})", tys.iter().format(", ")),
1391 TyKind::Array(ty, len) => write!(f, "[{ty:?}; {len:?}]"),
1392 TyKind::Never => write!(f, "!"),
1393 TyKind::Constr(pred, ty) => write!(f, "{{{ty:?} | {pred:?}}}"),
1394 TyKind::RawPtr(ty, Mutability::Not) => write!(f, "*const {ty:?}"),
1395 TyKind::RawPtr(ty, Mutability::Mut) => write!(f, "*mut {ty:?}"),
1396 TyKind::Infer => write!(f, "_"),
1397 TyKind::OpaqueDef(opaque_ty) => {
1398 write!(f, "impl trait <def_id = {:?}>", opaque_ty.def_id.resolved_id(),)
1399 }
1400 TyKind::TraitObject(poly_traits, _lft, _syntax) => {
1401 write!(f, "dyn {poly_traits:?}")
1402 }
1403 TyKind::Err(_) => write!(f, "err"),
1404 }
1405 }
1406}
1407
1408impl fmt::Debug for BareFnTy<'_> {
1409 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1410 if !self.generic_params.is_empty() {
1411 write!(
1412 f,
1413 "for<{}>",
1414 self.generic_params
1415 .iter()
1416 .map(|param| param.name.ident())
1417 .format(",")
1418 )?;
1419 }
1420 write!(f, "{:?}", self.decl)
1421 }
1422}
1423
1424impl fmt::Debug for Lifetime {
1425 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1426 match self {
1427 Lifetime::Hole(_) => write!(f, "'_"),
1428 Lifetime::Resolved(lft) => write!(f, "{lft:?}"),
1429 }
1430 }
1431}
1432
1433impl fmt::Debug for ConstArg {
1434 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1435 write!(f, "{:?}", self.kind)
1436 }
1437}
1438
1439impl fmt::Debug for ConstArgKind {
1440 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1441 match self {
1442 ConstArgKind::Lit(n) => write!(f, "{n}"),
1443 ConstArgKind::Param(p) => write!(f, "{p:?}"),
1444 ConstArgKind::Infer => write!(f, "_"),
1445 }
1446 }
1447}
1448
1449impl fmt::Debug for BaseTy<'_> {
1450 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1451 match &self.kind {
1452 BaseTyKind::Path(qpath) => write!(f, "{qpath:?}"),
1453 BaseTyKind::Slice(ty) => write!(f, "[{ty:?}]"),
1454 BaseTyKind::Err(_) => write!(f, "err"),
1455 }
1456 }
1457}
1458
1459impl fmt::Debug for QPath<'_> {
1460 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1461 match self {
1462 QPath::Resolved(_, path) => write!(f, "{path:?}"),
1463 QPath::TypeRelative(qself, assoc) => write!(f, "<{qself:?}>::{assoc:?}"),
1464 }
1465 }
1466}
1467
1468impl fmt::Debug for Path<'_> {
1469 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1470 write!(f, "{:?}", self.segments.iter().format("::"))?;
1471 if !self.refine.is_empty() {
1472 write!(f, "({:?})", self.refine.iter().format(", "))?;
1473 }
1474 Ok(())
1475 }
1476}
1477
1478impl fmt::Debug for PathSegment<'_> {
1479 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1480 write!(f, "{}", self.ident)?;
1481 let args: Vec<_> = self
1482 .args
1483 .iter()
1484 .map(|a| a as &dyn std::fmt::Debug)
1485 .chain(self.constraints.iter().map(|b| b as &dyn std::fmt::Debug))
1486 .collect();
1487 if !args.is_empty() {
1488 write!(f, "<{:?}>", args.iter().format(", "))?;
1489 }
1490 Ok(())
1491 }
1492}
1493
1494impl fmt::Debug for GenericArg<'_> {
1495 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1496 match self {
1497 GenericArg::Type(ty) => write!(f, "{ty:?}"),
1498 GenericArg::Lifetime(lft) => write!(f, "{lft:?}"),
1499 GenericArg::Const(cst) => write!(f, "{cst:?}"),
1500 GenericArg::Infer => write!(f, "_"),
1501 }
1502 }
1503}
1504
1505impl fmt::Debug for AssocItemConstraint<'_> {
1506 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1507 match &self.kind {
1508 AssocItemConstraintKind::Equality { term } => {
1509 write!(f, "{:?} = {:?}", self.ident, term)
1510 }
1511 }
1512 }
1513}
1514
1515impl fmt::Debug for AliasReft<'_> {
1516 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1517 match self {
1518 AliasReft::Qualified { qself, trait_, name } => {
1519 write!(f, "<{qself:?} as {trait_:?}>::{name}")
1520 }
1521 AliasReft::TypeRelative { qself, name } => {
1522 write!(f, "{qself:?}::{name}")
1523 }
1524 }
1525 }
1526}
1527
1528impl fmt::Debug for QuantKind {
1529 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1530 match self {
1531 QuantKind::Forall => write!(f, "∀"),
1532 QuantKind::Exists => write!(f, "∃"),
1533 }
1534 }
1535}
1536
1537impl fmt::Debug for Expr<'_> {
1538 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1539 match self.kind {
1540 ExprKind::Var(QPathExpr::Resolved(path, ..)) => write!(f, "{path:?}"),
1541 ExprKind::Var(QPathExpr::TypeRelative(qself, assoc)) => {
1542 write!(f, "<{qself:?}>::{assoc}")
1543 }
1544 ExprKind::BinaryOp(op, e1, e2) => write!(f, "({e1:?} {op:?} {e2:?})"),
1545 ExprKind::PrimApp(op, e1, e2) => write!(f, "[{op:?}]({e1:?}, {e2:?})"),
1546 ExprKind::UnaryOp(op, e) => write!(f, "{op:?}{e:?}"),
1547 ExprKind::Literal(lit) => write!(f, "{lit:?}"),
1548 ExprKind::App(uf, es) => write!(f, "{uf:?}({:?})", es.iter().format(", ")),
1549 ExprKind::Alias(alias, refine_args) => {
1550 write!(f, "{alias:?}({:?})", refine_args.iter().format(", "))
1551 }
1552 ExprKind::IfThenElse(p, e1, e2) => {
1553 write!(f, "(if {p:?} {{ {e1:?} }} else {{ {e2:?} }})")
1554 }
1555 ExprKind::Dot(var, fld) => write!(f, "{var:?}.{fld}"),
1556 ExprKind::Abs(params, body) => {
1557 write!(
1558 f,
1559 "|{}| {body:?}",
1560 params.iter().format_with(", ", |param, f| {
1561 f(&format_args!("{}: {:?}", param.name, param.sort))
1562 })
1563 )
1564 }
1565 ExprKind::Record(flds) => {
1566 write!(f, "{{ {:?} }}", flds.iter().format(", "))
1567 }
1568 ExprKind::Constructor(path, exprs, spread) => {
1569 if let Some(path) = path
1570 && let Some(s) = spread
1571 {
1572 write!(f, "{:?} {{ {:?}, ..{:?} }}", path, exprs.iter().format(", "), s)
1573 } else if let Some(path) = path {
1574 write!(f, "{:?} {{ {:?} }}", path, exprs.iter().format(", "))
1575 } else if let Some(s) = spread {
1576 write!(f, "{{ {:?} ..{:?} }}", exprs.iter().format(", "), s)
1577 } else {
1578 write!(f, "{{ {:?} }}", exprs.iter().format(", "))
1579 }
1580 }
1581 ExprKind::BoundedQuant(kind, refine_param, rng, expr) => {
1582 write!(f, "{kind:?} {refine_param:?} in {}.. {} {{ {expr:?} }}", rng.start, rng.end)
1583 }
1584 ExprKind::Err(_) => write!(f, "err"),
1585 ExprKind::Block(decls, body) => {
1586 for decl in decls {
1587 write!(f, "let {:?} = {:?};", decl.param, decl.init)?;
1588 }
1589 write!(f, "{body:?}")
1590 }
1591 }
1592 }
1593}
1594
1595impl fmt::Debug for PathExpr<'_> {
1596 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1597 write!(f, "{}", self.segments.iter().format("::"))
1598 }
1599}
1600
1601impl fmt::Debug for Lit {
1602 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1603 match self {
1604 Lit::Int(i, Some(NumLitKind::Real)) => write!(f, "{i}real"),
1605 Lit::Int(i, _) => write!(f, "{i}"),
1606 Lit::Bool(b) => write!(f, "{b}"),
1607 Lit::Str(s) => write!(f, "\"{s:?}\""),
1608 Lit::Char(c) => write!(f, "\'{c}\'"),
1609 }
1610 }
1611}
1612
1613impl fmt::Debug for Sort<'_> {
1614 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1615 match self {
1616 Sort::Path(path) => write!(f, "{path:?}"),
1617 Sort::BitVec(w) => write!(f, "bitvec({w})"),
1618 Sort::Loc => write!(f, "loc"),
1619 Sort::Func(fsort) => write!(f, "{fsort:?}"),
1620 Sort::SortOf(bty) => write!(f, "<{bty:?}>::sort"),
1621 Sort::Infer => write!(f, "_"),
1622 Sort::Err(_) => write!(f, "err"),
1623 }
1624 }
1625}
1626
1627impl fmt::Debug for SortPath<'_> {
1628 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1629 write!(f, "{:?}", self.res)?;
1630 if !self.args.is_empty() {
1631 write!(f, "<{:?}>", self.args.iter().format(", "))?;
1632 }
1633 Ok(())
1634 }
1635}
1636
1637impl fmt::Debug for SortRes {
1638 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1639 match self {
1640 SortRes::PrimSort(PrimSort::Bool) => write!(f, "bool"),
1641 SortRes::PrimSort(PrimSort::Int) => write!(f, "int"),
1642 SortRes::PrimSort(PrimSort::Real) => write!(f, "real"),
1643 SortRes::PrimSort(PrimSort::Char) => write!(f, "char"),
1644 SortRes::PrimSort(PrimSort::Str) => write!(f, "str"),
1645 SortRes::PrimSort(PrimSort::Set) => write!(f, "Set"),
1646 SortRes::PrimSort(PrimSort::Map) => write!(f, "Map"),
1647 SortRes::SortParam(n) => write!(f, "@{n}"),
1648 SortRes::TyParam(def_id) => write!(f, "{}::sort", def_id_to_string(*def_id)),
1649 SortRes::SelfParam { trait_id } => {
1650 write!(f, "{}::Self::sort", def_id_to_string(*trait_id))
1651 }
1652 SortRes::SelfAlias { alias_to } => {
1653 write!(f, "{}::Self::sort", def_id_to_string(*alias_to))
1654 }
1655 SortRes::SelfParamAssoc { ident: assoc, .. } => {
1656 write!(f, "Self::{assoc}")
1657 }
1658 SortRes::User(def_id) => write!(f, "{:?}", def_id.name()),
1659 SortRes::Adt(def_id) => write!(f, "{}::sort", def_id_to_string(*def_id)),
1660 }
1661 }
1662}
1663
1664impl fmt::Debug for PolyFuncSort<'_> {
1665 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1666 if self.params > 0 {
1667 write!(f, "for<{}>{:?}", self.params, self.fsort)
1668 } else {
1669 write!(f, "{:?}", self.fsort)
1670 }
1671 }
1672}
1673
1674impl fmt::Debug for FuncSort<'_> {
1675 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1676 match self.inputs() {
1677 [input] => {
1678 write!(f, "{:?} -> {:?}", input, self.output())
1679 }
1680 inputs => {
1681 write!(f, "({:?}) -> {:?}", inputs.iter().format(", "), self.output())
1682 }
1683 }
1684 }
1685}