Skip to content
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

Make "only traits defined in the current crate can be implemented for arbitrary types" less vague #96227

Closed
JosephTLyons opened this issue Apr 19, 2022 · 2 comments · Fixed by #96273
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

@JosephTLyons
Copy link
Contributor

JosephTLyons commented Apr 19, 2022

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

use chrono::offset::Utc;
use std::fmt::Display;

impl Display for Utc {}
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:4:1
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3ba9fdd15e921f6ac1ecff15c7d0b783#)  |
4 | impl Display for Utc {}
  | ^^^^^^^^^^^^^^^^^---
  | |                |
  | |                `Utc` 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:

Not sure... my main issue with this error is the message itself: "only traits defined in the current crate can be implemented for arbitrary types." The fact that it only mentioned "arbitrary types" leads me to believe that my only solution is to change something about the type, but I don't what that change Is supposed to be (In this case, it is using the "newtype" idiom.

The word arbitrary is vague to me - as a beginner, I don't fully know what that means. In fact, if I had to guess, I would consider both the original type, and the new type that wraps the old type, both "arbitrary," so I think the issue is around that word. I feel as if the message should not mention "arbitrary" and should rather indicate that you can only implement this trait on a type that's defined within this crate.

I do understand that the help diagnostic is nudging the user towards a newtype pattern, but my main point is that the error is pretty ambiguous to me.

@JosephTLyons JosephTLyons 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 Apr 19, 2022
@TaKO8Ki TaKO8Ki self-assigned this Apr 20, 2022
@compiler-errors
Copy link
Member

compiler-errors commented Apr 20, 2022

I think this can be solved super easily by taking "can be implemented for arbitrary types" and changing it based on the self type of the crate:

  • ADTs - "can be implemented for types defined outside of the crate"
  • builtins - "can be implemented for built-in types"
  • generics - (?), but maybe something like "can have blanket implementations defined in this trait"
  • other types - maybe fall back to this error message

Or something along these lines... The point is that we should specialize this message for the self type that the impl block has.

Feel free to r? me if you're working on this @TaKO8Ki, or otherwise this would be a pretty good E-easy + E-mentor issue that I would love to help someone with.

@TaKO8Ki
Copy link
Member

TaKO8Ki commented Apr 20, 2022

@compiler-errors
I'm going to work on this!

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

Successfully merging a pull request may close this issue.

3 participants