Skip to content
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

Using associated constants for generics is not considered using a type generic #80977

Closed
clarfonthey opened this issue Jan 13, 2021 · 0 comments · Fixed by #89829
Closed

Using associated constants for generics is not considered using a type generic #80977

clarfonthey opened this issue Jan 13, 2021 · 0 comments · Fixed by #89829
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]`

Comments

@clarfonthey
Copy link
Contributor

Filing in addition to #80976 because this seems to be a separate issue. Includes the same example below. Essentially, even though the associated constant To::DIM is being used in the definition of the struct, the compiler marks To as being unused since no types related to it are being used.

#![feature(const_generics, const_evaluatable_checked)]

trait Vector {
    type Scalar;
    const DIM: usize;
}

struct Transform<From, To>
where
    From: Vector,
    To: Vector<Scalar = From::Scalar>,
{
    columns: [[From::Scalar; From::DIM]; To::DIM],
}

(Playground)

@clarfonthey clarfonthey added the C-bug Category: This is a bug. label Jan 13, 2021
@BoxyUwU BoxyUwU added A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Oct 13, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 23, 2021
Consider types appearing in const expressions to be invariant

This is an approach to fix rust-lang#80977.
Currently, a type parameter which is only used in a constant expression is considered bivariant and will trigger error E0392 *"parameter T is never used"*.
Here is a short example:

```rust
pub trait Foo {
    const N: usize;
}

struct Bar<T: Foo>([u8; T::N])
where [(); T::N]:;
```
([playgound](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2015&gist=b51a272853f75925e72efc1597478aa5))

While it is possible to silence this error by adding a `PhantomData<T>` field, I think the better solution would be to make `T` invariant.
This would be analogous to the invariance constraints added for associated types.
However, I'm quite new to the compiler and unsure whether this is the right approach.

r? `@varkor` (since you authored rust-lang#60058)
@bors bors closed this as completed in 2b874f0 Oct 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants