granite-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 Propagate parallel_compiler feature through rustc crates. Turned off feature gives change of builded crates: 238 -> 224. 2022-03-28 08:41:12 +03:00
rustc_apfloat Addressed comments by @compiler-errors and @bjorn3 2022-03-30 17:04:46 -04:00
rustc_arena mark FIXMES for all the places found that are probably offset_from 2022-03-29 20:18:28 -04:00
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 Rollup merge of #94948 - jhpratt:rustc_deprecated, r=Dylan-DPC 2022-03-20 09:14:59 +01:00
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 Merge commit 'f2cdd4a78d89c009342197cf5844a21f8aa813df' into sync_cg_clif-2022-04-22 2022-04-22 21:11:38 +02:00
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 errors: lazily load fallback fluent bundle 2022-04-13 02:44:59 +01:00
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 Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_graphviz eplace usages of vec![].into_iter with [].into_iter 2022-01-09 14:09:25 +11:00
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 Rollup merge of #95434 - cjgillot:dump-dep-kind, r=oli-obk 2022-04-21 20:55:18 +02:00
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 Reduce max hash in raw strings from u16 to u8 2022-03-23 22:13:55 +01:00
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 Make rustc_log doc test runnable 2022-01-03 22:31:56 -08:00
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 replace dynamic library module with libloading 2021-12-06 12:03:47 -05:00
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 Remove unused macro rules 2022-04-18 23:28:06 +02:00
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 Auto merge of #95655 - kckeiks:create-hir-crate-items-query, r=cjgillot 2022-04-17 08:06:53 +00:00
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 Remove opaque type obligation and just register opaque types as they are encountered. 2022-03-28 16:57:45 +00:00
rustc_ty_utils Avoid accessing HIR from MIR queries. 2022-04-10 13:08:36 +02:00
rustc_type_ir Revert "Auto merge of #93893 - oli-obk:sad_revert, r=oli-obk" 2022-03-28 16:27:14 +00:00
rustc_typeck Auto merge of #96520 - lcnr:general-incoherent-impls, r=petrochenkov 2022-05-05 23:24:36 +00:00