syntax: Add feature gate.

This commit adds a `const_in_array_repeat_expressions` feature gate and
only create `Candidate::Repeat` if it is enabled.
This commit is contained in:
David Wood 2019-06-12 07:56:07 +01:00
parent 485a80255b
commit 3cca4ceed8
No known key found for this signature in database
GPG Key ID: 2592E76C87381FD9
9 changed files with 46 additions and 6 deletions

View File

@ -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`.

View File

@ -811,7 +811,7 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
let not_promotable = IsNotImplicitlyPromotable::in_operand(self, operand) || let not_promotable = IsNotImplicitlyPromotable::in_operand(self, operand) ||
IsNotPromotable::in_operand(self, operand); IsNotPromotable::in_operand(self, operand);
debug!("assign: self.def_id={:?} operand={:?}", self.def_id, 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); debug!("assign: candidate={:?}", candidate);
self.promotion_candidates.push(candidate); self.promotion_candidates.push(candidate);
} }

View File

@ -562,10 +562,10 @@ declare_features! (
// Allows `if/while p && let q = r && ...` chains. // Allows `if/while p && let q = r && ...` chains.
(active, let_chains, "1.37.0", Some(53667), None), (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), (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), (active, transparent_unions, "1.37.0", Some(60405), None),
// Allows explicit discriminants on non-unit enum variants. // Allows explicit discriminants on non-unit enum variants.
@ -580,6 +580,9 @@ declare_features! (
// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests // Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests
(active, cfg_doctest, "1.37.0", Some(62210), None), (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 // feature-group-end: actual feature gates
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------

View File

@ -199,6 +199,7 @@ symbols! {
const_fn_union, const_fn_union,
const_generics, const_generics,
const_indexing, const_indexing,
const_in_array_repeat_expressions,
const_let, const_let,
const_panic, const_panic,
const_raw_ptr_deref, const_raw_ptr_deref,

View File

@ -1,5 +1,6 @@
// ignore-compile-mode-nll // ignore-compile-mode-nll
// compile-flags: -Z borrowck=migrate // compile-flags: -Z borrowck=migrate
#![feature(constants_in_array_repeat_expressions)]
#![allow(warnings)] #![allow(warnings)]
// Some type that is not copyable. // Some type that is not copyable.

View File

@ -1,5 +1,5 @@
error: repeated expression does not implement `std::marker::Copy` 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]; LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>` | ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
@ -7,7 +7,7 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
= note: the `std::marker::Copy` trait is required because the repeated element will be copied = note: the `std::marker::Copy` trait is required because the repeated element will be copied
error: repeated expression does not implement `std::marker::Copy` 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]; LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>` | ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`

View File

@ -1,6 +1,6 @@
// ignore-compile-mode-nll // ignore-compile-mode-nll
#![allow(warnings)] #![allow(warnings)]
#![feature(nll)] #![feature(constants_in_array_repeat_expressions, nll)]
// Some type that is not copyable. // Some type that is not copyable.
struct Bar; struct Bar;

View File

@ -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() {}

View File

@ -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`.