Skip to main content

charon_lib/ast/items/
trait_impl.rs

1use crate::ast::*;
2use crate::ids::IndexVec;
3use derive_generic_visitor::{Drive, DriveMut, DriveTwo};
4use enum_as_inner::EnumAsInner;
5use serde_state::DeserializeState;
6use serde_state::SerializeState;
7
8/// A trait **implementation**.
9///
10/// For instance:
11/// ```text
12/// impl Foo for List {
13///   type Bar = ...
14///
15///   fn baz(...) { ... }
16/// }
17/// ```
18#[derive(Debug, Clone)]
19#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
20pub struct TraitImpl {
21    pub def_id: TraitImplId,
22    pub item_meta: ItemMeta,
23    pub src: TraitImplSource,
24    /// The information about the implemented trait.
25    /// Note that this contains the instantiation of the "parent"
26    /// clauses.
27    pub impl_trait: TraitDeclRef,
28    /// Whether this is a negative impl (`impl !Trait for Type`).
29    pub is_negative: bool,
30    /// Whether this is an `unsafe impl`.
31    pub is_unsafe: bool,
32    pub generics: GenericParams,
33    /// The trait references for the parent clauses (see [TraitDecl]).
34    pub implied_trait_refs: IndexVec<TraitClauseId, PolyTraitRef>,
35    /// The implemented associated constants.
36    pub consts: IndexMap<AssocConstId, GlobalDeclRef>,
37    /// The implemented associated types.
38    pub types: IndexMap<AssocTypeId, Binder<TraitAssocTyImpl>>,
39    /// The implemented methods
40    pub methods: IndexMap<TraitMethodId, Binder<FunDeclRef>>,
41    /// The virtual table instance for this trait implementation.
42    pub vtable: VTableDecl,
43}
44
45/// The value of a trait associated type.
46#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
47#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
48pub struct TraitAssocTyImpl {
49    pub value: Ty,
50    /// This matches the corresponding vector in `TraitAssocTy`. In the same way, this is empty
51    /// after the `lift_associated_item_clauses` pass.
52    pub implied_trait_refs: IndexVec<TraitClauseId, PolyTraitRef>,
53}
54
55/// Where the impl comes from.
56#[derive(Debug, Clone)]
57#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
58#[cfg_attr(feature = "charon_on_charon", charon::variants_suffix("TraitImpl"))]
59pub enum TraitImplSource {
60    /// A regular trait implementation.
61    Normal,
62    /// The blanket implementation generated for a trait alias.
63    TraitAlias,
64    /// An implementation of one of the `Fn*` traits, generated for a closure.
65    Closure {
66        #[serde_state(stateless)]
67        kind: ClosureKind,
68    },
69    /// The `Destruct` implementation generated for an ADT or closure.
70    Destruct,
71}
72
73/// The virtual table instance for a trait implementation.
74#[derive(Debug, Clone)]
75#[derive(EnumAsInner)]
76#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
77pub enum VTableDecl {
78    /// The trait is not dyn-compatible, so no vtable exists.
79    NotDynCompatible,
80    /// The trait is dyn-compatible, but we have not computed a vtable for it, as it is not used.
81    Lazy,
82    /// The trait is dyn-compatible, and we have computed a vtable for it.
83    #[cfg_attr(feature = "charon_on_charon", charon::rename("VTableInstance"))]
84    VTable(GlobalDeclRef),
85    /// We don't support computing a vtable for this impl; the string explains why.
86    #[cfg_attr(feature = "charon_on_charon", charon::rename("UnknownVTable"))]
87    Unknown(String),
88}
89
90impl TraitImpl {
91    pub fn methods(&self) -> impl Iterator<Item = &Binder<FunDeclRef>> {
92        self.methods.iter()
93    }
94
95    /// Whether this trait impl is unsafe to declare, because it is an `unsafe impl` (safety.unsafe-impl)
96    /// or it has an unsafe attribute (safety.unsafe-attribute).
97    pub fn is_unsafe_to_declare(&self, _krate: &TranslatedCrate) -> bool {
98        self.is_unsafe || self.item_meta.is_unsafe_to_declare()
99    }
100}