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
Rollup merge of rust-lang#68004 - nikomatsakis:negative-impls, r=varkor
permit negative impls for non-auto traits
This is a prototype impl that extends `impl !Trait` beyond auto traits. It is not integrated with coherence or anything else, and hence only serves to prevent downstream impls (but not to allow downstream crates to rely on the absence of such impls for coherence purposes).
Fixesrust-lang#66544
TODO:
- [x] need a test that you can't rely on negative impls for coherence purposes
- [x] test that negative impls cannot specialize positive ones
- [x] test that positive impls cannot specialize negative ones
- [x] extend negative impl to `Clone` in order to fully fixrust-lang#66544
- [x] and maybe make `CoerceUnsized` unsafe? -- that problem is now split out into rust-lang#68015
- [x] introduce feature flag and prepare a write-up
- [x] improve diagnostics?
With the feature gate `negative_impls`, you can write negative impls as well as positive ones:
10
+
11
+
```rust
12
+
#![feature(negative_impls)]
13
+
traitDerefMut { }
14
+
impl<T:?Sized> !DerefMutfor&T { }
15
+
```
16
+
17
+
Negative impls indicate a semver guarantee that the given trait will not be implemented for the given types. Negative impls play an additional purpose for auto traits, described below.
18
+
19
+
Negative impls have the following characteristics:
20
+
21
+
* They do not have any items.
22
+
* They must obey the orphan rules as if they were a positive impl.
23
+
* They cannot "overlap" with any positive impls.
24
+
25
+
## Semver interaction
26
+
27
+
It is a breaking change to remove a negative impl. Negative impls are a commitment not to implement the given trait for the named types.
28
+
29
+
## Orphan and overlap rules
30
+
31
+
Negative impls must obey the same orphan rules as a positive impl. This implies you cannot add a negative impl for types defined in upstream crates and so forth.
32
+
33
+
Similarly, negative impls cannot overlap with positive impls, again using the same "overlap" check that we ordinarily use to determine if two impls overlap. (Note that positive impls typically cannot overlap with one another either, except as permitted by specialization.)
34
+
35
+
## Interaction with auto traits
36
+
37
+
Declaring a negative impl `impl !SomeAutoTrait for SomeType` for an
38
+
auto-trait serves two purposes:
39
+
40
+
* as with any trait, it declares that `SomeType` will never implement `SomeAutoTrait`;
41
+
* it disables the automatic `SomeType: SomeAutoTrait` impl that would otherwise have been generated.
42
+
43
+
Note that, at present, there is no way to indicate that a given type
44
+
does not implement an auto trait *but that it may do so in the
45
+
future*. For ordinary types, this is done by simply not declaring any
46
+
impl at all, but that is not an option for auto traits. A workaround
47
+
is that one could embed a marker type as one of the fields, where the
48
+
marker type is `!AutoTrait`.
49
+
50
+
## Immediate uses
51
+
52
+
Negative impls are used to declare that `&T: !DerefMut` and `&mut T: !Clone`, as required to fix the soundness of `Pin` described in [#66544](https://github.com/rust-lang/rust/issues/66544).
53
+
54
+
This serves two purposes:
55
+
56
+
* For proving the correctness of unsafe code, we can use that impl as evidence that no `DerefMut` or `Clone` impl exists.
57
+
* It prevents downstream crates from creating such impls.
0 commit comments