Skip to content
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

add WellKnownTraits chapter to the book #428

Merged
merged 2 commits into from
May 5, 2020

Conversation

basil-cow
Copy link
Contributor

@basil-cow basil-cow commented May 1, 2020

I don't know what to use for n/a so I chose 🗿, if you have a better idea I'll change it
closes #416
please be(!) harsh, English is not my strongest suit

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

A few comments.

@@ -0,0 +1,57 @@
# Well known traits

Not all traits can be encoded in Rust's type system: special traits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better phrasing would be: "Not all trait logic can be encoded in Rust types: ..."

Comment on lines 8 to 11
As an example, consider the following two aspects of `Sized` logic:
1) In order to prove that a struct implements `Sized`, we need to prove
that the last field of that struct is `Sized`.
2) Structs need all of their fields, except, maybe, the last one to be `Sized`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While these two points are valid, the flow is a bit weird. I would say something like

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.

Comment on lines 24 to 31
Auto traits are another kind of well known traits.
The idea is that the type implements an auto trait if all data owned by that type implements it,
with an ability to opt-out via special syntax:
```rust,ignore
impl !AutoTrait for Foo {}
```
Common examples of auto trais are `Send` and `Sync`. Since this semantic is not expressable with
"regular" impls, it needs special support in chalk too.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, this is...not exactly correct. At least, not by the definition we've been using for well known traits. Instead I would say something like

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. Some common examples of auto traits are Send and Sync.

| well-formedness | ✅ | 🗿 | ✅ | 🗿 | ✅ | 🗿 | 🗿 | 🗿 | 🗿 |

legend:
🗿 - not applicable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe just leave this empty. A bit less clutter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ? Makes it clear we didn't forget, but is unobtrusive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works :)

❌ - not implemented
❌ in the column type means that type is not yet in chalk

The list of types not yet in chalk is not full, but auto traits/`WellKnownTrait`s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Areredify! Left some suggestions. This is very nice.

Not all traits can be encoded in Rust's type system: special traits
like `Sized`, `Drop` or `Unsize` need additional compiler support in order to
function properly. To address this, chalk introduces a notion of `WellKnownTrait`s:
a subset of rustc's trait lang items that need special handling in trait system logic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the key point here is something like:

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 aWellKnownTrait -- basically, a trait which is built-in to the language and where we will generate custom logic.

| well-formedness | ✅ | 🗿 | ✅ | 🗿 | ✅ | 🗿 | 🗿 | 🗿 | 🗿 |

legend:
🗿 - not applicable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ? Makes it clear we didn't forget, but is unobtrusive.

| structs | 🗿 | 🗿 | ✅ | ✅ | 🗿 | 🗿 | 🗿 | 🗿 | ✅ |
| scalar types | 📚 | 📚 | ✅ | 🗿 | 🗿 | 🗿 | 🗿 | 🗿 | ❌ |
| trait objects | 🗿 | 🗿 | 🗿 | ✅ | 🗿 | 🗿 | 🗿 | 🗿 | 🗿 |
| functions | ✅ | ✅ | ✅ | 🗿 | 🗿 | ❌ | 🗿 | 🗿 | ❌ |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this refer to "function pointers" or FnDef types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it refers to TyData::Function which is presume is FnDef

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's function pointer, and should be renamed.

# Current state
| Type | Copy | Clone | Sized | Unsize | Drop | Fn | Unpin | Generator | auto traits |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| tuple types | ✅ | ✅ | ✅ | ✅ | 🗿 | 🗿 | 🗿 | 🗿 | ❌ |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these can link to the relevant enum variant, where applicable

@nikomatsakis nikomatsakis merged commit e1234f6 into rust-lang:master May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

document the "built-in trait impls" and where they live
3 participants