Skip to content

add WellKnownTraits chapter to the book #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [Type equality and unification](./clauses/type_equality.md)
- [Implied bounds](./clauses/implied_bounds.md)
- [Lowering rules](./clauses/lowering_rules.md)
- [Well known traits](./clauses/well_known_traits.md)
- [Well-formedness checking](./clauses/wf.md)
- [Canonical queries](./canonical_queries.md)
- [Canonicalization](./canonical_queries/canonicalization.md)
Expand Down
52 changes: 52 additions & 0 deletions book/src/clauses/well_known_traits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Well known traits

For most traits, the question of whether some type T implements the trait is determined by
looking solely at the impls that exist for the trait. But there are some well-known traits
where we have "built-in" impls that are never expressly written in the compiler, they are
built-in to the language itself. In some cases, these impls also encode complex conditions
that an ordinary impl cannot express. To address this, chalk has a notion of a `WellKnownTrait`
-- basically, a trait which is inherent to the language and where we will generate custom logic.

As an example, consider the logic for `Sized` in regards to structs: A struct can have
at most one `!Sized` field, and it must be the last. And the last field isn't `Sized`,
then neither is the struct itself.

Chalk has two main places that deal with well known trait logic:
1) [`chalk-solve\clauses\builtin_traits`][builtin_traits_mod], which generates built-in implementations
for well-known traits.
2) [well-formedness](wf.md) checks, some of which need to know about well known traits.

[builtin_traits_mod]: https://github.com/rust-lang/chalk/blob/master/chalk-solve/src/clauses/builtin_traits.rs

# Auto traits

Auto traits, while not exactly well known traits, do also have special logic.
The idea is that the type implements an auto trait if all data owned by that type implements it,
with an ability to specifically opt-out or opt-in. Additionally, auto traits are [coinductive][coinductive_section].
Some common examples of auto traits are `Send` and `Sync`.

[coinductive_section]: ../engine/logic/coinduction.html#coinduction-and-refinement-strands

# Current state
| Type | Copy | Clone | Sized | Unsize | Drop | Fn | Unpin | Generator | auto traits |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| tuple types | ✅ | ✅ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| structs | ⚬ | ⚬ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ✅ |
| scalar types | 📚 | 📚 | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| trait objects | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ |
| functions ptrs | ✅ | ✅ | ✅ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
| arrays❌ | ❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| slices❌ | ❌ | ❌ | ⚬ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| closures❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
| generators❌ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ⚬ | ❌ | ❌ | ❌ |
| gen. witness❌ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| ----------- | | | | | | | | | |
| well-formedness | ✅ | ⚬ | ✅ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ |

legend:
⚬ - not applicable
✅ - implemented
📚 - implementation provided in libcore
❌ - not implemented

❌ after a type name means that type is not yet in chalk