Skip to content

Wrong error reporting for missing trait implementations #20366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flaper87 opened this issue Dec 31, 2014 · 3 comments
Closed

Wrong error reporting for missing trait implementations #20366

flaper87 opened this issue Dec 31, 2014 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@flaper87
Copy link
Contributor

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
@flaper87 flaper87 added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 31, 2014
@mzabaluev
Copy link
Contributor

Might be related: #19950

@steveklabnik
Copy link
Member

Triage: updated code:

extern crate alloc;

use std::marker::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);
}

error:

hello.rs:14:5: 14:12 error: the trait `core::marker::Sync` is not implemented for the type `*mut u8` [E0277]
hello.rs:14     is_send(arc);
                ^~~~~~~
hello.rs:14:5: 14:12 help: run `rustc --explain E0277` to see a detailed explanation
hello.rs:14:5: 14:12 note: `*mut u8` cannot be shared between threads safely
hello.rs:14:5: 14:12 note: required because it appears within the type `Inner`
hello.rs:14:5: 14:12 note: required by `is_send`

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

The error today indicates that Sync is required because of the requirements on the impl of Send for Arc; I think this solves this issue.

error[E0277]: the trait bound `*mut u8: std::marker::Sync` is not satisfied in `Inner`
  --> test.rs:15:5
   |
15 |     is_send(arc);
   |     ^^^^^^^ `*mut u8` cannot be shared between threads safely
   |
   = help: within `Inner`, the trait `std::marker::Sync` is not implemented for `*mut u8`
   = note: required because it appears within the type `Inner`
   = note: required because of the requirements on the impl of `std::marker::Send` for `alloc::arc::Arc<Inner>`
   = note: required by `is_send`

error: aborting due to previous error(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants