You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
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
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
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.
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:
Consider the following code snippet playground
This procudes the error:
However, the parameter
B
is used - it's part of how we boundA
.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.The text was updated successfully, but these errors were encountered: