Skip to content

Confusing diagnostic when attempting to implementing trait for tuple #67535

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
varkor opened this issue Dec 22, 2019 · 4 comments
Closed

Confusing diagnostic when attempting to implementing trait for tuple #67535

varkor opened this issue Dec 22, 2019 · 4 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@varkor
Copy link
Member

varkor commented Dec 22, 2019

impl std::ops::AddAssign for () {
    fn add_assign(&self, other: ()) -> () {
        ()
    }
}

produces:

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> src/lib.rs:12:1
   |
12 | impl std::ops::AddAssign for () {
   | ^^^^^-------------------^^^^^--
   | |    |                       |
   | |    |                       this is not defined in the current crate because tuples are always foreign
   | |    this is not defined in the current crate because tuples are always foreign
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

Notice how it highlights std::ops::AddAssign and says this is not defined in the current crate because tuples are always foreign, despite AddAssign have nothing to do with tuples.

@varkor varkor 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. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Dec 22, 2019
@jonas-schievink jonas-schievink added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Dec 22, 2019
@estebank
Copy link
Contributor

estebank commented Dec 22, 2019

The change needs to be made somewhere in the logic of

let mut err = struct_span_err!(
self.tcx.sess,
sp,
E0117,
"only traits defined in the current crate can be implemented for \
arbitrary types"
);
err.span_label(sp, "impl doesn't use only types from inside the current crate");
for (ty, is_target_ty) in &tys {
let mut ty = *ty;
self.tcx.infer_ctxt().enter(|infcx| {
// Remove the lifetimes unnecessary for this error.
ty = infcx.freshen(ty);
});
ty = match ty.kind {
// Remove the type arguments from the output, as they are not relevant.
// You can think of this as the reverse of `resolve_vars_if_possible`.
// That way if we had `Vec<MyType>`, we will properly attribute the
// problem to `Vec<T>` and avoid confusing the user if they were to see
// `MyType` in the error.
ty::Adt(def, _) => self.tcx.mk_adt(def, ty::List::empty()),
_ => ty,
};
let this = "this".to_string();
let (ty, postfix) = match &ty.kind {
ty::Slice(_) => (this, " because slices are always foreign"),
ty::Array(..) => (this, " because arrays are always foreign"),
ty::Tuple(..) => (this, " because tuples are always foreign"),
_ => (format!("`{}`", ty), ""),
};
let msg = format!("{} is not defined in the current crate{}", ty, postfix);
if *is_target_ty {
// Point at `D<A>` in `impl<A, B> for C<B> in D<A>`
err.span_label(impl_ty.span, &msg);
} else {
// Point at `C<B>` in `impl<A, B> for C<B> in D<A>`
err.span_label(tr.path.span, &msg);
}
}
err.note("define and implement a trait or new type instead");
err.emit();
return;

The output should have been

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> src/lib.rs:12:1
   |
12 | impl std::ops::AddAssign for () {
   | ^^^^^-------------------^^^^^--
   | |    |                       |
   | |    |                       this is not defined in the current crate because tuples are always foreign
   | |    `std::ops::AddAssign` 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

@estebank
Copy link
Contributor

I see why this happens: AddAssign has an Rhs type argument with a default type Self (AddAssign<Rhs = Self>) which means that what was written gets desugared internally to impl std::ops::AddAssign<Rhs=()> for () { /* ... */ }, which would have made more sense:

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> src/lib.rs:12:1
   |
12 | impl std::ops::AddAssign<Rhs=()> for () {
   | ^^^^^---------------------------^^^^^--
   | |    |                               |
   | |    |                               this is not defined in the current crate because tuples are always foreign
   | |    this is not defined in the current crate because tuples are always foreign
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

@estebank estebank added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Dec 29, 2022
@TroyNeubauer
Copy link
Contributor

Would like to work on this. Excited for my first issue!
@rustbot claim

@estebank
Copy link
Contributor

@TroyNeubauer you'll have to check whether the trait itself is foreign, and if so avoid adding the "because tuples are always foreign" and instead say "this is not defined in the current crate because this is a foreign trait".

TroyNeubauer added a commit to TroyNeubauer/rust that referenced this issue Jan 2, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 2, 2023
…error, r=estebank

Implement fix for rust-lang#67535

Implements a fix for rust-lang#67535
r? `@estebank`
@estebank estebank closed this as completed Jan 2, 2023
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. 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