-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Make const parameters enforce no variance constraints #60058
Conversation
r? @cramertj (rust_highfive has picked a reviewer for you, use r? to override) |
@bors r+ |
📌 Commit 318a10e has been approved by |
⌛ Testing commit 318a10e with merge 9622c488405c6688f3868b24967a4cdd0394666a... |
💔 Test failed - checks-travis |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors retry (Failure looks spurious.) |
Make const parameters enforce no variance constraints Fixes #60047. Also includes some minor const refactoring for convenience.
☀️ Test successful - checks-travis, status-appveyor |
Tested on commit rust-lang/rust@e577e49. Direct link to PR: <rust-lang/rust#60058> 🎉 rls on windows: test-fail → test-pass (cc @Xanewok, @rust-lang/infra).
// Const parameters are always invariant. | ||
for (idx, param) in generics.params.iter().enumerate() { | ||
if let ty::GenericParamDefKind::Const = param.kind { | ||
variances[idx] = ty::Invariant; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be variances[generics.parent_count + idx] = ty::Invariant
? Since count
also include parent generics while params
do not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you'd also want to change the variance of parent generics here. Maybe this code should be elsewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're absolutely right. Thanks for spotting that!
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)
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)
Fixes #60047. Also includes some minor const refactoring for convenience.