Skip to content

Extraneous comma in trait bound suggestion #72693

Closed
@jplatte

Description

@jplatte

The following code (Playground):

trait T1 {
    type A;
}

trait T2: T1
where
    Self::A: Clone,
{
}

fn generic<T: T2>()
where
    u8: Copy,
{
}

Produces an invalid suggestion:

error[E0277]: the trait bound `<T as T1>::A: std::clone::Clone` is not satisfied
  --> src/lib.rs:11:15
   |
5  | trait T2: T1
   |       -- required by a bound in this
6  | where
7  |     Self::A: Clone,
   |              ----- required by this bound in `T2`
...
11 | fn generic<T: T2>()
   |               ^^ the trait `std::clone::Clone` is not implemented for `<T as T1>::A`
   |
help: consider further restricting the associated type
   |
13 |     u8: Copy,, <T as T1>::A: std::clone::Clone
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

on nightly. On stable, the error message looks a bit different but seems to also be suggesting an extra comma.

When trying to reduce this test case I found that in other cases this same lint seems to suggest the right thing (this may be useful as an additional test case):

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

fn generic<T>() -> impl Clone
where
    Generic<T>: Default,
{
    Generic::<T>::default()
}

correct suggestion (nightly):

Standard Error

   Compiling playground v0.0.1 (/playground)

error[E0277]: the trait bound `T: std::clone::Clone` is not satisfied
 --> src/lib.rs:4:20
  |
4 | fn generic<T>() -> impl Clone
  |                    ^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `T`
...
8 |     Generic::<T>::default()
  |     ----------------------- this returned value is of type `Generic<T>`
  |
  = note: required because of the requirements on the impl of `std::clone::Clone` for `Generic<T>`
  = note: the return type of a function must have a statically known size
help: consider further restricting type parameter `T`
  |
6 |     Generic<T>: Default, T: std::clone::Clone
  |                        ^^^^^^^^^^^^^^^^^^^^^^

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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