-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Description
I tried this code:
use std::marker::PhantomData;
struct MyType {
_definitely_not_thread_safe: PhantomData<*mut ()>,
}
impl<T: Send> From<T> for MyType {
fn from(_: T) -> MyType {
todo!()
}
}
I expected to see this happen: It compiles, because there is no conflict - MyType
clearly doesn't implement Send
, so blanket implementation for T: Send
is valid and shouldn't conflict with From<MyType> for MyType
.
Instead, this happened:
error[E0119]: conflicting implementations of trait `From<MyType>` for type `MyType`
--> <source>:7:1
|
7 | impl<T: Send> From<T> for MyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<T> From<T> for T;
= note: upstream crates may add a new impl of trait `std::marker::Send` for type `std::marker::PhantomData<*mut ()>` in future versions
This error seems wrong or misleading at least. After all, the whole point of using marker type in this way is to make something as non-Send/non-Sync.
I know of the unstable negative_impls
feature, which would have the intended behaviour here even without a marker, but I'd assume that at least Rust core would already use negative trait impls for built-in types. Indeed, what makes such error even more confusing is that negative trait impl is exactly what Rust docs renders for these types:

Meta
(applies to all versions of the compiler, it's a language issue)