Skip to content

Wrong error reporting for missing trait implementations #20366

Closed
@flaper87

Description

@flaper87

Arc's Send and Sync looks like:

#[unsafe_no_drop_flag]
#[stable]
pub struct Arc<T> {
// FIXME #12808: strange name to try to avoid interfering with
// field accesses of the contained type via Deref
_ptr: NonZero<*mut ArcInner<T>>,
}

unsafe impl<T: Sync + Send> Send for Arc<T> { }
unsafe impl<T: Sync + Send> Sync for Arc<T> { }

Therefore, the code below, makes the Arc object neither Send nor Sync.

extern crate alloc;

use std::kinds::Send;
use alloc::arc::Arc;

struct Inner(*mut u8);

unsafe impl Send for Inner {}

fn is_send<T:Send>(s: T) {}

fn main() {
    let arc = Arc::new(Inner(0 as *mut u8));
    is_send(arc);
}

Unfortunately, the error reported is quite obscure, as it mentions that the is_send function requires T to be Sync which is not true.

test.rs:14:5: 14:12 error: the trait `core::kinds::Sync` is not implemented for the type `*mut u8`
test.rs:14     is_send(arc);
               ^~~~~~~
test.rs:14:5: 14:12 note: the type `*mut u8` must implement `core::kinds::Sync` because it appears within the type `Inner`
test.rs:14     is_send(arc);
               ^~~~~~~
test.rs:14:5: 14:12 note: the trait `core::kinds::Sync` must be implemented because it is required by `is_send`
test.rs:14     is_send(arc);
               ^~~~~~~
error: aborting due to previous error

If we put the Arc in a wrapper type, then the error becomes even more obscure as it reports that Send is not being guaranteed because Sync is not implemented. Although this being true to some extent, it's not helpful at all:

extern crate alloc;

use std::kinds::Send;
use alloc::arc::Arc;

struct Inner(*mut u8);

unsafe impl Send for Inner {}

struct MyWrapper(Arc<Inner>);

fn is_send<T:Send>(s: T) {}

fn main() {
    let arc = MyWrapper(Arc::new(Inner(0 as *mut u8)));
    is_send(arc);
}
test.rs:16:5: 16:12 error: the trait `core::kinds::Sync` is not implemented for the type `*mut u8`
test.rs:16     is_send(arc);
               ^~~~~~~
test.rs:16:5: 16:12 note: the type `*mut u8` must implement `core::kinds::Sync` because it appears within the type `Inner`
test.rs:16     is_send(arc);
               ^~~~~~~
test.rs:16:5: 16:12 note: the type `Inner` must implement `core::kinds::Sync` because it appears within the type `MyWrapper`
test.rs:16     is_send(arc);
               ^~~~~~~
test.rs:16:5: 16:12 note: the trait `core::kinds::Send` must be implemented because it is required by `is_send`
test.rs:16     is_send(arc);
               ^~~~~~~
error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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