Skip to content

Commit bfc10a8

Browse files
Allow errors to abort const checking when emitted
This is a hack for parity with `qualify_min_const_fn`, which only emitted a single error.
1 parent 7fb9587 commit bfc10a8

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

compiler/rustc_mir/src/transform/check_consts/ops.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub enum Status {
6262

6363
/// An operation that is not *always* allowed in a const context.
6464
pub trait NonConstOp: std::fmt::Debug {
65+
const STOPS_CONST_CHECKING: bool = false;
66+
6567
/// Returns an enum indicating whether this operation is allowed within the given item.
6668
fn status_in_item(&self, _ccx: &ConstCx<'_, '_>) -> Status {
6769
Status::Forbidden

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ pub struct Validator<'mir, 'tcx> {
176176

177177
/// The span of the current statement.
178178
span: Span,
179+
180+
const_checking_stopped: bool,
179181
}
180182

181183
impl Deref for Validator<'mir, 'tcx> {
@@ -188,7 +190,12 @@ impl Deref for Validator<'mir, 'tcx> {
188190

189191
impl Validator<'mir, 'tcx> {
190192
pub fn new(ccx: &'mir ConstCx<'mir, 'tcx>) -> Self {
191-
Validator { span: ccx.body.span, ccx, qualifs: Default::default() }
193+
Validator {
194+
span: ccx.body.span,
195+
ccx,
196+
qualifs: Default::default(),
197+
const_checking_stopped: false,
198+
}
192199
}
193200

194201
pub fn check_body(&mut self) {
@@ -226,13 +233,22 @@ impl Validator<'mir, 'tcx> {
226233

227234
/// Emits an error if an expression cannot be evaluated in the current context.
228235
pub fn check_op(&mut self, op: impl NonConstOp) {
229-
ops::non_const(self.ccx, op, self.span);
236+
self.check_op_spanned(op, self.span);
230237
}
231238

232239
/// Emits an error at the given `span` if an expression cannot be evaluated in the current
233240
/// context.
234-
pub fn check_op_spanned(&mut self, op: impl NonConstOp, span: Span) {
235-
ops::non_const(self.ccx, op, span);
241+
pub fn check_op_spanned<O: NonConstOp>(&mut self, op: O, span: Span) {
242+
// HACK: This is for strict equivalence with the old `qualify_min_const_fn` pass, which
243+
// only emitted one error per function. It should be removed and the test output updated.
244+
if self.const_checking_stopped {
245+
return;
246+
}
247+
248+
let err_emitted = ops::non_const(self.ccx, op, span);
249+
if err_emitted && O::STOPS_CONST_CHECKING {
250+
self.const_checking_stopped = true;
251+
}
236252
}
237253

238254
fn check_static(&mut self, def_id: DefId, span: Span) {

0 commit comments

Comments
 (0)