Skip to content

The compiler should be more helpful when trait bounds are not met. #64417

New issue

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

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

Already on GitHub? # to your account

Closed
glandium opened this issue Sep 12, 2019 · 2 comments · Fixed by #69255
Closed

The compiler should be more helpful when trait bounds are not met. #64417

glandium opened this issue Sep 12, 2019 · 2 comments · Fixed by #69255
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

Here's a reduced testcase:

use std::sync::Arc;

struct Foo;

#[derive(Clone)]
struct Bar<T>(Arc<T>);

fn main() {
    let a = Bar(Arc::new(Foo));
    let b = a.clone();
}

This fails to compile with:

error[E0599]: no method named `clone` found for type `Bar<Foo>` in the current scope
  --> src/main.rs:10:15
   |
6  | struct Bar<T>(Arc<T>);
   | ---------------------- method `clone` not found for this
...
10 |     let b = a.clone();
   |               ^^^^^
   |
   = note: the method `clone` exists but the following trait bounds were not satisfied:
           `Bar<Foo> : std::clone::Clone`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `clone`, perhaps you need to implement it:
           candidate #1: `std::clone::Clone`

The way #[derive] works (#26925), and with 20/20 hindsight, it's obvious what's wrong. But even when you know about #26925 it takes a mental effort to go from that error message to "oh, right, #[derive(Clone)] adds T: Clone".

It would be useful if the compiler went further, and, considering there is an impl Clone for Bar<T>, detailed why that impl doesn't apply to Foo.

@glandium glandium changed the title The compiler should be more helpful when trait bounds are not met because of #26925 The compiler should be more helpful when trait bounds are not met. Sep 12, 2019
@glandium
Copy link
Contributor Author

Come to think of it, it's more general than #26925. One could hit a similar error with custom impls of any trait and the information why the generic impl doesn't match would be valuable in those cases too.

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 13, 2019
@d-e-s-o
Copy link
Contributor

d-e-s-o commented Oct 11, 2019

I'd honestly go one step further and consider implementing Clone without the requirement T: Clone iff there is no need, e.g., if there is an inner type wrapping the T that does not require T: Clone on its implementation of Clone, as is the case for Arc:

impl<T> Clone for Arc<T>
where
    T: ?Sized,

(not sure how feasible that is [or whether I missed something in my reasoning], but it may be something worth shooting for)

ryzhyk pushed a commit to vmware-archive/differential-datalog that referenced this issue Oct 22, 2019
When using an auto derive to implement a trait such as Clone on a
generic object, the code emitted will contain a constraint like: T:
Clone.
That is a problem for our SharedObserver, because our inner Observer
does not implement Clone, and that is fine and desired. In order to work
around this problem (known upstream as [issue 64417][]) we need to
implement Clone for SharedObserver manually.

[issue 64417]: rust-lang/rust#64417
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Jan 8, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 27, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 28, 2020
@bors bors closed this as completed in 55aee8d Feb 29, 2020
@fmease fmease added A-trait-system Area: Trait system and removed A-trait-system Area: Trait system labels Dec 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

5 participants