From 9bb983380b3ebb61e8b6333f7af61b3c04c062da Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Tue, 29 Oct 2019 08:24:59 -0700 Subject: [PATCH 1/4] Add method to `Candidate` that determines its promotability rules --- src/librustc_mir/transform/promote_consts.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs index 3af08090853a6..ced5c0146d9cd 100644 --- a/src/librustc_mir/transform/promote_consts.rs +++ b/src/librustc_mir/transform/promote_consts.rs @@ -80,6 +80,17 @@ pub enum Candidate { Argument { bb: BasicBlock, index: usize }, } +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::Argument { .. } => true, + } + } +} + fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { let attrs = tcx.get_attrs(def_id); let attr = attrs.iter().find(|a| a.check_name(sym::rustc_args_required_const))?; @@ -727,11 +738,7 @@ pub fn validate_candidates( }; candidates.iter().copied().filter(|&candidate| { - validator.explicit = match candidate { - Candidate::Ref(_) | - Candidate::Repeat(_) => false, - Candidate::Argument { .. } => true, - }; + validator.explicit = candidate.forces_explicit_promotion(); // FIXME(eddyb) also emit the errors for shuffle indices // and `#[rustc_args_required_const]` arguments here. From 46b68b009ba64330f6ad503a1882a7df637b1a86 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Tue, 29 Oct 2019 12:50:43 -0700 Subject: [PATCH 2/4] Emit errors in `promote_consts` when required promotion fails --- src/librustc_mir/transform/promote_consts.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs index ced5c0146d9cd..9bd9aba945e3c 100644 --- a/src/librustc_mir/transform/promote_consts.rs +++ b/src/librustc_mir/transform/promote_consts.rs @@ -743,7 +743,17 @@ pub fn validate_candidates( // FIXME(eddyb) also emit the errors for shuffle indices // and `#[rustc_args_required_const]` arguments here. - validator.validate_candidate(candidate).is_ok() + let is_promotable = validator.validate_candidate(candidate).is_ok(); + match candidate { + Candidate::Argument { bb, index } if !is_promotable => { + let span = body[bb].terminator().source_info.span; + let msg = format!("argument {} is required to be a constant", index + 1); + tcx.sess.span_err(span, &msg); + } + _ => () + } + + is_promotable }).collect() } From 122c6fedf4929c262e73158f156c2446c4b8bb84 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Tue, 29 Oct 2019 12:56:59 -0700 Subject: [PATCH 3/4] Stop emitting error in `qualify_consts` if promotion fails --- src/librustc_mir/transform/qualify_consts.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 2f77cd5ddf716..49805dfc026f3 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -1599,20 +1599,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> { // This is not a problem, because the argument explicitly // requests constness, in contrast to regular promotion // which happens even without the user requesting it. - // We can error out with a hard error if the argument is not - // constant here. + // + // `promote_consts` is responsible for emitting the error if + // the argument is not promotable. if !IsNotPromotable::in_operand(self, arg) { debug!("visit_terminator_kind: candidate={:?}", candidate); self.promotion_candidates.push(candidate); - } else { - if is_shuffle { - span_err!(self.tcx.sess, self.span, E0526, - "shuffle indices are not constant"); - } else { - self.tcx.sess.span_err(self.span, - &format!("argument {} is required to be a constant", - i + 1)); - } } } } From 627e3ef739dbead85aa04f32dc2d2a2ab7dd4637 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Wed, 30 Oct 2019 08:28:11 -0700 Subject: [PATCH 4/4] Remove references to now unused `E0526` It remains as a comment in `error_codes.rs` for consistency with other unused errors. --- src/librustc_mir/error_codes.rs | 2 +- src/tools/tidy/src/error_codes_check.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustc_mir/error_codes.rs b/src/librustc_mir/error_codes.rs index 419c905cb5127..c119ca536fb52 100644 --- a/src/librustc_mir/error_codes.rs +++ b/src/librustc_mir/error_codes.rs @@ -2545,7 +2545,7 @@ There are some known bugs that trigger this message. // E0471, // constant evaluation error (in pattern) // E0385, // {} in an aliasable location E0521, // borrowed data escapes outside of closure - E0526, // shuffle indices are not constant +// E0526, // shuffle indices are not constant E0594, // cannot assign to {} // E0598, // lifetime of {} is too short to guarantee its contents can be... E0625, // thread-local statics cannot be accessed at compile-time diff --git a/src/tools/tidy/src/error_codes_check.rs b/src/tools/tidy/src/error_codes_check.rs index 159baff184d1b..bd58de81c778a 100644 --- a/src/tools/tidy/src/error_codes_check.rs +++ b/src/tools/tidy/src/error_codes_check.rs @@ -40,7 +40,6 @@ const WHITELIST: &[&str] = &[ "E0514", "E0519", "E0523", - "E0526", "E0554", "E0570", "E0629",