Struct chalk_solve::rust_ir::TraitDatum
source · pub struct TraitDatum<I: Interner> {
pub id: TraitId<I>,
pub binders: Binders<TraitDatumBound<I>>,
pub flags: TraitFlags,
pub associated_ty_ids: Vec<AssocTypeId<I>>,
pub well_known: Option<WellKnownTrait>,
}
Expand description
A rust intermediate representation (rust_ir) of a Trait Definition. For example, given the following rust code:
use std::fmt::Debug;
trait Foo<T>
where
T: Debug,
{
type Bar<U>;
}
This would represent the trait Foo
declaration. Note that the details of
the trait members (e.g., the associated type declaration (type Bar<U>
) are
not contained in this type, and are represented separately (e.g., in
AssociatedTyDatum
).
Not to be confused with the rust_ir for a Trait Implementation, which is
represented by ImplDatum
Fields§
§id: TraitId<I>
§binders: Binders<TraitDatumBound<I>>
§flags: TraitFlags
“Flags” indicate special kinds of traits, like auto traits.
In Rust syntax these are represented in different ways, but in
chalk we add annotations like #[auto]
.
associated_ty_ids: Vec<AssocTypeId<I>>
§well_known: Option<WellKnownTrait>
If this is a well-known trait, which one? If None
, this is a regular,
user-defined trait.
Implementations§
source§impl<I: Interner> TraitDatum<I>
impl<I: Interner> TraitDatum<I>
pub fn is_auto_trait(&self) -> bool
pub fn is_non_enumerable_trait(&self) -> bool
pub fn is_coinductive_trait(&self) -> bool
sourcepub fn where_clauses(&self) -> Binders<&Vec<QuantifiedWhereClause<I>>>
pub fn where_clauses(&self) -> Binders<&Vec<QuantifiedWhereClause<I>>>
Gives access to the where clauses of the trait, quantified over the type parameters of the trait:
trait Foo<T> where T: Debug { }
^^^^^^^^^^^^^^
Trait Implementations§
source§impl<I: Clone + Interner> Clone for TraitDatum<I>
impl<I: Clone + Interner> Clone for TraitDatum<I>
source§fn clone(&self) -> TraitDatum<I>
fn clone(&self) -> TraitDatum<I>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<I: PartialEq + Interner> PartialEq for TraitDatum<I>
impl<I: PartialEq + Interner> PartialEq for TraitDatum<I>
source§fn eq(&self, other: &TraitDatum<I>) -> bool
fn eq(&self, other: &TraitDatum<I>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<I: Interner> RenderAsRust<I> for TraitDatum<I>
impl<I: Interner> RenderAsRust<I> for TraitDatum<I>
fn fmt(&self, s: &InternalWriterState<'_, I>, f: &mut Formatter<'_>) -> Result
fn display<'a>(
&'a self,
s: &'a InternalWriterState<'a, I>,
) -> DisplayRenderAsRust<'a, I, Self>where
Self: Sized,
source§impl<I: Interner> ToProgramClauses<I> for TraitDatum<I>
impl<I: Interner> ToProgramClauses<I> for TraitDatum<I>
source§fn to_program_clauses(
&self,
builder: &mut ClauseBuilder<'_, I>,
environment: &Environment<I>,
)
fn to_program_clauses( &self, builder: &mut ClauseBuilder<'_, I>, environment: &Environment<I>, )
Given the following trait declaration: trait Ord<T> where Self: Eq<T> { ... }
, generate:
-- Rule WellFormed-TraitRef
forall<Self, T> {
WF(Self: Ord<T>) :- Implemented(Self: Ord<T>), WF(Self: Eq<T>).
}
and the reverse rules:
-- Rule Implemented-From-Env
forall<Self, T> {
(Self: Ord<T>) :- FromEnv(Self: Ord<T>).
}
-- Rule Implied-Bound-From-Trait
forall<Self, T> {
FromEnv(Self: Eq<T>) :- FromEnv(Self: Ord<T>).
}
As specified in the orphan rules, if a trait is not marked #[upstream]
, the current crate
can implement it for any type. To represent that, we generate:
// `Ord<T>` would not be `#[upstream]` when compiling `std`
forall<Self, T> { LocalImplAllowed(Self: Ord<T>). }
For traits that are #[upstream]
(i.e. not in the current crate), the orphan rules dictate
that impls are allowed as long as at least one type parameter is local and each type
prior to that is fully visible. That means that each type prior to the first local
type cannot contain any of the type parameters of the impl.
This rule is fairly complex, so we expand it and generate a program clause for each possible case. This is represented as follows:
// for `#[upstream] trait Foo<T, U, V> where Self: Eq<T> { ... }`
forall<Self, T, U, V> {
LocalImplAllowed(Self: Foo<T, U, V>) :- IsLocal(Self).
}
forall<Self, T, U, V> {
LocalImplAllowed(Self: Foo<T, U, V>) :-
IsFullyVisible(Self),
IsLocal(T).
}
forall<Self, T, U, V> {
LocalImplAllowed(Self: Foo<T, U, V>) :-
IsFullyVisible(Self),
IsFullyVisible(T),
IsLocal(U).
}
forall<Self, T, U, V> {
LocalImplAllowed(Self: Foo<T, U, V>) :-
IsFullyVisible(Self),
IsFullyVisible(T),
IsFullyVisible(U),
IsLocal(V).
}
The overlap check uses compatible { … } mode to ensure that it accounts for impls that may exist in some other compatible world. For every upstream trait, we add a rule to account for the fact that upstream crates are able to compatibly add impls of upstream traits for upstream types.
// For `#[upstream] trait Foo<T, U, V> where Self: Eq<T> { ... }`
forall<Self, T, U, V> {
Implemented(Self: Foo<T, U, V>) :-
Implemented(Self: Eq<T>), // where clauses
Compatible, // compatible modality
IsUpstream(Self),
IsUpstream(T),
IsUpstream(U),
IsUpstream(V),
CannotProve. // returns ambiguous
}
In certain situations, this is too restrictive. Consider the following code:
/* In crate std */
trait Sized { }
struct str { }
/* In crate bar (depends on std) */
trait Bar { }
impl Bar for str { }
impl<T> Bar for T where T: Sized { }
Here, because of the rules we’ve defined, these two impls overlap. The std crate is upstream to bar, and thus it is allowed to compatibly implement Sized for str. If str can implement Sized in a compatible future, these two impls definitely overlap since the second impl covers all types that implement Sized.
The solution we’ve got right now is to mark Sized as “fundamental” when it is defined. This signals to the Rust compiler that it can rely on the fact that str does not implement Sized in all contexts. A consequence of this is that we can no longer add an implementation of Sized compatibly for str. This is the trade off you make when defining a fundamental trait.
To implement fundamental traits, we simply just do not add the rule above that allows upstream types to implement upstream traits. Fundamental traits are not allowed to compatibly do that.
source§impl<I: Interner> TypeVisitable<I> for TraitDatum<I>
impl<I: Interner> TypeVisitable<I> for TraitDatum<I>
source§fn visit_with<B>(
&self,
visitor: &mut dyn TypeVisitor<I, BreakTy = B>,
outer_binder: DebruijnIndex,
) -> ControlFlow<B>
fn visit_with<B>( &self, visitor: &mut dyn TypeVisitor<I, BreakTy = B>, outer_binder: DebruijnIndex, ) -> ControlFlow<B>
visitor
to self
; binders
is the
number of binders that are in scope when beginning the
visitor. Typically binders
starts as 0, but is adjusted when
we encounter Binders<T>
in the IR or other similar
constructs.impl<I: Eq + Interner> Eq for TraitDatum<I>
impl<I: Interner> StructuralPartialEq for TraitDatum<I>
Auto Trait Implementations§
impl<I> Freeze for TraitDatum<I>
impl<I> RefUnwindSafe for TraitDatum<I>where
<I as Interner>::DefId: RefUnwindSafe,
<I as Interner>::InternedVariableKinds: RefUnwindSafe,
<I as Interner>::InternedSubstitution: RefUnwindSafe,
<I as Interner>::InternedType: RefUnwindSafe,
<I as Interner>::InternedLifetime: RefUnwindSafe,
impl<I> Send for TraitDatum<I>where
<I as Interner>::DefId: Send,
<I as Interner>::InternedVariableKinds: Send,
<I as Interner>::InternedSubstitution: Send,
<I as Interner>::InternedType: Send,
<I as Interner>::InternedLifetime: Send,
impl<I> Sync for TraitDatum<I>where
<I as Interner>::DefId: Sync,
<I as Interner>::InternedVariableKinds: Sync,
<I as Interner>::InternedSubstitution: Sync,
<I as Interner>::InternedType: Sync,
<I as Interner>::InternedLifetime: Sync,
impl<I> Unpin for TraitDatum<I>where
<I as Interner>::DefId: Unpin,
<I as Interner>::InternedVariableKinds: Unpin,
<I as Interner>::InternedSubstitution: Unpin,
<I as Interner>::InternedType: Unpin,
<I as Interner>::InternedLifetime: Unpin,
impl<I> UnwindSafe for TraitDatum<I>where
<I as Interner>::DefId: UnwindSafe,
<I as Interner>::InternedVariableKinds: UnwindSafe,
<I as Interner>::InternedSubstitution: UnwindSafe,
<I as Interner>::InternedType: UnwindSafe,
<I as Interner>::InternedLifetime: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Cast for T
impl<T> Cast for T
source§fn cast<U>(self, interner: <U as HasInterner>::Interner) -> Uwhere
Self: CastTo<U>,
U: HasInterner,
fn cast<U>(self, interner: <U as HasInterner>::Interner) -> Uwhere
Self: CastTo<U>,
U: HasInterner,
U
using CastTo
.source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.