Skip to content

Commit ccc53bb

Browse files
authored
Rollup merge of rust-lang#91140 - nbdd0121:const_typeck, r=oli-obk
Split inline const to two feature gates and mark expression position inline const complete This PR splits inline const in pattern position into its own `#![feature(inline_const_pat)]` feature gate, and make the usage in expression position complete. I think I have resolved most outstanding issues related to `inline_const` with rust-lang#89561 and other PRs. The only thing left that I am aware of is rust-lang#90150 and the lack of lifetime checks when inline const is used in pattern position (FIXME in rust-lang#89561). Implementation-wise when used in pattern position it has to be lowered during MIR building while in expression position it's evaluated only when monomorphizing (just like normal consts), so it makes some sense to separate it into two feature gates so one can progress without being blocked by another. `@rustbot` label: T-compiler F-inline_const
2 parents 39542a0 + 6f38568 commit ccc53bb

32 files changed

+72
-43
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
719719
gate_all!(const_trait_impl, "const trait impls are experimental");
720720
gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
721721
gate_all!(inline_const, "inline-const is experimental");
722+
gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
722723
gate_all!(
723724
const_generics_defaults,
724725
"default values for const generic parameters are experimental"

compiler/rustc_feature/src/active.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ declare_features! (
409409
/// Allows associated types in inherent impls.
410410
(incomplete, inherent_associated_types, "1.52.0", Some(8995), None),
411411
/// Allow anonymous constants from an inline `const` block
412-
(incomplete, inline_const, "1.49.0", Some(76001), None),
412+
(active, inline_const, "1.49.0", Some(76001), None),
413+
/// Allow anonymous constants from an inline `const` block in pattern position
414+
(incomplete, inline_const_pat, "1.58.0", Some(76001), None),
413415
/// Allows using `pointer` and `reference` in intra-doc links
414416
(active, intra_doc_pointers, "1.51.0", Some(80896), None),
415417
/// Allows `#[instruction_set(_)]` attribute

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ impl<'a> Parser<'a> {
12431243
} else if self.eat_keyword(kw::Unsafe) {
12441244
self.parse_block_expr(None, lo, BlockCheckMode::Unsafe(ast::UserProvided), attrs)
12451245
} else if self.check_inline_const(0) {
1246-
self.parse_const_block(lo.to(self.token.span))
1246+
self.parse_const_block(lo.to(self.token.span), false)
12471247
} else if self.is_do_catch_block() {
12481248
self.recover_do_catch(attrs)
12491249
} else if self.is_try_block() {

compiler/rustc_parse/src/parser/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,12 @@ impl<'a> Parser<'a> {
10951095
}
10961096

10971097
/// Parses inline const expressions.
1098-
fn parse_const_block(&mut self, span: Span) -> PResult<'a, P<Expr>> {
1099-
self.sess.gated_spans.gate(sym::inline_const, span);
1098+
fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, P<Expr>> {
1099+
if pat {
1100+
self.sess.gated_spans.gate(sym::inline_const_pat, span);
1101+
} else {
1102+
self.sess.gated_spans.gate(sym::inline_const, span);
1103+
}
11001104
self.eat_keyword(kw::Const);
11011105
let blk = self.parse_block()?;
11021106
let anon_const = AnonConst {

compiler/rustc_parse/src/parser/pat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'a> Parser<'a> {
437437
PatKind::Box(pat)
438438
} else if self.check_inline_const(0) {
439439
// Parse `const pat`
440-
let const_expr = self.parse_const_block(lo.to(self.token.span))?;
440+
let const_expr = self.parse_const_block(lo.to(self.token.span), true)?;
441441

442442
if let Some(re) = self.parse_range_end() {
443443
self.parse_pat_range_begin_with(const_expr, re)?
@@ -884,7 +884,7 @@ impl<'a> Parser<'a> {
884884

885885
fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
886886
if self.check_inline_const(0) {
887-
self.parse_const_block(self.token.span)
887+
self.parse_const_block(self.token.span, true)
888888
} else if self.check_path() {
889889
let lo = self.token.span;
890890
let (qself, path) = if self.eat_lt() {

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ symbols! {
731731
inlateout,
732732
inline,
733733
inline_const,
734+
inline_const_pat,
734735
inout,
735736
instruction_set,
736737
intel,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# `inline_const_pat`
2+
3+
The tracking issue for this feature is: [#76001]
4+
5+
See also [`inline_const`](inline-const.md)
6+
7+
------
8+
9+
This feature allows you to use inline constant expressions in pattern position:
10+
11+
```rust
12+
#![feature(inline_const_pat)]
13+
14+
const fn one() -> i32 { 1 }
15+
16+
let some_int = 3;
17+
match some_int {
18+
const { 1 + 2 } => println!("Matched 1 + 2"),
19+
const { one() } => println!("Matched const fn returning 1"),
20+
_ => println!("Didn't match anything :("),
21+
}
22+
```
23+
24+
[#76001]: https://github.com/rust-lang/rust/issues/76001

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

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

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

5+
See also [`inline_const_pat`](inline-const-pat.md)
6+
57
------
68

79
This feature allows you to use inline constant expressions. For example, you can
@@ -27,19 +29,4 @@ fn main() {
2729
}
2830
```
2931

30-
You can also use inline constant expressions in patterns:
31-
32-
```rust
33-
#![feature(inline_const)]
34-
35-
const fn one() -> i32 { 1 }
36-
37-
let some_int = 3;
38-
match some_int {
39-
const { 1 + 2 } => println!("Matched 1 + 2"),
40-
const { one() } => println!("Matched const fn returning 1"),
41-
_ => println!("Didn't match anything :("),
42-
}
43-
```
44-
4532
[#76001]: https://github.com/rust-lang/rust/issues/76001

src/test/ui/consts/closure-structural-match-issue-90013.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Regression test for issue 90013.
22
// check-pass
3-
#![allow(incomplete_features)]
43
#![feature(inline_const)]
54

65
fn main() {

src/test/ui/consts/const-blocks/fn-call-in-const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22

33
#![feature(inline_const)]
4-
#![allow(unused, incomplete_features)]
4+
#![allow(unused)]
55

66
// Some type that is not copyable.
77
struct Bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let const { () } = ();
3+
//~^ ERROR inline-const in pattern position is experimental [E0658]
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: inline-const in pattern position is experimental
2+
--> $DIR/feature-gate-inline_const_pat.rs:2:9
3+
|
4+
LL | let const { () } = ();
5+
| ^^^^^
6+
|
7+
= note: see issue #76001 <https://github.com/rust-lang/rust/issues/76001> for more information
8+
= help: add `#![feature(inline_const_pat)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/half-open-range-patterns/range_pat_interactions0.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(incomplete_features)]
33
#![feature(exclusive_range_pattern)]
44
#![feature(half_open_range_patterns)]
5-
#![feature(inline_const)]
5+
#![feature(inline_const_pat)]
66

77
fn main() {
88
let mut if_lettable = vec![];

src/test/ui/half-open-range-patterns/range_pat_interactions3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
y @ (0..5 | 6) => or_two.push(y),
1313
//~^ exclusive range pattern syntax is experimental
1414
y @ 0..const { 5 + 1 } => assert_eq!(y, 5),
15-
//~^ inline-const is experimental
15+
//~^ inline-const in pattern position is experimental
1616
//~| exclusive range pattern syntax is experimental
1717
y @ -5.. => range_from.push(y),
1818
y @ ..-7 => assert_eq!(y, -8),

src/test/ui/half-open-range-patterns/range_pat_interactions3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ LL | y @ ..-7 => assert_eq!(y, -8),
77
= note: see issue #67264 <https://github.com/rust-lang/rust/issues/67264> for more information
88
= help: add `#![feature(half_open_range_patterns)]` to the crate attributes to enable
99

10-
error[E0658]: inline-const is experimental
10+
error[E0658]: inline-const in pattern position is experimental
1111
--> $DIR/range_pat_interactions3.rs:14:20
1212
|
1313
LL | y @ 0..const { 5 + 1 } => assert_eq!(y, 5),
1414
| ^^^^^
1515
|
1616
= note: see issue #76001 <https://github.com/rust-lang/rust/issues/76001> for more information
17-
= help: add `#![feature(inline_const)]` to the crate attributes to enable
17+
= help: add `#![feature(inline_const_pat)]` to the crate attributes to enable
1818

1919
error[E0658]: exclusive range pattern syntax is experimental
2020
--> $DIR/range_pat_interactions3.rs:10:17

src/test/ui/inline-const/const-expr-array-init.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// build-pass
22

3-
#![allow(incomplete_features)]
43
#![feature(inline_const)]
54

65
use std::cell::Cell;

src/test/ui/inline-const/const-expr-basic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22

3-
#![allow(incomplete_features)]
43
#![feature(inline_const)]
4+
55
fn foo() -> i32 {
66
const {
77
let x = 5 + 10;

src/test/ui/inline-const/const-expr-inference.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// check-pass
22

33
#![feature(inline_const)]
4-
#![allow(incomplete_features)]
54

65
pub fn todo<T>() -> T {
76
const { todo!() }

src/test/ui/inline-const/const-expr-lifetime-err.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(incomplete_features)]
21
#![feature(const_mut_refs)]
32
#![feature(inline_const)]
43

src/test/ui/inline-const/const-expr-lifetime-err.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0597]: `y` does not live long enough
2-
--> $DIR/const-expr-lifetime-err.rs:24:30
2+
--> $DIR/const-expr-lifetime-err.rs:23:30
33
|
44
LL | fn foo<'a>() {
55
| -- lifetime `'a` defined here

src/test/ui/inline-const/const-expr-lifetime.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-pass
22

3-
#![allow(incomplete_features)]
43
#![feature(const_mut_refs)]
54
#![feature(inline_const)]
65

src/test/ui/inline-const/const-expr-macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22

3-
#![allow(incomplete_features)]
43
#![feature(inline_const)]
4+
55
macro_rules! do_const_block{
66
($val:block) => { const $val }
77
}

src/test/ui/inline-const/const-expr-reference.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-pass
22

3-
#![allow(incomplete_features)]
43
#![feature(inline_const)]
54

65
const fn bar() -> i32 {

src/test/ui/inline-const/const-match-pat-generic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(incomplete_features)]
2-
#![feature(inline_const)]
2+
#![feature(inline_const_pat)]
33

44
// rust-lang/rust#82518: ICE with inline-const in match referencing const-generic parameter
55

src/test/ui/inline-const/const-match-pat-inference.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// check-pass
22

3-
#![feature(inline_const)]
3+
#![feature(inline_const_pat)]
44
#![allow(incomplete_features)]
55

66
fn main() {

src/test/ui/inline-const/const-match-pat-lifetime-err.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(incomplete_features)]
44
#![feature(const_mut_refs)]
5-
#![feature(inline_const)]
5+
#![feature(inline_const_pat)]
66

77
use std::marker::PhantomData;
88

src/test/ui/inline-const/const-match-pat-lifetime.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(incomplete_features)]
44
#![feature(const_mut_refs)]
55
#![feature(inline_const)]
6+
#![feature(inline_const_pat)]
67

78
use std::marker::PhantomData;
89

src/test/ui/inline-const/const-match-pat-range.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-pass
22

33
#![allow(incomplete_features)]
4-
#![feature(inline_const, half_open_range_patterns, exclusive_range_pattern)]
4+
#![feature(inline_const_pat, half_open_range_patterns, exclusive_range_pattern)]
55
fn main() {
66
const N: u32 = 10;
77
let x: u32 = 3;

src/test/ui/inline-const/const-match-pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22

33
#![allow(incomplete_features)]
4-
#![feature(inline_const)]
4+
#![feature(inline_const_pat)]
55
const MMIO_BIT1: u8 = 4;
66
const MMIO_BIT2: u8 = 5;
77

src/test/ui/lint/dead-code/anon-const-in-pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// check-pass
2-
#![feature(inline_const)]
2+
#![feature(inline_const_pat)]
33
#![allow(incomplete_features)]
44
#![deny(dead_code)]
55

src/test/ui/pattern/non-structural-match-types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(incomplete_features)]
33
#![allow(unreachable_code)]
44
#![feature(const_async_blocks)]
5-
#![feature(inline_const)]
5+
#![feature(inline_const_pat)]
66

77
fn main() {
88
match loop {} {

src/test/ui/simd/intrinsic/generic-elements-pass.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// ignore-emscripten FIXME(#45351) hits an LLVM assert
33

44
#![feature(repr_simd, platform_intrinsics)]
5-
#![allow(incomplete_features)]
65
#![feature(inline_const)]
76

87
#[repr(simd)]

0 commit comments

Comments
 (0)