Skip to content

Commit 1ca0e5f

Browse files
Rollup merge of rust-lang#104087 - nbdd0121:const, r=scottmcm
Stabilise inline_const # Stabilisation Report ## Summary This PR will stabilise `inline_const` feature in expression position. `inline_const_pat` is still unstable and will *not* be stabilised. The feature will allow code like this: ```rust foo(const { 1 + 1 }) ``` which is roughly desugared into ```rust struct Foo; impl Foo { const FOO: i32 = 1 + 1; } foo(Foo::FOO) ``` This feature is from rust-lang/rfcs#2920 and is tracked in rust-lang#76001 (the tracking issue should *not* be closed as it needs to track inline const in pattern position). The initial implementation is done in rust-lang#77124. ## Difference from RFC There are two major differences (enhancements) as implemented from the RFC. First thing is that the RFC says that the type of an inline const block inferred from the content *within* it, but we currently can infer the type using the information from outside the const block as well. This is a frequently requested feature to the initial implementation (e.g. rust-lang#89964). The inference is implemented in rust-lang#89561 and is done by treating inline const similar to a closure and therefore share inference context with its parent body. This allows code like: ```rust let v: Vec<i32> = const { Vec::new() }; ``` Another enhancement that differs from the RFC is that we currently allow inline consts to reference generic parameters. This is implemented in rust-lang#96557. This allows code like: ```rust fn create_none_array<T, const N: usize>() -> [Option<T>; N] { [const { None::<T> }; N] } ``` This enhancement also makes inline const usable as static asserts: ```rust fn require_zst<T>() { const { assert!(std::mem::size_of::<T>() == 0) } } ``` ## Documentation Reference: rust-lang/reference#1295 ## Unresolved issues We still have a few issues that are not resolved, but I don't think it necessarily has to block stabilisation: * expr fragment specifier issue: rust-lang#86730 * ~~`const {}` behaves similar to `async {}` but not to `{}` and `unsafe {}` (they are treated as `ExpressionWithoutBlock` rather than `ExpressionWithBlock`): https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/const.20blocks.20differ.20from.20normal.20and.20from.20unsafe.20blocks/near/290229453~~ ## Tests There are a few tests in https://github.com/rust-lang/rust/tree/master/src/test/ui/inline-const
2 parents 290d792 + 6dcf0cd commit 1ca0e5f

File tree

95 files changed

+206
-319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+206
-319
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
556556
half_open_range_patterns_in_slices,
557557
"half-open range patterns in slices are unstable"
558558
);
559-
gate_all!(inline_const, "inline-const is experimental");
560559
gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
561560
gate_all!(associated_const_equality, "associated const equality is incomplete");
562561
gate_all!(yeet_expr, "`do yeet` expression is experimental");

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ declare_features! (
211211
(accepted, inclusive_range_syntax, "1.26.0", Some(28237)),
212212
/// Allows inferring outlives requirements (RFC 2093).
213213
(accepted, infer_outlives_requirements, "1.30.0", Some(44493)),
214+
/// Allow anonymous constants from an inline `const` block
215+
(accepted, inline_const, "CURRENT_RUSTC_VERSION", Some(76001)),
214216
/// Allows irrefutable patterns in `if let` and `while let` statements (RFC 2086).
215217
(accepted, irrefutable_let_patterns, "1.33.0", Some(44495)),
216218
/// Allows `#[instruction_set(_)]` attribute.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,6 @@ declare_features! (
501501
(unstable, impl_trait_in_fn_trait_return, "1.64.0", Some(99697)),
502502
/// Allows associated types in inherent impls.
503503
(incomplete, inherent_associated_types, "1.52.0", Some(8995)),
504-
/// Allow anonymous constants from an inline `const` block
505-
(unstable, inline_const, "1.49.0", Some(76001)),
506504
/// Allow anonymous constants from an inline `const` block in pattern position
507505
(unstable, inline_const_pat, "1.58.0", Some(76001)),
508506
/// Allows using `pointer` and `reference` in intra-doc links

compiler/rustc_middle/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#![feature(coroutines)]
3838
#![feature(generic_nonzero)]
3939
#![feature(if_let_guard)]
40-
#![feature(inline_const)]
40+
#![cfg_attr(bootstrap, feature(inline_const))]
4141
#![feature(iter_from_coroutine)]
4242
#![feature(negative_impls)]
4343
#![feature(never_type)]

compiler/rustc_mir_transform/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![feature(cow_is_borrowed)]
55
#![feature(decl_macro)]
66
#![feature(impl_trait_in_assoc_type)]
7-
#![feature(inline_const)]
7+
#![cfg_attr(bootstrap, feature(inline_const))]
88
#![feature(is_sorted)]
99
#![feature(let_chains)]
1010
#![feature(map_try_insert)]

compiler/rustc_parse/src/parser/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1254,8 +1254,6 @@ impl<'a> Parser<'a> {
12541254
fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, P<Expr>> {
12551255
if pat {
12561256
self.psess.gated_spans.gate(sym::inline_const_pat, span);
1257-
} else {
1258-
self.psess.gated_spans.gate(sym::inline_const, span);
12591257
}
12601258
self.eat_keyword(kw::Const);
12611259
let (attrs, blk) = self.parse_inner_attrs_and_block()?;

compiler/rustc_serialize/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#![feature(const_option)]
1313
#![feature(core_intrinsics)]
1414
#![feature(generic_nonzero)]
15-
#![feature(inline_const)]
15+
#![cfg_attr(bootstrap, feature(inline_const))]
1616
#![feature(min_specialization)]
1717
#![feature(never_type)]
1818
#![feature(ptr_sub_ptr)]

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
#![feature(generic_nonzero)]
130130
#![feature(hasher_prefixfree_extras)]
131131
#![feature(hint_assert_unchecked)]
132-
#![feature(inline_const)]
133132
#![feature(inplace_iteration)]
134133
#![feature(iter_advance_by)]
135134
#![feature(iter_next_chunk)]
@@ -170,6 +169,7 @@
170169
// Language features:
171170
// tidy-alphabetical-start
172171
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
172+
#![cfg_attr(bootstrap, feature(inline_const))]
173173
#![cfg_attr(not(bootstrap), rustc_preserve_ub_checks)]
174174
#![cfg_attr(not(test), feature(coroutine_trait))]
175175
#![cfg_attr(test, feature(panic_update_hook))]

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
//
202202
// Language features:
203203
// tidy-alphabetical-start
204+
#![cfg_attr(bootstrap, feature(inline_const))]
204205
#![feature(abi_unadjusted)]
205206
#![feature(adt_const_params)]
206207
#![feature(allow_internal_unsafe)]
@@ -231,7 +232,6 @@
231232
#![feature(fundamental)]
232233
#![feature(generic_arg_infer)]
233234
#![feature(if_let_guard)]
234-
#![feature(inline_const)]
235235
#![feature(intra_doc_pointers)]
236236
#![feature(intrinsics)]
237237
#![feature(lang_items)]

library/core/src/sync/atomic.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ impl AtomicBool {
511511
/// # Examples
512512
///
513513
/// ```
514-
/// #![feature(atomic_from_mut, inline_const)]
514+
/// #![feature(atomic_from_mut)]
515+
/// # #![cfg_attr(bootstrap, feature(inline_const))]
515516
/// use std::sync::atomic::{AtomicBool, Ordering};
516517
///
517518
/// let mut some_bools = [const { AtomicBool::new(false) }; 10];
@@ -1313,7 +1314,8 @@ impl<T> AtomicPtr<T> {
13131314
/// # Examples
13141315
///
13151316
/// ```
1316-
/// #![feature(atomic_from_mut, inline_const)]
1317+
/// #![feature(atomic_from_mut)]
1318+
/// # #![cfg_attr(bootstrap, feature(inline_const))]
13171319
/// use std::ptr::null_mut;
13181320
/// use std::sync::atomic::{AtomicPtr, Ordering};
13191321
///
@@ -2303,7 +2305,8 @@ macro_rules! atomic_int {
23032305
/// # Examples
23042306
///
23052307
/// ```
2306-
/// #![feature(atomic_from_mut, inline_const)]
2308+
/// #![feature(atomic_from_mut)]
2309+
/// # #![cfg_attr(bootstrap, feature(inline_const))]
23072310
#[doc = concat!($extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};")]
23082311
///
23092312
#[doc = concat!("let mut some_ints = [const { ", stringify!($atomic_type), "::new(0) }; 10];")]

library/core/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
#![feature(hasher_prefixfree_extras)]
4848
#![feature(hashmap_internals)]
4949
#![feature(try_find)]
50-
#![feature(inline_const)]
50+
#![cfg_attr(bootstrap, feature(inline_const))]
5151
#![feature(is_sorted)]
5252
#![feature(layout_for_ptr)]
5353
#![feature(pattern)]

library/portable-simd/crates/core_simd/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
convert_float_to_int,
88
core_intrinsics,
99
decl_macro,
10-
inline_const,
1110
intra_doc_pointers,
1211
repr_simd,
1312
simd_ffi,

src/doc/unstable-book/src/language-features/inline-const-pat.md

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
The tracking issue for this feature is: [#76001]
44

5-
See also [`inline_const`](inline-const.md)
6-
75
------
86

97
This feature allows you to use inline constant expressions in pattern position:

src/doc/unstable-book/src/language-features/inline-const.md

-32
This file was deleted.

src/tools/clippy/tests/ui-toml/suppress_lint_in_const/test.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(inline_const)]
21
#![warn(clippy::indexing_slicing)]
32
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
43
// we want to avoid false positives.

src/tools/clippy/tests/ui-toml/suppress_lint_in_const/test.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: indexing may panic
2-
--> tests/ui-toml/suppress_lint_in_const/test.rs:27:5
2+
--> tests/ui-toml/suppress_lint_in_const/test.rs:26:5
33
|
44
LL | x[index];
55
| ^^^^^^^^
@@ -9,39 +9,39 @@ LL | x[index];
99
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
1010

1111
error: indexing may panic
12-
--> tests/ui-toml/suppress_lint_in_const/test.rs:42:5
12+
--> tests/ui-toml/suppress_lint_in_const/test.rs:41:5
1313
|
1414
LL | v[0];
1515
| ^^^^
1616
|
1717
= help: consider using `.get(n)` or `.get_mut(n)` instead
1818

1919
error: indexing may panic
20-
--> tests/ui-toml/suppress_lint_in_const/test.rs:43:5
20+
--> tests/ui-toml/suppress_lint_in_const/test.rs:42:5
2121
|
2222
LL | v[10];
2323
| ^^^^^
2424
|
2525
= help: consider using `.get(n)` or `.get_mut(n)` instead
2626

2727
error: indexing may panic
28-
--> tests/ui-toml/suppress_lint_in_const/test.rs:44:5
28+
--> tests/ui-toml/suppress_lint_in_const/test.rs:43:5
2929
|
3030
LL | v[1 << 3];
3131
| ^^^^^^^^^
3232
|
3333
= help: consider using `.get(n)` or `.get_mut(n)` instead
3434

3535
error: indexing may panic
36-
--> tests/ui-toml/suppress_lint_in_const/test.rs:50:5
36+
--> tests/ui-toml/suppress_lint_in_const/test.rs:49:5
3737
|
3838
LL | v[N];
3939
| ^^^^
4040
|
4141
= help: consider using `.get(n)` or `.get_mut(n)` instead
4242

4343
error: indexing may panic
44-
--> tests/ui-toml/suppress_lint_in_const/test.rs:51:5
44+
--> tests/ui-toml/suppress_lint_in_const/test.rs:50:5
4545
|
4646
LL | v[M];
4747
| ^^^^

src/tools/clippy/tests/ui/arithmetic_side_effects.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
arithmetic_overflow,
1111
unconditional_panic
1212
)]
13-
#![feature(const_mut_refs, inline_const)]
13+
#![feature(const_mut_refs)]
1414
#![warn(clippy::arithmetic_side_effects)]
1515

1616
extern crate proc_macro_derive;

src/tools/clippy/tests/ui/bool_to_int_with_if.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(let_chains, inline_const)]
1+
#![feature(let_chains)]
22
#![warn(clippy::bool_to_int_with_if)]
33
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
44

src/tools/clippy/tests/ui/bool_to_int_with_if.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(let_chains, inline_const)]
1+
#![feature(let_chains)]
22
#![warn(clippy::bool_to_int_with_if)]
33
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
44

src/tools/clippy/tests/ui/const_is_empty.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(inline_const)]
21
#![warn(clippy::const_is_empty)]
32
#![allow(clippy::needless_late_init, unused_must_use)]
43

0 commit comments

Comments
 (0)