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

Make const parameters enforce no variance constraints #60058

Merged
merged 4 commits into from
Apr 18, 2019

Conversation

varkor
Copy link
Member

@varkor varkor commented Apr 17, 2019

Fixes #60047. Also includes some minor const refactoring for convenience.

@rust-highfive
Copy link
Collaborator

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 17, 2019
@cramertj
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Apr 18, 2019

📌 Commit 318a10e has been approved by cramertj

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2019
@bors
Copy link
Contributor

bors commented Apr 18, 2019

⌛ Testing commit 318a10e with merge 9622c488405c6688f3868b24967a4cdd0394666a...

@bors
Copy link
Contributor

bors commented Apr 18, 2019

💔 Test failed - checks-travis

@rust-highfive
Copy link
Collaborator

The job arm-android of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[01:44:45] test vec::test_split_at_mut ... ok
[01:44:45] test vec::test_splice_unbounded ... ok
[01:44:45] test vec::test_split_off ... ok
[01:44:45] test vec::test_swap_remove_empty ... ok
[01:44:45] died due to signal 11
[01:44:45] 
[01:44:45] 
[01:44:45] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "test" "--target" "arm-linux-androideabi" "-j" "4" "--release" "--locked" "--color" "always" "--features" "panic-unwind backtrace" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "-p" "alloc" "--"
[01:44:45] expected success, got: exit code: 3
---
travis_time:end:12916ca0:start=1555556759258130925,finish=1555556759270541288,duration=12410363
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:1a7b6a66
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:008de5f4
travis_time:start:008de5f4
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:17ed89e4
$ dmesg | grep -i kill

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 @TimNN. (Feature Requests)

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 18, 2019
@varkor
Copy link
Member Author

varkor commented Apr 18, 2019

@bors retry

(Failure looks spurious.)

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2019
bors added a commit that referenced this pull request Apr 18, 2019
Make const parameters enforce no variance constraints

Fixes #60047. Also includes some minor const refactoring for convenience.
@bors
Copy link
Contributor

bors commented Apr 18, 2019

⌛ Testing commit 318a10e with merge e577e49...

@bors
Copy link
Contributor

bors commented Apr 18, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: cramertj
Pushing e577e49 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 18, 2019
@bors bors merged commit 318a10e into rust-lang:master Apr 18, 2019
@rust-highfive
Copy link
Collaborator

📣 Toolstate changed by #60058!

Tested on commit e577e49.
Direct link to PR: #60058

🎉 rls on windows: test-fail → test-pass (cc @Xanewok, @rust-lang/infra).

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Apr 18, 2019
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).
@varkor varkor deleted the const-generics-ty-refactor branch April 18, 2019 12:41
// Const parameters are always invariant.
for (idx, param) in generics.params.iter().enumerate() {
if let ty::GenericParamDefKind::Const = param.kind {
variances[idx] = ty::Invariant;
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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!

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request 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)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Permit unused generic const parameters in type definitions
5 participants