Skip to content

Error says std might implement Send for types which are defined as !Send #146427

@RReverser

Description

@RReverser

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:

Image

Meta

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

Metadata

Metadata

Assignees

Labels

A-coherenceArea: CoherenceA-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions