Skip to content

Commit 93ce064

Browse files
authored
Rollup merge of rust-lang#65946 - ecstatic-morse:refactor-promotion2, 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
2 parents a6e1a73 + 627e3ef commit 93ce064

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

src/librustc_mir/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2545,7 +2545,7 @@ There are some known bugs that trigger this message.
25452545
// E0471, // constant evaluation error (in pattern)
25462546
// E0385, // {} in an aliasable location
25472547
E0521, // borrowed data escapes outside of closure
2548-
E0526, // shuffle indices are not constant
2548+
// E0526, // shuffle indices are not constant
25492549
E0594, // cannot assign to {}
25502550
// E0598, // lifetime of {} is too short to guarantee its contents can be...
25512551
E0625, // thread-local statics cannot be accessed at compile-time

src/librustc_mir/transform/promote_consts.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ pub enum Candidate {
8080
Argument { bb: BasicBlock, index: usize },
8181
}
8282

83+
impl Candidate {
84+
/// Returns `true` if we should use the "explicit" rules for promotability for this `Candidate`.
85+
fn forces_explicit_promotion(&self) -> bool {
86+
match self {
87+
Candidate::Ref(_) |
88+
Candidate::Repeat(_) => false,
89+
Candidate::Argument { .. } => true,
90+
}
91+
}
92+
}
93+
8394
fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Vec<usize>> {
8495
let attrs = tcx.get_attrs(def_id);
8596
let attr = attrs.iter().find(|a| a.check_name(sym::rustc_args_required_const))?;
@@ -727,16 +738,22 @@ pub fn validate_candidates(
727738
};
728739

729740
candidates.iter().copied().filter(|&candidate| {
730-
validator.explicit = match candidate {
731-
Candidate::Ref(_) |
732-
Candidate::Repeat(_) => false,
733-
Candidate::Argument { .. } => true,
734-
};
741+
validator.explicit = candidate.forces_explicit_promotion();
735742

736743
// FIXME(eddyb) also emit the errors for shuffle indices
737744
// and `#[rustc_args_required_const]` arguments here.
738745

739-
validator.validate_candidate(candidate).is_ok()
746+
let is_promotable = validator.validate_candidate(candidate).is_ok();
747+
match candidate {
748+
Candidate::Argument { bb, index } if !is_promotable => {
749+
let span = body[bb].terminator().source_info.span;
750+
let msg = format!("argument {} is required to be a constant", index + 1);
751+
tcx.sess.span_err(span, &msg);
752+
}
753+
_ => ()
754+
}
755+
756+
is_promotable
740757
}).collect()
741758
}
742759

src/librustc_mir/transform/qualify_consts.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1606,20 +1606,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
16061606
// This is not a problem, because the argument explicitly
16071607
// requests constness, in contrast to regular promotion
16081608
// which happens even without the user requesting it.
1609-
// We can error out with a hard error if the argument is not
1610-
// constant here.
1609+
//
1610+
// `promote_consts` is responsible for emitting the error if
1611+
// the argument is not promotable.
16111612
if !IsNotPromotable::in_operand(self, arg) {
16121613
debug!("visit_terminator_kind: candidate={:?}", candidate);
16131614
self.promotion_candidates.push(candidate);
1614-
} else {
1615-
if is_shuffle {
1616-
span_err!(self.tcx.sess, self.span, E0526,
1617-
"shuffle indices are not constant");
1618-
} else {
1619-
self.tcx.sess.span_err(self.span,
1620-
&format!("argument {} is required to be a constant",
1621-
i + 1));
1622-
}
16231615
}
16241616
}
16251617
}

src/tools/tidy/src/error_codes_check.rs

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ const WHITELIST: &[&str] = &[
4040
"E0514",
4141
"E0519",
4242
"E0523",
43-
"E0526",
4443
"E0554",
4544
"E0570",
4645
"E0629",

0 commit comments

Comments
 (0)