rust/compiler
Guillaume Gomez 66443a1852
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
2022-05-06 20:05:37 +02:00
..
rustc
rustc_apfloat
rustc_arena
rustc_ast Rollup merge of #96682 - nnethercote:show-invisible-delims, r=petrochenkov 2022-05-05 15:43:05 +02:00
rustc_ast_lowering Auto merge of #96546 - nnethercote:overhaul-MacArgs, r=petrochenkov 2022-05-04 21:16:28 +00:00
rustc_ast_passes Feature-gate do yeet inside cfgs too 2022-04-30 18:10:01 -07:00
rustc_ast_pretty Rollup merge of #96682 - nnethercote:show-invisible-delims, r=petrochenkov 2022-05-05 15:43:05 +02:00
rustc_attr
rustc_borrowck Auto merge of #96268 - jackh726:remove-mutable_borrow_reservation_conflict-lint, r=nikomatsakis 2022-05-06 12:32:44 +00:00
rustc_builtin_macros Auto merge of #91779 - ridwanabdillahi:natvis, r=michaelwoerister 2022-05-05 12:26:38 +00:00
rustc_codegen_cranelift
rustc_codegen_gcc Merge new_metadata into codegen_allocator 2022-04-30 21:20:08 +02:00
rustc_codegen_llvm Stabilize bool::then_some 2022-05-04 13:22:08 +02:00
rustc_codegen_ssa Auto merge of #91779 - ridwanabdillahi:natvis, r=michaelwoerister 2022-05-05 12:26:38 +00:00
rustc_const_eval Rollup merge of #96714 - RalfJung:scalar-pair-debug, r=oli-obk 2022-05-05 15:43:07 +02:00
rustc_data_structures Auto merge of #94598 - scottmcm:prefix-free-hasher-methods, r=Amanieu 2022-05-06 09:43:57 +00:00
rustc_driver
rustc_error_codes Remove ``` `ignore``` from E0705 test 2022-04-30 10:48:38 -05:00
rustc_error_messages macros: subdiagnostic derive 2022-04-29 02:05:20 +01:00
rustc_errors macros: subdiagnostic derive 2022-04-29 02:05:20 +01:00
rustc_expand Auto merge of #91779 - ridwanabdillahi:natvis, r=michaelwoerister 2022-05-05 12:26:38 +00:00
rustc_feature Auto merge of #96520 - lcnr:general-incoherent-impls, r=petrochenkov 2022-05-05 23:24:36 +00:00
rustc_fs_util
rustc_graphviz
rustc_hir generalize "incoherent impls" impl for custom types 2022-05-05 10:53:00 +02:00
rustc_hir_pretty Store all generic bounds as where predicates. 2022-04-30 13:55:13 +02:00
rustc_incremental
rustc_index Add element iterator for ChunkedBitSet 2022-04-30 16:40:49 +02:00
rustc_infer Stabilize bool::then_some 2022-05-04 13:22:08 +02:00
rustc_interface Stabilize bool::then_some 2022-05-04 13:22:08 +02:00
rustc_lexer
rustc_lint Auto merge of #96268 - jackh726:remove-mutable_borrow_reservation_conflict-lint, r=nikomatsakis 2022-05-06 12:32:44 +00:00
rustc_lint_defs Auto merge of #96268 - jackh726:remove-mutable_borrow_reservation_conflict-lint, r=nikomatsakis 2022-05-06 12:32:44 +00:00
rustc_llvm RustWrapper: explicitly don't handle DXILPointerTyID 2022-04-28 13:53:52 -04:00
rustc_log
rustc_macros macros: add interop between diagnostic derives 2022-04-29 02:12:10 +01:00
rustc_metadata Add support for a new attribute #[debugger_visualizer] to support embedding debugger visualizers into a generated PDB. 2022-05-03 10:53:54 -07:00
rustc_middle Rollup merge of #96557 - nbdd0121:const, r=oli-obk 2022-05-06 20:05:37 +02:00
rustc_mir_build Rollup merge of #96628 - joshtriplett:stabilize-then-some, r=m-ou-se 2022-05-05 10:20:35 +09:00
rustc_mir_dataflow Stabilize bool::then_some 2022-05-04 13:22:08 +02:00
rustc_mir_transform Rollup merge of #96714 - RalfJung:scalar-pair-debug, r=oli-obk 2022-05-05 15:43:07 +02:00
rustc_monomorphize Stabilize bool::then_some 2022-05-04 13:22:08 +02:00
rustc_parse Auto merge of #96546 - nnethercote:overhaul-MacArgs, r=petrochenkov 2022-05-04 21:16:28 +00:00
rustc_parse_format Make rustc_parse_format compile on stable 2022-05-03 11:26:58 +02:00
rustc_passes Auto merge of #96520 - lcnr:general-incoherent-impls, r=petrochenkov 2022-05-05 23:24:36 +00:00
rustc_plugin_impl
rustc_privacy rustc: Panic by default in DefIdTree::parent 2022-05-02 01:56:50 +03:00
rustc_query_impl Enable tracing for all queryies 2022-05-04 16:15:26 +00:00
rustc_query_system Rollup merge of #96697 - oli-obk:trace_queries, r=michaelwoerister 2022-05-05 10:20:38 +09:00
rustc_resolve Rollup merge of #96557 - nbdd0121:const, r=oli-obk 2022-05-06 20:05:37 +02:00
rustc_save_analysis rustc: Panic by default in DefIdTree::parent 2022-05-02 01:56:50 +03:00
rustc_serialize
rustc_session Rollup merge of #96433 - petrochenkov:delim, r=nnethercote 2022-04-28 20:13:02 +02:00
rustc_span Rollup merge of #96747 - JohnTitor:expect-local-track-caller, r=compiler-errors 2022-05-05 19:34:27 -07:00
rustc_symbol_mangling
rustc_target Stabilize bool::then_some 2022-05-04 13:22:08 +02:00
rustc_trait_selection Auto merge of #96720 - JohnTitor:rollup-9jaaekr, r=JohnTitor 2022-05-05 02:49:16 +00:00
rustc_traits
rustc_ty_utils
rustc_type_ir
rustc_typeck Auto merge of #96520 - lcnr:general-incoherent-impls, r=petrochenkov 2022-05-05 23:24:36 +00:00