Skip to content

Commit 5b148be

Browse files
committed
add WellKnownTraits chapter to the book
1 parent 1c20731 commit 5b148be

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [Type equality and unification](./clauses/type_equality.md)
1818
- [Implied bounds](./clauses/implied_bounds.md)
1919
- [Lowering rules](./clauses/lowering_rules.md)
20+
- [Well known traits](./clauses/well_known_traits.md)
2021
- [Well-formedness checking](./clauses/wf.md)
2122
- [Canonical queries](./canonical_queries.md)
2223
- [Canonicalization](./canonical_queries/canonicalization.md)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Well known traits
2+
3+
Not all traits can be encoded in Rust's type system: special traits
4+
like `Sized`, `Drop` or `Unsize` need additional compiler support in order to
5+
function properly. To address this, chalk introduces a notion of `WellKnownTrait`s:
6+
a subset of rustc's trait lang items that need special handling in trait system logic.
7+
8+
As an example, consider the following two aspects of `Sized` logic:
9+
1) In order to prove that a struct implements `Sized`, we need to prove
10+
that the last field of that struct is `Sized`.
11+
2) Structs need all of their fields, except, maybe, the last one to be `Sized`.
12+
13+
Neither of those aspects are expressable in Rust, so chalk generates
14+
special clauses used to encode them. These examples illustrate two main
15+
places that deal with well known traits:
16+
1) [`chalk-solve\clauses\builtin_traits`][builtin_traits_mod], which generates
17+
requirements for proving that a given type implements a well known trait.
18+
2) [well-formedness](wf.md) checks, some of which need to know about well known traits.
19+
20+
[builtin_traits_mod]: https://github.com/rust-lang/chalk/blob/master/chalk-solve/src/clauses/builtin_traits.rs
21+
22+
# Auto traits
23+
24+
Auto traits are another kind of well known traits.
25+
The idea is that the type implements an auto trait if all data owned by that type implements it,
26+
with an ability to opt-out via special syntax:
27+
```rust,ignore
28+
impl !AutoTrait for Foo {}
29+
```
30+
Common examples of auto trais are `Send` and `Sync`. Since this semantic is not expressable with
31+
"regular" impls, it needs special support in chalk too.
32+
33+
# Current state
34+
| Type | Copy | Clone | Sized | Unsize | Drop | Fn | Unpin | Generator | auto traits |
35+
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
36+
| tuple types ||||| 🗿 | 🗿 | 🗿 | 🗿 ||
37+
| structs | 🗿 | 🗿 ||| 🗿 | 🗿 | 🗿 | 🗿 ||
38+
| scalar types | 📚 | 📚 || 🗿 | 🗿 | 🗿 | 🗿 | 🗿 ||
39+
| trait objects | 🗿 | 🗿 | 🗿 || 🗿 | 🗿 | 🗿 | 🗿 | 🗿 |
40+
| functions |||| 🗿 | 🗿 || 🗿 | 🗿 ||
41+
| arrays❌ ||||| 🗿 | 🗿 | 🗿 | 🗿 ||
42+
| slices❌ ||| 🗿 || 🗿 | 🗿 | 🗿 | 🗿 ||
43+
| closures❌ |||| 🗿 | 🗿 || 🗿 | 🗿 ||
44+
| generators❌ | 🗿 | 🗿 || 🗿 | 🗿 | 🗿 ||||
45+
| gen. witness❌ | 🗿 | 🗿 | 🗿 | 🗿 | 🗿 | 🗿 | 🗿 | 🗿 ||
46+
| ----------- | | | | | | | | | |
47+
| well-formedness || 🗿 || 🗿 || 🗿 | 🗿 | 🗿 | 🗿 |
48+
49+
legend:
50+
🗿 - not applicable
51+
✅ - implemented
52+
📚 - implementation provided in libcore
53+
❌ - not implemented
54+
❌ in the column type means that type is not yet in chalk
55+
56+
The list of types not yet in chalk is not full, but auto traits/`WellKnownTrait`s
57+
implementation for them is fairly trivial, so it is not listed here.

0 commit comments

Comments
 (0)