Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove const_in_array_repeat #80404

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,6 @@ declare_features! (
/// Allows `async || body` closures.
(active, async_closure, "1.37.0", Some(62290), None),

/// Allows `[x; N]` where `x` is a constant (RFC 2203).
(active, const_in_array_repeat_expressions, "1.37.0", Some(49147), None),
JulianKnodt marked this conversation as resolved.
Show resolved Hide resolved

/// Allows `impl Trait` to be used inside type aliases (RFC 2515).
(active, type_alias_impl_trait, "1.38.0", Some(63063), None),

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ declare_features! (
(removed, extern_in_paths, "1.33.0", Some(55600), None,
Some("subsumed by `::foo::bar` paths")),
(removed, quote, "1.33.0", Some(29601), None, None),
/// Allows `[x; N]` where `x` is a constant (RFC 2203).
(removed, const_in_array_repeat_expressions, "1.37.0", Some(49147), None,
Some("removed due to causing promotable bugs")),
/// Allows using `#[unsafe_destructor_blind_to_params]` (RFC 1238).
(removed, dropck_parametricity, "1.38.0", Some(28498), None, None),
(removed, await_macro, "1.38.0", Some(50547), None,
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ pub enum ObligationCauseCode<'tcx> {
/// Inline asm operand type must be `Sized`.
InlineAsmSized,
/// `[T, ..n]` implies that `T` must be `Copy`.
/// If `true`, suggest `const_in_array_repeat_expressions` feature flag.
RepeatVec(bool),
RepeatVec,

/// Types of fields (other than the last, except for packed structs) in a struct must be sized.
FieldSized {
Expand Down
15 changes: 1 addition & 14 deletions compiler/rustc_mir/src/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ use rustc_trait_selection::traits::{self, ObligationCause, PredicateObligations}
use crate::dataflow::impls::MaybeInitializedPlaces;
use crate::dataflow::move_paths::MoveData;
use crate::dataflow::ResultsCursor;
use crate::transform::{
check_consts::ConstCx,
promote_consts::should_suggest_const_in_array_repeat_expressions_attribute,
};

use crate::borrow_check::{
borrow_set::BorrowSet,
Expand Down Expand Up @@ -1997,22 +1993,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
let span = body.source_info(location).span;
let ty = operand.ty(body, tcx);
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) {
let ccx = ConstCx::new_with_param_env(tcx, body, self.param_env);
// To determine if `const_in_array_repeat_expressions` feature gate should
// be mentioned, need to check if the rvalue is promotable.
let should_suggest =
should_suggest_const_in_array_repeat_expressions_attribute(
&ccx, operand,
);
debug!("check_rvalue: should_suggest={:?}", should_suggest);

let def_id = body.source.def_id().expect_local();
self.infcx.report_selection_error(
&traits::Obligation::new(
ObligationCause::new(
span,
self.tcx().hir().local_def_id_to_hir_id(def_id),
traits::ObligationCauseCode::RepeatVec(should_suggest),
traits::ObligationCauseCode::RepeatVec,
),
self.param_env,
ty::Binder::bind(ty::TraitRef::new(
Expand Down
66 changes: 3 additions & 63 deletions compiler/rustc_mir/src/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ pub enum Candidate {
/// Borrow of a constant temporary, candidate for lifetime extension.
Ref(Location),

/// Promotion of the `x` in `[x; 32]`.
Repeat(Location),

/// Currently applied to function calls where the callee has the unstable
/// `#[rustc_args_required_const]` attribute as well as the SIMD shuffle
/// intrinsic. The intrinsic requires the arguments are indeed constant and
Expand All @@ -120,14 +117,14 @@ impl Candidate {
/// Returns `true` if we should use the "explicit" rules for promotability for this `Candidate`.
fn forces_explicit_promotion(&self) -> bool {
match self {
Candidate::Ref(_) | Candidate::Repeat(_) => false,
Candidate::Ref(_) => false,
Candidate::Argument { .. } | Candidate::InlineAsm { .. } => true,
}
}

fn source_info(&self, body: &Body<'_>) -> SourceInfo {
match self {
Candidate::Ref(location) | Candidate::Repeat(location) => *body.source_info(*location),
Candidate::Ref(location) => *body.source_info(*location),
Candidate::Argument { bb, .. } | Candidate::InlineAsm { bb, .. } => {
*body.source_info(body.terminator_loc(*bb))
}
Expand Down Expand Up @@ -213,11 +210,6 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
Rvalue::Ref(..) => {
self.candidates.push(Candidate::Ref(location));
}
Rvalue::Repeat(..) if self.ccx.tcx.features().const_in_array_repeat_expressions => {
// FIXME(#49147) only promote the element when it isn't `Copy`
// (so that code that can copy it at runtime is unaffected).
self.candidates.push(Candidate::Repeat(location));
JulianKnodt marked this conversation as resolved.
Show resolved Hide resolved
}
_ => {}
}
}
Expand Down Expand Up @@ -334,21 +326,6 @@ impl<'tcx> Validator<'_, 'tcx> {
_ => bug!(),
}
}
Candidate::Repeat(loc) => {
assert!(!self.explicit);

let statement = &self.body[loc.block].statements[loc.statement_index];
match &statement.kind {
StatementKind::Assign(box (_, Rvalue::Repeat(ref operand, _))) => {
if !self.tcx.features().const_in_array_repeat_expressions {
return Err(Unpromotable);
}

self.validate_operand(operand)
}
_ => bug!(),
}
}
Candidate::Argument { bb, index } => {
assert!(self.explicit);

Expand Down Expand Up @@ -1090,18 +1067,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
_ => bug!(),
}
}
Candidate::Repeat(loc) => {
let statement = &mut blocks[loc.block].statements[loc.statement_index];
match statement.kind {
StatementKind::Assign(box (_, Rvalue::Repeat(ref mut operand, _))) => {
let ty = operand.ty(local_decls, self.tcx);
let span = statement.source_info.span;

Rvalue::Use(mem::replace(operand, promoted_operand(ty, span)))
}
_ => bug!(),
}
}
Candidate::Argument { bb, index } => {
let terminator = blocks[bb].terminator_mut();
match terminator.kind {
Expand Down Expand Up @@ -1182,8 +1147,7 @@ pub fn promote_candidates<'tcx>(
let mut extra_statements = vec![];
for candidate in candidates.into_iter().rev() {
match candidate {
Candidate::Repeat(Location { block, statement_index })
| Candidate::Ref(Location { block, statement_index }) => {
Candidate::Ref(Location { block, statement_index }) => {
if let StatementKind::Assign(box (place, _)) =
&body[block].statements[statement_index].kind
{
Expand Down Expand Up @@ -1267,27 +1231,3 @@ pub fn promote_candidates<'tcx>(

promotions
}

/// This function returns `true` if the `const_in_array_repeat_expressions` feature attribute should
/// be suggested. This function is probably quite expensive, it shouldn't be run in the happy path.
/// Feature attribute should be suggested if `operand` can be promoted and the feature is not
/// enabled.
crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>(
ccx: &ConstCx<'_, 'tcx>,
operand: &Operand<'tcx>,
) -> bool {
let mut rpo = traversal::reverse_postorder(&ccx.body);
let (temps, _) = collect_temps_and_candidates(&ccx, &mut rpo);
let validator = Validator { ccx, temps: &temps, explicit: false };

let should_promote = validator.validate_operand(operand).is_ok();
let feature_flag = validator.ccx.tcx.features().const_in_array_repeat_expressions;
debug!(
"should_suggest_const_in_array_repeat_expressions_flag: def_id={:?} \
should_promote={:?} feature_flag={:?}",
validator.ccx.def_id(),
should_promote,
feature_flag
);
should_promote && !feature_flag
}
Original file line number Diff line number Diff line change
Expand Up @@ -1881,23 +1881,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
ObligationCauseCode::Coercion { source: _, target } => {
err.note(&format!("required by cast to type `{}`", self.ty_to_string(target)));
}
ObligationCauseCode::RepeatVec(suggest_const_in_array_repeat_expressions) => {
ObligationCauseCode::RepeatVec => {
err.note(
"the `Copy` trait is required because the repeated element will be copied",
);
if suggest_const_in_array_repeat_expressions {
err.note(
"this array initializer can be evaluated at compile-time, see issue \
#49147 <https://github.com/rust-lang/rust/issues/49147> \
for more information",
);
if tcx.sess.opts.unstable_features.is_nightly_build() {
err.help(
"add `#![feature(const_in_array_repeat_expressions)]` to the \
crate attributes to enable",
);
}
}
}
ObligationCauseCode::VariableType(hir_id) => {
let parent_node = self.tcx.hir().get_parent_node(hir_id);
Expand Down
1 change: 0 additions & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
#![feature(coerce_unsized)]
#![feature(const_btree_new)]
#![feature(const_fn)]
#![feature(const_in_array_repeat_expressions)]
#![feature(cow_is_borrowed)]
#![feature(const_cow_is_borrowed)]
#![feature(dispatch_from_dyn)]
Expand Down

This file was deleted.

15 changes: 15 additions & 0 deletions src/test/ui/array-slice-vec/repeat_empty_ok.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![crate_type = "lib"]

pub struct Header<'a> {
pub value: &'a [u8],
}

pub fn test() {
let headers = [Header{value: &[]}; 128];
//~^ ERROR the trait bound
}

pub fn test2() {
let headers = [Header{value: &[0]}; 128];
//~^ ERROR the trait bound
}
19 changes: 19 additions & 0 deletions src/test/ui/array-slice-vec/repeat_empty_ok.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0277]: the trait bound `Header<'_>: Copy` is not satisfied
--> $DIR/repeat_empty_ok.rs:8:19
|
LL | let headers = [Header{value: &[]}; 128];
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
|
= note: the `Copy` trait is required because the repeated element will be copied

error[E0277]: the trait bound `Header<'_>: Copy` is not satisfied
--> $DIR/repeat_empty_ok.rs:13:19
|
LL | let headers = [Header{value: &[0]}; 128];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
|
= note: the `Copy` trait is required because the repeated element will be copied

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-pass

#![allow(unused)]
#![feature(const_in_array_repeat_expressions)]
#![feature(inline_const)]
#![allow(unused, incomplete_features)]

// Some type that is not copyable.
struct Bar;
Expand All @@ -18,6 +18,6 @@ const _: [u32; 2] = [type_copy(); 2];

// This is allowed because all promotion contexts use the explicit rules for promotability when
// inside an explicit const context.
const _: [Option<Bar>; 2] = [type_no_copy(); 2];
const _: [Option<Bar>; 2] = [const { type_no_copy() }; 2];

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(const_in_array_repeat_expressions)]

// Some type that is not copyable.
struct Bar;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the trait bound `Option<Bar>: Copy` is not satisfied
--> $DIR/fn-call-in-non-const.rs:16:31
--> $DIR/fn-call-in-non-const.rs:14:31
|
LL | let _: [Option<Bar>; 2] = [no_copy(); 2];
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Option<Bar>`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// ignore-compare-mode-nll
// compile-flags: -Z borrowck=migrate
#![feature(const_in_array_repeat_expressions)]
#![allow(warnings)]

// Some type that is not copyable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the trait bound `Option<Bar>: Copy` is not satisfied
--> $DIR/migrate-fail.rs:14:37
--> $DIR/migrate-fail.rs:13:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `Copy` is not implemented for `Option<Bar>`
Expand All @@ -9,7 +9,7 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
= note: the `Copy` trait is required because the repeated element will be copied

error[E0277]: the trait bound `Option<Bar>: Copy` is not satisfied
--> $DIR/migrate-fail.rs:20:37
--> $DIR/migrate-fail.rs:19:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `Copy` is not implemented for `Option<Bar>`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// check-pass
// compile-flags: -Z borrowck=migrate
// ignore-compare-mode-nll
#![feature(const_in_array_repeat_expressions)]
#![allow(warnings)]

// Some type that is not copyable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ignore-compare-mode-nll
#![feature(const_in_array_repeat_expressions, nll)]
#![allow(warnings)]

// Some type that is not copyable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the trait bound `Option<Bar>: Copy` is not satisfied
--> $DIR/nll-fail.rs:13:37
--> $DIR/nll-fail.rs:12:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `Copy` is not implemented for `Option<Bar>`
Expand All @@ -9,7 +9,7 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
= note: the `Copy` trait is required because the repeated element will be copied

error[E0277]: the trait bound `Option<Bar>: Copy` is not satisfied
--> $DIR/nll-fail.rs:19:37
--> $DIR/nll-fail.rs:18:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `Copy` is not implemented for `Option<Bar>`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// check-pass
// ignore-compare-mode-nll
#![allow(warnings)]
#![feature(const_in_array_repeat_expressions, nll)]

// Some type that is not copyable.
struct Bar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// run-pass
#![feature(const_in_array_repeat_expressions)]

#[derive(Debug, Eq, PartialEq)]
struct Bar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(const_in_array_repeat_expressions)]

#[derive(Copy, Clone)]
struct Foo<T>(T);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the trait bound `Foo<String>: Copy` is not satisfied
--> $DIR/trait-error.rs:7:5
--> $DIR/trait-error.rs:5:5
|
LL | [Foo(String::new()); 4];
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Foo<String>`
Expand Down

This file was deleted.

Loading