Skip to content

Commit 3cca4ce

Browse files
committed
syntax: Add feature gate.
This commit adds a `const_in_array_repeat_expressions` feature gate and only create `Candidate::Repeat` if it is enabled.
1 parent 485a802 commit 3cca4ce

File tree

9 files changed

+46
-6
lines changed

9 files changed

+46
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `const_in_array_repeat_expressions`
2+
3+
The tracking issue for this feature is: [#49147]
4+
5+
[#44109]: https://github.com/rust-lang/rust/issues/49147
6+
7+
------------------------
8+
9+
Relaxes the rules for repeat expressions, `[x; N]` such that `x` may also be `const` (strictly
10+
speaking rvalue promotable), in addition to `typeof(x): Copy`. The result of `[x; N]` where `x` is
11+
`const` is itself also `const`.

src/librustc_mir/transform/qualify_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
811811
let not_promotable = IsNotImplicitlyPromotable::in_operand(self, operand) ||
812812
IsNotPromotable::in_operand(self, operand);
813813
debug!("assign: self.def_id={:?} operand={:?}", self.def_id, operand);
814-
if !not_promotable {
814+
if !not_promotable && self.tcx.features().const_in_array_repeat_expressions {
815815
debug!("assign: candidate={:?}", candidate);
816816
self.promotion_candidates.push(candidate);
817817
}

src/libsyntax/feature_gate.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,10 @@ declare_features! (
562562
// Allows `if/while p && let q = r && ...` chains.
563563
(active, let_chains, "1.37.0", Some(53667), None),
564564

565-
// #[repr(transparent)] on enums.
565+
// Allows #[repr(transparent)] on enums (RFC 2645).
566566
(active, transparent_enums, "1.37.0", Some(60405), None),
567567

568-
// #[repr(transparent)] on unions.
568+
// Allows #[repr(transparent)] on unions (RFC 2645).
569569
(active, transparent_unions, "1.37.0", Some(60405), None),
570570

571571
// Allows explicit discriminants on non-unit enum variants.
@@ -580,6 +580,9 @@ declare_features! (
580580
// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests
581581
(active, cfg_doctest, "1.37.0", Some(62210), None),
582582

583+
// Allows `[x; N]` where `x` is a constant (RFC 2203).
584+
(active, const_in_array_repeat_expressions, "1.37.0", Some(49147), None),
585+
583586
// -------------------------------------------------------------------------
584587
// feature-group-end: actual feature gates
585588
// -------------------------------------------------------------------------

src/libsyntax_pos/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ symbols! {
199199
const_fn_union,
200200
const_generics,
201201
const_indexing,
202+
const_in_array_repeat_expressions,
202203
const_let,
203204
const_panic,
204205
const_raw_ptr_deref,

src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// ignore-compile-mode-nll
22
// compile-flags: -Z borrowck=migrate
3+
#![feature(constants_in_array_repeat_expressions)]
34
#![allow(warnings)]
45

56
// Some type that is not copyable.

src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: repeated expression does not implement `std::marker::Copy`
2-
--> $DIR/migrate-borrowck.rs:87:37
2+
--> $DIR/migrate-borrowck.rs:88:37
33
|
44
LL | let arr: [Option<Bar>; 2] = [x; 2];
55
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
66
|
77
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
88

99
error: repeated expression does not implement `std::marker::Copy`
10-
--> $DIR/migrate-borrowck.rs:103:37
10+
--> $DIR/migrate-borrowck.rs:104:37
1111
|
1212
LL | let arr: [Option<Bar>; 2] = [x; 2];
1313
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`

src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ignore-compile-mode-nll
22
#![allow(warnings)]
3-
#![feature(nll)]
3+
#![feature(constants_in_array_repeat_expressions, nll)]
44

55
// Some type that is not copyable.
66
struct Bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ignore-tidy-linelength
2+
#![allow(warnings)]
3+
4+
struct Bar;
5+
6+
fn foo() {
7+
let arr: [Option<String>; 2] = [None::<String>; 2];
8+
//~^ ERROR the trait bound `std::option::Option<std::string::String>: std::marker::Copy` is not satisfied [E0277]
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0277]: the trait bound `std::option::Option<std::string::String>: std::marker::Copy` is not satisfied
2+
--> $DIR/feature-gate-const_in_array_repeat_expressions.rs:7:36
3+
|
4+
LL | let arr: [Option<String>; 2] = [None::<String>; 2];
5+
| ^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<std::string::String>`
6+
|
7+
= help: the following implementations were found:
8+
<std::option::Option<T> as std::marker::Copy>
9+
= note: the `Copy` trait is required because the repeated element will be copied
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)