Skip to content

False positive 'type parameter is never used' with struct #60214

Open
@Aaron1011

Description

@Aaron1011

Consider the following code snippet playground

trait MyTrait<I> {}
trait SecondTrait {}

fn bar<A: MyTrait<B>, B: SecondTrait>() {}

struct MyStruct<A: MyTrait<B>, B: SecondTrait> {
    field: A
}

This procudes the error:

error[E0392]: parameter `B` is never used
 --> src/lib.rs:6:32
  |
6 | struct MyStruct<A: MyTrait<B>, B: SecondTrait> {
  |                                ^ unused type parameter
  |
  = help: consider removing `B` or using a marker such as `std::marker::PhantomData`

However, the parameter B is used - it's part of how we bound A.

The function and struct are equivalent in terms of generic parameters - since the equalivalent function compiles, the struct should as well.

Though this can be worked around via PhantomData, there's no reason to require using it. The struct definition is perfectly valid and useful, and should be useable without any workarounds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions