Skip to content

Commit 5b2346a

Browse files
authored
Merge pull request #326 from alercah/auto-traits
Clarify semantics of auto traits.
2 parents f0a7e73 + db6ed4a commit 5b2346a

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/special-types-and-traits.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ immutable [`static` items].
8585
The [`Send`], [`Sync`], [`UnwindSafe`] and [`RefUnwindSafe`] traits are _auto
8686
traits_. Auto traits have special properties.
8787

88-
First, auto traits are automatically implemented using the following rules:
88+
If no explicit implementation or negative implementation is written out for an
89+
auto trait for a given type, then the compiler implements it automatically
90+
according to the following rules:
8991

9092
* `&T`, `&mut T`, `*const T`, `*mut T`, `[T; n]` and `[T]` implement the trait
9193
if `T` does.
@@ -96,12 +98,23 @@ First, auto traits are automatically implemented using the following rules:
9698
closure that captures a `T` by shared reference and a `U` by value implements
9799
any auto traits that both `&T` and `U` do.
98100

101+
For generic types (counting the built-in types above as generic over `T`), if an
102+
generic implementation is available, then the compiler does not automatically
103+
implement it for types that could use the implementation except that they do not
104+
meet the requisite trait bounds. For instance, the standard library implements
105+
`Send` for all `&T` where `T` is `Sync`; this means that the compiler will not
106+
implement `Send` for `&T` if `T` is `Send` but not `Sync`.
107+
99108
Auto traits can also have negative implementations, shown as `impl !AutoTrait
100109
for T` in the standard library documentation, that override the automatic
101110
implementations. For example `*mut T` has a negative implementation of `Send`,
102-
and so `*mut T` and `(*mut T,)` are not `Send`. Finally, auto traits may
103-
be added as a bound to any [trait object]\: `Box<Debug + Send + UnwindSafe>` is
104-
a valid type.
111+
and so `*mut T` is not `Send`, even if `T` is. There is currently no stable way
112+
to specify additional negative implementations; they exist only in the standard
113+
library.
114+
115+
Auto traits may be added as an additional bound to any [trait object], even
116+
though normally only one trait is allowed. For instance, `Box<dyn Debug + Send +
117+
UnwindSafe>` is a valid type.
105118

106119
## `Sized`
107120

0 commit comments

Comments
 (0)