Skip to content

Misleading error when implementing foreign trait on pointers to local types #99572

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
Hoverbear opened this issue Jul 21, 2022 · 5 comments · Fixed by #99686
Closed

Misleading error when implementing foreign trait on pointers to local types #99572

Hoverbear opened this issue Jul 21, 2022 · 5 comments · Fixed by #99686
Assignees
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

@Hoverbear
Copy link
Contributor

Hoverbear commented Jul 21, 2022

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e0a6148a3b4952ab3abd1f4bd7784029

use serde::ser::Serialize;

struct BooperDoop;

impl Serialize for *mut BooperDoop {
    // ...
}

fn main() {}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0117]](https://doc.rust-lang.org/stable/error-index.html#E0117): only traits defined in the current crate can be implemented for arbitrary types
 --> src/main.rs:5:1
  |
5 | impl Serialize for *mut BooperDoop {
  | ^^^^^^^^^^^^^^^^^^^---------------
  | |                  |
  | |                  `*mut BooperDoop` is not defined in the current crate
  | impl doesn't use only types from inside the current crate
  |
  = note: define and implement a trait or new type instead

For more information about this error, try `rustc --explain E0117`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

Compiling playground v0.0.1 (/playground)
error[[E0117]](https://doc.rust-lang.org/stable/error-index.html#E0117): only traits defined in the current crate can be implemented for arbitrary types
 --> src/main.rs:5:1
  |
5 | impl Serialize for *mut BooperDoop {
  | ^^^^^^^^^^^^^^^^^^^---------------
  | |                  |
  | |                  `*mut BooperDoop` is not defined in the current crate, try implementing on `Booperdoop`
  | impl doesn't use only types from inside the current crate
  |
  = note: define and implement a trait or new type instead

For more information about this error, try `rustc --explain E0117`.
error: could not compile `playground` due to previous error
@Hoverbear Hoverbear added 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. labels Jul 21, 2022
@compiler-errors
Copy link
Member

compiler-errors commented Jul 21, 2022

impl doesn't use only types from inside the current crate

This could use some work, too, I think. Maybe "impl mentions type from outside the current crate" might be a sufficient replacement, though we should probably make sure that wording is compatible with current coherence rules.

Though, in this case, for a non-#[fundamental] self type like *mut _, we could then replace that^ message with something even more specific.

I'm a bit skeptical of mentioning "try implementing Trait on YourLocalType" when we have impl Trait for *mut YourLocalType, since implementing something for T and *mut T can have very different meanings depending on the signatures inside the trait.

@vincenzopalazzo
Copy link
Member

Let's improve this case!

@rustbot claim

@Hoverbear
Copy link
Contributor Author

This was actually closer to my original error I got, it uses a type alias to create some extra confusion:

use serde::ser::Serialize;

struct BooperDoop;
type ScooperSwoop = *mut BooperDoop;

impl Serialize for ScooperSwoop {
    // ...
}

fn main() {}

@vincenzopalazzo
Copy link
Member

This was actually closer to my original error I got, it uses a type alias to create some extra confusion:

Amazing example :) thanks, I'm on it

@vincenzopalazzo
Copy link
Member

This is a reproducible example without serde!

use std::fmt;

struct BooperDoop;

impl fmt::Display for *mut BooperDoop {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "This not compile")
    }
}

fn main() {}

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
3 participants