Skip to content

Commit

Permalink
syntax: Add feature gate.
Browse files Browse the repository at this point in the history
This commit adds a `const_in_array_repeat_expressions` feature gate and
only create `Candidate::Repeat` if it is enabled.
  • Loading branch information
davidtwco committed Jul 7, 2019
1 parent 485a802 commit 3cca4ce
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 6 deletions.
@@ -0,0 +1,11 @@
# `const_in_array_repeat_expressions`

The tracking issue for this feature is: [#49147]

[#44109]: https://github.com/rust-lang/rust/issues/49147

------------------------

Relaxes the rules for repeat expressions, `[x; N]` such that `x` may also be `const` (strictly
speaking rvalue promotable), in addition to `typeof(x): Copy`. The result of `[x; N]` where `x` is
`const` is itself also `const`.
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -811,7 +811,7 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
let not_promotable = IsNotImplicitlyPromotable::in_operand(self, operand) ||
IsNotPromotable::in_operand(self, operand);
debug!("assign: self.def_id={:?} operand={:?}", self.def_id, operand);
if !not_promotable {
if !not_promotable && self.tcx.features().const_in_array_repeat_expressions {
debug!("assign: candidate={:?}", candidate);
self.promotion_candidates.push(candidate);
}
Expand Down
7 changes: 5 additions & 2 deletions src/libsyntax/feature_gate.rs
Expand Up @@ -562,10 +562,10 @@ declare_features! (
// Allows `if/while p && let q = r && ...` chains.
(active, let_chains, "1.37.0", Some(53667), None),

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

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

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

// Allows `[x; N]` where `x` is a constant (RFC 2203).
(active, const_in_array_repeat_expressions, "1.37.0", Some(49147), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax_pos/symbol.rs
Expand Up @@ -199,6 +199,7 @@ symbols! {
const_fn_union,
const_generics,
const_indexing,
const_in_array_repeat_expressions,
const_let,
const_panic,
const_raw_ptr_deref,
Expand Down
@@ -1,5 +1,6 @@
// ignore-compile-mode-nll
// compile-flags: -Z borrowck=migrate
#![feature(constants_in_array_repeat_expressions)]
#![allow(warnings)]

// Some type that is not copyable.
Expand Down
@@ -1,13 +1,13 @@
error: repeated expression does not implement `std::marker::Copy`
--> $DIR/migrate-borrowck.rs:87:37
--> $DIR/migrate-borrowck.rs:88:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
|
= note: the `std::marker::Copy` trait is required because the repeated element will be copied

error: repeated expression does not implement `std::marker::Copy`
--> $DIR/migrate-borrowck.rs:103:37
--> $DIR/migrate-borrowck.rs:104:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
Expand Down
@@ -1,6 +1,6 @@
// ignore-compile-mode-nll
#![allow(warnings)]
#![feature(nll)]
#![feature(constants_in_array_repeat_expressions, nll)]

// Some type that is not copyable.
struct Bar;
Expand Down
@@ -0,0 +1,11 @@
// ignore-tidy-linelength
#![allow(warnings)]

struct Bar;

fn foo() {
let arr: [Option<String>; 2] = [None::<String>; 2];
//~^ ERROR the trait bound `std::option::Option<std::string::String>: std::marker::Copy` is not satisfied [E0277]
}

fn main() {}
@@ -0,0 +1,13 @@
error[E0277]: the trait bound `std::option::Option<std::string::String>: std::marker::Copy` is not satisfied
--> $DIR/feature-gate-const_in_array_repeat_expressions.rs:7:36
|
LL | let arr: [Option<String>; 2] = [None::<String>; 2];
| ^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<std::string::String>`
|
= help: the following implementations were found:
<std::option::Option<T> as std::marker::Copy>
= note: the `Copy` trait is required because the repeated element will be copied

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

0 comments on commit 3cca4ce

Please sign in to comment.