Skip to content

Commit

Permalink
Rollup merge of rust-lang#65946 - ecstatic-morse:refactor-promotion2,…
Browse files Browse the repository at this point in the history
… r=eddyb

Make `promote_consts` emit the errors when required promotion fails

A very minimal version of rust-lang#65942.

This will cause a generic "argument X is required to be a constant" message for `simd_shuffle` LLVM intrinsics instead of the [custom one](https://github.com/rust-lang/rust/blob/caa1f8d7b3b021c86a70ff62d23a07d97acff4c4/src/librustc_mir/transform/qualify_consts.rs#L1616). It may be possible to remove this special-casing altogether after rust-lang/stdarch#825.

r? @eddyb
  • Loading branch information
tmandry committed Oct 31, 2019
2 parents a6e1a73 + 627e3ef commit 93ce064
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/librustc_mir/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 23 additions & 6 deletions src/librustc_mir/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<usize>> {
let attrs = tcx.get_attrs(def_id);
let attr = attrs.iter().find(|a| a.check_name(sym::rustc_args_required_const))?;
Expand Down Expand Up @@ -727,16 +738,22 @@ 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.

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

Expand Down
14 changes: 3 additions & 11 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,20 +1606,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));
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/src/error_codes_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const WHITELIST: &[&str] = &[
"E0514",
"E0519",
"E0523",
"E0526",
"E0554",
"E0570",
"E0629",
Expand Down

0 comments on commit 93ce064

Please sign in to comment.