mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-26 13:01:27 +03:00
sembr src/coherence.md
This commit is contained in:
@@ -8,13 +8,14 @@ Coherence checking is what detects both of trait impls and inherent impls overla
|
||||
Overlapping trait impls always produce an error,
|
||||
while overlapping inherent impls result in an error only if they have methods with the same name.
|
||||
|
||||
Checking for overlaps is split in two parts. First there's the [overlap check(s)](#overlap-checks),
|
||||
Checking for overlaps is split in two parts.
|
||||
First there's the [overlap check(s)](#overlap-checks),
|
||||
which finds overlaps between traits and inherent implementations that the compiler currently knows about.
|
||||
|
||||
However, Coherence also results in an error if any other impls **could** exist,
|
||||
even if they are currently unknown.
|
||||
even if they are currently unknown.
|
||||
This affects impls which may get added to upstream crates in a backwards compatible way,
|
||||
and impls from downstream crates.
|
||||
and impls from downstream crates.
|
||||
This is called the Orphan check.
|
||||
|
||||
## Overlap checks
|
||||
@@ -25,7 +26,7 @@ Overlap checks always consider pairs of implementations, comparing them to each
|
||||
|
||||
Overlap checking for inherent impl blocks is done through `fn check_item` (in coherence/inherent_impls_overlap.rs),
|
||||
where you can very clearly see that (at least for small `n`), the check really performs `n^2`
|
||||
comparisons between impls.
|
||||
comparisons between impls.
|
||||
|
||||
In the case of traits, this check is currently done as part of building the [specialization graph](traits/specialization.md),
|
||||
to handle specializing impls overlapping with their parent, but this may change in the future.
|
||||
@@ -37,7 +38,7 @@ Overlapping is sometimes partially allowed:
|
||||
1. for marker traits
|
||||
2. under [specialization](traits/specialization.md)
|
||||
|
||||
but normally isn't.
|
||||
but normally isn't.
|
||||
|
||||
The overlap check has various modes (see [`OverlapMode`]).
|
||||
Importantly, there's the explicit negative impl check, and the implicit negative impl check.
|
||||
@@ -47,9 +48,9 @@ Both try to prove that an overlap is definitely impossible.
|
||||
|
||||
### The explicit negative impl check
|
||||
|
||||
This check is done in [`impl_intersection_has_negative_obligation`].
|
||||
This check is done in [`impl_intersection_has_negative_obligation`].
|
||||
|
||||
This check tries to find a negative trait implementation.
|
||||
This check tries to find a negative trait implementation.
|
||||
For example:
|
||||
|
||||
```rust
|
||||
|
||||
Reference in New Issue
Block a user