Skip to content

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

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

Open
Aaron1011 opened this issue Apr 23, 2019 · 3 comments
Open

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

Aaron1011 opened this issue Apr 23, 2019 · 3 comments
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

Aaron1011 commented Apr 23, 2019

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.

@jonas-schievink jonas-schievink added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Apr 23, 2019
@jonas-schievink
Copy link
Contributor

This is probably intentional - the problem with type parameters that aren't used in field types is that it's not possible to infer their variance, so PhantomData can be used to act "as if" there's a field of a specific type incorporating the unused type parameter

@Centril Centril added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 23, 2019
@scottmcm
Copy link
Member

Note also that, in cases like this, it means that MyStruct { field } can't figure out which of possibly-many Bs you meant, so it's probably not what you want even if it were to work.

Usually this is handled by not bounding the struct, and just bounding methods on the struct.

@frewsxcv
Copy link
Member

the problem with type parameters that aren't used in field types is that it's not possible to infer their variance

I'm not familiar enough with compilers or programming languages to understand what it means to "infer variance", but I'm wondering if there's a more descriptive error message we can add here explaining why it's not possible. Here's an example where I hit this:

pub trait Coord {
    fn x(&self) -> f32;
    fn y(&self) -> f32;
}

pub trait Rect<C: Coord> {
    fn min(&self) -> C;
    fn max(&self) -> C;
}

pub trait Line<C: Coord> {
    fn start(&self) -> C;
    fn end(&self) -> C;
}

pub enum Geometry<C, R, L>
where C: Coord,
      R: Rect<C>,
      L: Line<C>
{
    Rect(R),
    Line(L),
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants