You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/coherence.md
+37-12
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,40 @@
1
-
# Overlap checks
2
1
3
-
As part of checking items (specifically: structs, enums, traits, unions),
4
-
the compiler checks whether impl blocks overlap, for example because they define the same functions.
5
-
This is an example an overlap check.
6
-
The same overlap check is done when constructing a [specialization graph](./specialization.md).
7
-
Here, trait implementations could overlap, for example because of a conflicting blanket implementation overlapping with some specific implementation.
2
+
# Coherence
8
3
9
-
The overlap check always compares two impls.
10
-
In the case of inherent impl blocks, this means that at least for small n,
11
-
rustc quite literally compares each impl to each other impl block in an `n^2` loop
12
-
(see `fn check_item` in coherence/inherent_impls_overlap.rs).
4
+
> NOTE: this is based on [notes by @lcnr](https://github.com/rust-lang/rust/pull/121848)
5
+
6
+
Coherence checking is what detects both of trait impls and inherent impls overlapping with others.
7
+
(reminder: [inherent impls](https://doc.rust-lang.org/reference/items/implementations.html#inherent-implementations) are impls of concrete types like `impl MyStruct {}`)
8
+
9
+
Overlapping trait impls always produce an error,
10
+
while overlapping inherent impls result in an error only if they have methods with the same name.
11
+
12
+
Checking for overlaps is split in two parts. First there's the [overlap check(s)](#overlap-checks),
13
+
which finds overlaps between traits and inherent implementations that the compiler currently knows about.
14
+
15
+
However, Coherence also results in an error if any other impls **could** exist,
16
+
even if they are currently unknown.
17
+
This affects impls which may get added to upstream crates in a backwards compatible way,
18
+
and impls from downstream crates.
19
+
This is called the Orphan check.
20
+
21
+
## Overlap checks
22
+
23
+
Overlap checks are performed for both inherent impls, and for trait impls.
24
+
This uses the same overlap checking code, really done as two separate analyses.
25
+
Overlap checks always consider pairs of implementations, comparing them to each other.
26
+
27
+
Overlap checking for inherent impl blocks is done through `fn check_item` in coherence/inherent_impls_overlap.rs),
28
+
where you can very clearly see that (at least for small `n`), the check really performs `n^2`
29
+
comparisons between impls.
30
+
31
+
In the case of traits, this check is currently done as part of building the [specialization graph](./specialization.md),
32
+
to handle specializing impls overlapping with their parent, but this may change in the future.
33
+
34
+
In both cases, all pairs of impls are checked for overlap.
13
35
14
36
Overlapping is sometimes partially allowed:
37
+
15
38
1. for maker traits
16
39
2. under [specialization](./specialization.md)
17
40
@@ -23,7 +46,7 @@ Both try to apply negative reasoning to prove that an overlap is definitely impo
0 commit comments