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
@@ -61,7 +61,7 @@ Thus, it is probably best to look at the only *truly authoritative* source on th
61
61
62
62
## The Orphan Check in rustc
63
63
64
-
The orphan check as implemented today in the Rust compiler takes place in the `[orphan_check](https://github.com/rust-lang/rust/blob/b7c6e8f1805cd8a4b0a1c1f22f17a89e9e2cea23/src/librustc/traits/coherence.rs#L236)` function which is called [for every declared impl](https://github.com/rust-lang/rust/blob/b7c6e8f1805cd8a4b0a1c1f22f17a89e9e2cea23/src/librustc_typeck/coherence/orphan.rs#L45). Since implementations for locally defined traits are always defined, that function returns OK if the trait being implemented is local. Otherwise, it dispatches to the `[orphan_check_trait_ref](https://github.com/rust-lang/rust/blob/b7c6e8f1805cd8a4b0a1c1f22f17a89e9e2cea23/src/librustc/traits/coherence.rs#L343)` function which does the major orphan rules checking.
64
+
The orphan check as implemented today in the Rust compiler takes place in the [`orphan_check`](https://github.com/rust-lang/rust/blob/b7c6e8f1805cd8a4b0a1c1f22f17a89e9e2cea23/src/librustc/traits/coherence.rs#L236) function which is called [for every declared impl](https://github.com/rust-lang/rust/blob/b7c6e8f1805cd8a4b0a1c1f22f17a89e9e2cea23/src/librustc_typeck/coherence/orphan.rs#L45). Since implementations for locally defined traits are always defined, that function returns OK if the trait being implemented is local. Otherwise, it dispatches to the [`orphan_check_trait_ref`](https://github.com/rust-lang/rust/blob/b7c6e8f1805cd8a4b0a1c1f22f17a89e9e2cea23/src/librustc/traits/coherence.rs#L343) function which does the major orphan rules checking.
65
65
66
66
Recall that the impls we are dealing with are in the form `impl<T0…Tn> Trait<P1…Pn> for P0`.
67
67
@@ -90,37 +90,39 @@ Here’s how the lowering rules would look:
90
90
For each trait `Trait`,
91
91
92
92
- If `Trait` is local to the current crate, we generate:
Here, we have modelled every possible case of `P1` to `Pn` being local and then checked if all prior type parameters are fully visible. This truly is a direct translation of the rules listed above!
121
+
IsFullyVisible(Pn-1),
122
+
IsLocal(Pn)
123
+
}
124
+
```
125
+
Here, we have modelled every possible case of `P1` to `Pn` being local and then checked if all prior type parameters are fully visible. This truly is a direct translation of the rules listed above!
124
126
125
127
Now, to complete the orphan check, we can iterate over each impl of the same form as before and check if `LocalImplAllowed(P0: Trait<P1…Pn>)` is provable.
126
128
@@ -131,16 +133,20 @@ The purpose of the overlap check is to ensure that there is only up to one impl
131
133
132
134
This is a simple application of the mathematical law:
133
135
134
-
> If two sets $$A$$ and $$B$$ are disjoint, then $$A\cap B = \emptyset$$
136
+
> If two sets *A* and *B* are disjoint, then *A* ∩ *B* = ∅
135
137
136
138
More concretely, let’s say you have the following two impls: ([example from RFC 1023](https://rust-lang.github.io/rfcs/1023-rebalancing-coherence.html#type-locality-and-negative-reasoning))
137
139
138
-
impl<T: Copy> Clone for T {..}
139
-
impl<U> Clone for MyType<U> {..}
140
+
```rust,ignore
141
+
impl<T: Copy> Clone for T { /* ... */ }
142
+
impl<U> Clone for MyType<U> { /* ... */ }
143
+
```
140
144
141
145
Then we’ll try to solve the following:
142
146
143
-
not { exists<T, U> { T = MyType<U>, T: Copy } }
147
+
```ignore
148
+
not { exists<T, U> { T = MyType<U>, T: Copy } }
149
+
```
144
150
145
151
One way to read this is to say “try to prove that there is no `MyType<U>` for any `U` that implements the `Copy` trait”. The reason we’re trying to prove this is because if there is such an implementation, then the second impl would overlap with the first one. The first impl applies to any type that implements `Copy`.
146
152
@@ -181,47 +187,51 @@ This significantly narrows down our set of potential impls that we need to accou
181
187
182
188
For downstream crates, we need to add rules for all possible impls that they could potentially add using any upstream traits or traits in the current crate. We can do this by enumerating the possibilities generated from the orphan rules specified above:
183
189
184
-
185
-
// Given a trait MyTrait<P1...Pn> where WCs
186
-
187
-
forall<Self, P1...Pn> {
188
-
Implemented(Self: MyTrait<P1...Pn>) :-
189
-
WCs, // where clauses
190
-
Compatible,
191
-
DownstreamType(Self), // local to a downstream crate
192
-
CannotProve,
193
-
}
194
-
forall<Self, P1...Pn> {
195
-
Implemented(Self: MyTrait<P1...Pn>) :-
196
-
WCs,
197
-
Compatible,
198
-
IsFullyVisible(Self),
199
-
DownstreamType(P1),
200
-
CannotProve,
201
-
}
202
-
...
203
-
forall<Self, P1...Pn> {
204
-
Implemented(Self: MyTrait<P1...Pn>) :-
205
-
WCs,
206
-
Compatible,
207
-
IsFullyVisible(Self),
208
-
IsFullyVisible(P1),
209
-
...,
210
-
IsFullyVisible(Pn-1),
211
-
DownstreamType(Pn),
212
-
CannotProve,
213
-
}
190
+
```ignore
191
+
// Given a trait MyTrait<P1...Pn> where WCs
192
+
193
+
forall<Self, P1...Pn> {
194
+
Implemented(Self: MyTrait<P1...Pn>) :-
195
+
WCs, // where clauses
196
+
Compatible,
197
+
DownstreamType(Self), // local to a downstream crate
198
+
CannotProve,
199
+
}
200
+
forall<Self, P1...Pn> {
201
+
Implemented(Self: MyTrait<P1...Pn>) :-
202
+
WCs,
203
+
Compatible,
204
+
IsFullyVisible(Self),
205
+
DownstreamType(P1),
206
+
CannotProve,
207
+
}
208
+
...
209
+
forall<Self, P1...Pn> {
210
+
Implemented(Self: MyTrait<P1...Pn>) :-
211
+
WCs,
212
+
Compatible,
213
+
IsFullyVisible(Self),
214
+
IsFullyVisible(P1),
215
+
...,
216
+
IsFullyVisible(Pn-1),
217
+
DownstreamType(Pn),
218
+
CannotProve,
219
+
}
220
+
```
214
221
215
222
Perhaps somewhat surprisingly, `IsFullyVisible` works here too. This is because our previous definition of the lowering for `IsFullyVisible` was quite broad. By lowering *all* types in the current crate and in upstream crates with `IsFullyVisible`, that predicate covers the correct set of types here too. The orphan rules only require that there are no types parameters prior to the first local type. Types that are not type parameters and also by definition not downstream types are all of the types in the current crate and in upstream crates. This is exactly what `IsFullyVisible` covers.
216
223
217
224
Fundamental types in both the current crate and in upstream crates can be considered local in a downstream crate if they are provided with a downstream type. To model this, we can add an additional rule for fundamental types:
#[upstream] trait Foo<T, U, V> where Self: Eq<T> { ... }
232
+
```rust,ignore
233
+
#[upstream] trait Foo<T, U, V> where Self: Eq<T> { /* ... */ }
234
+
```
225
235
226
236
**The question is**: do we need to bring these where clauses down into the rule that we generate for the overlap check?
227
237
**Answer:** Yes. Since the trait can only be implemented for types that satisfy its where clauses, it makes sense to also limit our assumption of compatible impls to impls that can exist.
@@ -234,60 +244,79 @@ Thus, based on the discussion above, the overlap check with coherence in mind ca
234
244
235
245
236
246
- All disjoint queries take place inside of `compatible`
247
+
237
248
-`compatible { G }` desugars into `forall<T> { (Compatible, DownstreamType(T)) => G }`, thus introducing a `Compatible` predicate using implication
249
+
238
250
- For each upstream trait `MyTrait<P1…Pn>`, we lower it into the following rule:
239
-
forall<Self, P1...Pn> {
240
-
Implemented(Self: MyTrait<P1...Pn>) :-
241
-
Compatible,
242
-
IsUpstream(Self),
243
-
IsUpstream(P1),
244
-
...,
245
-
IsUpstream(Pn),
246
-
CannotProve
247
-
}
248
-
This will accomplish our goal of returning an ambiguous answer whenever the overlap check query asks about any impls that an upstream crate may add in a compatible way. We determined in the discussion above that these are the only impls in any crate that can be added compatibly.
249
-
**Note:** Trait where clauses are lowered into the rule’s conditions as well as a prerequisite to everything else.
251
+
252
+
```ignore
253
+
forall<Self, P1...Pn> {
254
+
Implemented(Self: MyTrait<P1...Pn>) :-
255
+
Compatible,
256
+
IsUpstream(Self),
257
+
IsUpstream(P1),
258
+
...,
259
+
IsUpstream(Pn),
260
+
CannotProve
261
+
}
262
+
```
263
+
264
+
This will accomplish our goal of returning an ambiguous answer whenever the
265
+
overlap check query asks about any impls that an upstream crate may add in a
266
+
compatible way. We determined in the discussion above that these are the only
267
+
impls in any crate that can be added compatibly.
268
+
269
+
**Note:** Trait `where` clauses are lowered into the rule’s conditions as well as a prerequisite to everything else.
270
+
250
271
- For all traits `MyTrait<P1…Pn> where WCs` in the current crate and in upstream traits,
251
-
forall<Self, P1...Pn> {
252
-
Implemented(Self: MyTrait<P1...Pn>) :-
253
-
WCs, // where clauses
254
-
Compatible,
255
-
DownstreamType(Self), // local to a downstream crate
256
-
CannotProve,
257
-
}
258
-
forall<Self, P1...Pn> {
259
-
Implemented(Self: MyTrait<P1...Pn>) :-
260
-
WCs,
261
-
Compatible,
262
-
IsFullyVisible(Self),
263
-
DownstreamType(P1),
264
-
CannotProve,
265
-
}
266
-
...
267
-
forall<Self, P1...Pn> {
268
-
Implemented(Self: MyTrait<P1...Pn>) :-
269
-
WCs,
270
-
Compatible,
271
-
IsFullyVisible(Self),
272
-
IsFullyVisible(P1),
273
-
...,
274
-
IsFullyVisible(Pn-1),
275
-
DownstreamType(Pn),
276
-
CannotProve,
277
-
}
272
+
```ignore
273
+
forall<Self, P1...Pn> {
274
+
Implemented(Self: MyTrait<P1...Pn>) :-
275
+
WCs, // where clauses
276
+
Compatible,
277
+
DownstreamType(Self), // local to a downstream crate
278
+
CannotProve,
279
+
}
280
+
forall<Self, P1...Pn> {
281
+
Implemented(Self: MyTrait<P1...Pn>) :-
282
+
WCs,
283
+
Compatible,
284
+
IsFullyVisible(Self),
285
+
DownstreamType(P1),
286
+
CannotProve,
287
+
}
288
+
...
289
+
forall<Self, P1...Pn> {
290
+
Implemented(Self: MyTrait<P1...Pn>) :-
291
+
WCs,
292
+
Compatible,
293
+
IsFullyVisible(Self),
294
+
IsFullyVisible(P1),
295
+
...,
296
+
IsFullyVisible(Pn-1),
297
+
DownstreamType(Pn),
298
+
CannotProve,
299
+
}
300
+
```
301
+
278
302
- For fundamental types in both the current crate and in upstream crates,
Initially, when Niko and I started working on this, Niko suggested the following implementation:
283
310
284
-
- For each upstream trait, `MyTrait<P1…Pn>`, we lower it into the following rule:
285
-
forall<Self, P1...Pn> {
286
-
Implemented(Self: MyTrait<P1...Pn>) :-
287
-
Compatible,
288
-
not { LocalImplAllowed(Self: MyTrait<P1...Pn>) },
289
-
CannotProve
290
-
}
311
+
> For each upstream trait, `MyTrait<P1…Pn>`, we lower it into the following rule:
312
+
> ```ignore
313
+
> forall<Self, P1...Pn> {
314
+
> Implemented(Self: MyTrait<P1...Pn>) :-
315
+
> Compatible,
316
+
> not { LocalImplAllowed(Self: MyTrait<P1...Pn>) },
317
+
> CannotProve
318
+
> }
319
+
> ```
291
320
292
321
This appears to make sense because we need to assume that any impls that the current crate cannot add itself may exist somewhere else. By using `not { LocalImplAllowed(…) }`, we modelled exactly that. The problem is, that this assumption is actually too strong. What we actually need to model is that any **compatible** impls that the current crate cannot add itself may exist somewhere else. This is a **subset** of the impls covered by `not { LocalImplAllowed(…) }`.
0 commit comments