Skip to content

Confusing error when using type that doesn't conform to Sync due to a field that doesn't conform #24130

@lilyball

Description

@lilyball

When a type doesn't conform to Sync because it contains a field that doesn't conform, the error message yielded when trying to use that type where Sync is required is confusing. It talks about the type of the field itself, without actually mentioning that this is a field of the type in question, but it flags the point where the type is used rather than the field definition.

Example:

trait Trait {
    fn _foo(&self) {}
}

struct Foo {
    foo: Box<Trait+Send>
}

fn assert_is_sync<T: Sync>() {}

fn main() {
    assert_is_sync::<Foo>();
}

prints

unnamed.rs:12:5: 12:26 error: the trait `core::marker::Sync` is not implemented for the type `Trait + Send` [E0277]
unnamed.rs:12     assert_is_sync::<Foo>();
                  ^~~~~~~~~~~~~~~~~~~~~
unnamed.rs:12:5: 12:26 note: `Trait + Send` cannot be shared between threads safely
unnamed.rs:12     assert_is_sync::<Foo>();
                  ^~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

The only way to understand this error is to know that Foo contains a field whose type includes Trait + Send and correctly guess this means that Foo does not conform to Send.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions