about summary refs log tree commit diff
path: root/tests/rustdoc-js-std/reference-shrink.js
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2022-05-06 20:05:37 +0200
committerGitHub <noreply@github.com>2022-05-06 20:05:37 +0200
commit66443a185261a04d7098bb465e2338334a3aa7a4 (patch)
tree732802c4fbba4b80da89f3fbe61bf129116b65a4 /tests/rustdoc-js-std/reference-shrink.js
parente209e85e39b4851c3ec122a45ddeabe318b2d522 (diff)
parent5b5ac28c64ce695c7cb85f365b385892d78f1be6 (diff)
downloadrust-66443a185261a04d7098bb465e2338334a3aa7a4.tar.gz
rust-66443a185261a04d7098bb465e2338334a3aa7a4.zip
Rollup merge of #96557 - nbdd0121:const, r=oli-obk
Allow inline consts to reference generic params

Tracking issue: #76001

The RFC says that inline consts cannot reference to generic parameters (for now), same as array length expressions. And expresses that it's desirable for it to reference in-scope generics, when array length expressions gain that feature as well.

However it is possible to implement this for inline consts before doing this for all anon consts, because inline consts are only used as values and they won't be used in the type system. So we can have:
```rust
fn foo<T>() {
    let x = [4i32; std::mem::size_of::<T>()];   // NOT ALLOWED (for now)
    let x = const { std::mem::size_of::<T>() }; // ALLOWED with this PR!
    let x = [4i32; const { std::mem::size_of::<T>() }];   // NOT ALLOWED (for now)
}
```

This would make inline consts super useful for compile-time checks and assertions:
```rust
fn assert_zst<T>() {
    const { assert!(std::mem::size_of::<T>() == 0) };
}
```

This would create an error during monomorphization when `assert_zst` is instantiated with non-ZST `T`s. A error during mono might sound scary, but this is exactly what a "desugared" inline const would do:
```rust
fn assert_zst<T>() {
    struct F<T>(T);
    impl<T> F<T> {
        const V: () = assert!(std::mem::size_of::<T>() == 0);
    }
    let _ = F::<T>::V;
}
```

It should also be noted that the current inline const implementation can already reference the type params via type inference, so this resolver-level restriction is not any useful either:
```rust
fn foo<T>() -> usize {
    let (_, size): (PhantomData<T>, usize) = const {
        const fn my_size_of<T>() -> (PhantomData<T>, usize) {
            (PhantomData, std::mem::size_of::<T>())
        }
        my_size_of()
    };
    size
}
```

```@rustbot``` label: F-inline_const
Diffstat (limited to 'tests/rustdoc-js-std/reference-shrink.js')
0 files changed, 0 insertions, 0 deletions