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>

source

pub fn is_auto_trait(&self) -> bool

source

pub fn is_non_enumerable_trait(&self) -> bool

source

pub fn is_coinductive_trait(&self) -> bool

source

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>

source§

fn clone(&self) -> TraitDatum<I>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<I: Debug + Interner> Debug for TraitDatum<I>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<I: Hash + Interner> Hash for TraitDatum<I>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<I: PartialEq + Interner> PartialEq for TraitDatum<I>

source§

fn eq(&self, other: &TraitDatum<I>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<I: Interner> RenderAsRust<I> for TraitDatum<I>

source§

fn fmt(&self, s: &InternalWriterState<'_, I>, f: &mut Formatter<'_>) -> Result

source§

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>

source§

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>

source§

fn visit_with<B>( &self, visitor: &mut dyn TypeVisitor<I, BreakTy = B>, outer_binder: DebruijnIndex ) -> ControlFlow<B>

Apply the given visitor 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.
source§

impl<I: Eq + Interner> Eq for TraitDatum<I>

source§

impl<I: Interner> StructuralEq for TraitDatum<I>

source§

impl<I: Interner> StructuralPartialEq for TraitDatum<I>

Auto Trait Implementations§

§

impl<I> RefUnwindSafe for TraitDatum<I>
where <I as Interner>::DefId: RefUnwindSafe, <I as Interner>::InternedLifetime: RefUnwindSafe, <I as Interner>::InternedSubstitution: RefUnwindSafe, <I as Interner>::InternedType: RefUnwindSafe, <I as Interner>::InternedVariableKinds: RefUnwindSafe,

§

impl<I> Send for TraitDatum<I>
where <I as Interner>::DefId: Send, <I as Interner>::InternedLifetime: Send, <I as Interner>::InternedSubstitution: Send, <I as Interner>::InternedType: Send, <I as Interner>::InternedVariableKinds: Send,

§

impl<I> Sync for TraitDatum<I>
where <I as Interner>::DefId: Sync, <I as Interner>::InternedLifetime: Sync, <I as Interner>::InternedSubstitution: Sync, <I as Interner>::InternedType: Sync, <I as Interner>::InternedVariableKinds: Sync,

§

impl<I> Unpin for TraitDatum<I>
where <I as Interner>::DefId: Unpin, <I as Interner>::InternedLifetime: Unpin, <I as Interner>::InternedSubstitution: Unpin, <I as Interner>::InternedType: Unpin, <I as Interner>::InternedVariableKinds: Unpin,

§

impl<I> UnwindSafe for TraitDatum<I>
where <I as Interner>::DefId: UnwindSafe, <I as Interner>::InternedLifetime: UnwindSafe, <I as Interner>::InternedSubstitution: UnwindSafe, <I as Interner>::InternedType: UnwindSafe, <I as Interner>::InternedVariableKinds: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Cast for T

§

fn cast<U>(self, interner: <U as HasInterner>::Interner) -> U
where Self: CastTo<U>, U: HasInterner,

Cast a value to type U using CastTo.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T, I> VisitExt<I> for T
where I: Interner, T: TypeVisitable<I>,

§

fn has_free_vars(&self, interner: I) -> bool

Check whether there are free (non-bound) variables.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more