Skip to content

Commit b0a63cb

Browse files
authoredMar 26, 2020
Rollup merge of #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). Fixes #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 fix #66544 - [x] and maybe make `CoerceUnsized` unsafe? -- that problem is now split out into #68015 - [x] introduce feature flag and prepare a write-up - [x] improve diagnostics?
2 parents 3b1d735 + 0d07026 commit b0a63cb

File tree

133 files changed

+869
-351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+869
-351
lines changed
 
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# `negative_impls`
2+
3+
The tracking issue for this feature is [#68318].
4+
5+
[#68318]: https://github.com/rust-lang/rust/issues/68318
6+
7+
----
8+
9+
With the feature gate `negative_impls`, you can write negative impls as well as positive ones:
10+
11+
```rust
12+
#![feature(negative_impls)]
13+
trait DerefMut { }
14+
impl<T: ?Sized> !DerefMut for &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.

‎src/doc/unstable-book/src/language-features/optin-builtin-traits.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ The `optin_builtin_traits` feature gate allows you to define auto traits.
1010

1111
Auto traits, like [`Send`] or [`Sync`] in the standard library, are marker traits
1212
that are automatically implemented for every type, unless the type, or a type it contains,
13-
has explicitly opted out via a negative impl.
13+
has explicitly opted out via a negative impl. (Negative impls are separately controlled
14+
by the `negative_impls` feature.)
1415

1516
[`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html
1617
[`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html
@@ -22,6 +23,7 @@ impl !Trait for Type
2223
Example:
2324

2425
```rust
26+
#![feature(negative_impls)]
2527
#![feature(optin_builtin_traits)]
2628

2729
auto trait Valid {}
@@ -43,3 +45,63 @@ fn main() {
4345
// must_be_valid( MaybeValid(False) );
4446
}
4547
```
48+
49+
## Automatic trait implementations
50+
51+
When a type is declared as an `auto trait`, we will automatically
52+
create impls for every struct/enum/union, unless an explicit impl is
53+
provided. These automatic impls contain a where clause for each field
54+
of the form `T: AutoTrait`, where `T` is the type of the field and
55+
`AutoTrait` is the auto trait in question. As an example, consider the
56+
struct `List` and the auto trait `Send`:
57+
58+
```rust
59+
struct List<T> {
60+
data: T,
61+
next: Option<Box<List<T>>>,
62+
}
63+
```
64+
65+
Presuming that there is no explicit impl of `Send` for `List`, the
66+
compiler will supply an automatic impl of the form:
67+
68+
```rust
69+
struct List<T> {
70+
data: T,
71+
next: Option<Box<List<T>>>,
72+
}
73+
74+
unsafe impl<T> Send for List<T>
75+
where
76+
T: Send, // from the field `data`
77+
Option<Box<List<T>>>: Send, // from the field `next`
78+
{ }
79+
```
80+
81+
Explicit impls may be either positive or negative. They take the form:
82+
83+
```rust,ignore
84+
impl<...> AutoTrait for StructName<..> { }
85+
impl<...> !AutoTrait for StructName<..> { }
86+
```
87+
88+
## Coinduction: Auto traits permit cyclic matching
89+
90+
Unlike ordinary trait matching, auto traits are **coinductive**. This
91+
means, in short, that cycles which occur in trait matching are
92+
considered ok. As an example, consider the recursive struct `List`
93+
introduced in the previous section. In attempting to determine whether
94+
`List: Send`, we would wind up in a cycle: to apply the impl, we must
95+
show that `Option<Box<List>>: Send`, which will in turn require
96+
`Box<List>: Send` and then finally `List: Send` again. Under ordinary
97+
trait matching, this cycle would be an error, but for an auto trait it
98+
is considered a successful match.
99+
100+
## Items
101+
102+
Auto traits cannot have any trait items, such as methods or associated types. This ensures that we can generate default implementations.
103+
104+
## Supertraits
105+
106+
Auto traits cannot have supertraits. This is for soundness reasons, as the interaction of coinduction with implied bounds is difficult to reconcile.
107+

0 commit comments

Comments
 (0)