Skip to content

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

Closed
@Hoverbear

Description

@Hoverbear

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

Metadata

Metadata

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