Skip to content

Commit 67ad0ff

Browse files
use body.tainted_by_error to skip loading MIR
1 parent a431174 commit 67ad0ff

File tree

6 files changed

+21
-58
lines changed

6 files changed

+21
-58
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::interpret::{
66
ScalarMaybeUninit, StackPopCleanup,
77
};
88

9-
use rustc_errors::ErrorReported;
109
use rustc_hir::def::DefKind;
1110
use rustc_middle::mir;
1211
use rustc_middle::mir::interpret::ErrorHandled;
@@ -281,28 +280,6 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
281280

282281
let cid = key.value;
283282
let def = cid.instance.def.with_opt_param();
284-
285-
if let Some(def) = def.as_local() {
286-
if tcx.has_typeck_results(def.did) {
287-
if let Some(error_reported) = tcx.typeck_opt_const_arg(def).tainted_by_errors {
288-
return Err(ErrorHandled::Reported(error_reported));
289-
}
290-
if let Some(error_reported) = tcx.mir_borrowck_opt_const_arg(def).tainted_by_errors {
291-
return Err(ErrorHandled::Reported(error_reported));
292-
}
293-
}
294-
if !tcx.is_mir_available(def.did) {
295-
tcx.sess.delay_span_bug(
296-
tcx.def_span(def.did),
297-
&format!("no MIR body is available for {:?}", def.did),
298-
);
299-
return Err(ErrorHandled::Reported(ErrorReported {}));
300-
}
301-
if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured {
302-
return Err(ErrorHandled::Reported(error_reported));
303-
}
304-
}
305-
306283
let is_static = tcx.is_static(def.did);
307284

308285
let mut ecx = InterpCx::new(

compiler/rustc_const_eval/src/const_eval/machine.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustc_errors::ErrorReported;
2+
use rustc_hir::def::DefKind;
13
use rustc_middle::mir;
24
use rustc_middle::ty::{self, Ty};
35
use std::borrow::Borrow;
@@ -243,6 +245,12 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
243245
ty::InstanceDef::Item(def) => {
244246
if ecx.tcx.is_ctfe_mir_available(def.did) {
245247
Ok(ecx.tcx.mir_for_ctfe_opt_const_arg(def))
248+
} else if ecx.tcx.def_kind(def.did) == DefKind::AssocConst {
249+
ecx.tcx.sess.delay_span_bug(
250+
rustc_span::DUMMY_SP,
251+
"This is likely a const item that is missing from its impl",
252+
);
253+
throw_inval!(AlreadyReported(ErrorReported {}));
246254
} else {
247255
let path = ecx.tcx.def_path_str(def.did);
248256
Err(ConstEvalErrKind::NeedsRfc(format!("calling extern function `{}`", path))

compiler/rustc_const_eval/src/interpret/eval_context.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -509,26 +509,18 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
509509
instance: ty::InstanceDef<'tcx>,
510510
promoted: Option<mir::Promoted>,
511511
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
512-
// do not continue if typeck errors occurred (can only occur in local crate)
513512
let def = instance.with_opt_param();
514-
if let Some(def) = def.as_local() {
515-
if self.tcx.has_typeck_results(def.did) {
516-
if let Some(error_reported) = self.tcx.typeck_opt_const_arg(def).tainted_by_errors {
517-
throw_inval!(AlreadyReported(error_reported));
518-
}
519-
if let Some(error_reported) =
520-
self.tcx.mir_borrowck_opt_const_arg(def).tainted_by_errors
521-
{
522-
throw_inval!(AlreadyReported(error_reported));
523-
}
524-
}
525-
}
526-
527513
trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
528-
if let Some(promoted) = promoted {
529-
return Ok(&self.tcx.promoted_mir_opt_const_arg(def)[promoted]);
514+
let body = if let Some(promoted) = promoted {
515+
&self.tcx.promoted_mir_opt_const_arg(def)[promoted]
516+
} else {
517+
M::load_mir(self, instance)?
518+
};
519+
// do not continue if typeck errors occurred (can only occur in local crate)
520+
if let Some(err) = body.tainted_by_errors {
521+
throw_inval!(AlreadyReported(err));
530522
}
531-
M::load_mir(self, instance)
523+
Ok(body)
532524
}
533525

534526
/// Call this on things you got out of the MIR (so it is as generic as the current

compiler/rustc_middle/src/query/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -836,14 +836,13 @@ rustc_queries! {
836836
/// additional requirements that the closure's creator must verify.
837837
query mir_borrowck(key: LocalDefId) -> &'tcx mir::BorrowCheckResult<'tcx> {
838838
desc { |tcx| "borrow-checking `{}`", tcx.def_path_str(key.to_def_id()) }
839-
cache_on_disk_if { true }
839+
cache_on_disk_if(tcx) { tcx.is_typeck_child(key.to_def_id()) }
840840
}
841841
query mir_borrowck_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::BorrowCheckResult<'tcx> {
842842
desc {
843843
|tcx| "borrow-checking the const argument`{}`",
844844
tcx.def_path_str(key.0.to_def_id())
845845
}
846-
cache_on_disk_if { true }
847846
}
848847

849848
/// Gets a complete map from all types to their inherent impls.

src/test/ui/consts/const-fn-error.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const fn f(x: usize) -> usize {
66
//~^ ERROR mutable references
77
//~| ERROR calls in constant functions
88
//~| ERROR calls in constant functions
9-
//~| ERROR E0080
109
//~| ERROR `for` is not allowed in a `const fn`
1110
sum += i;
1211
}

src/test/ui/consts/const-fn-error.stderr

+3-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | / for i in 0..x {
55
LL | |
66
LL | |
77
LL | |
8-
... |
8+
LL | |
99
LL | | sum += i;
1010
LL | | }
1111
| |_____^
@@ -34,19 +34,7 @@ error[E0015]: calls in constant functions are limited to constant functions, tup
3434
LL | for i in 0..x {
3535
| ^^^^
3636

37-
error[E0080]: evaluation of constant value failed
38-
--> $DIR/const-fn-error.rs:5:14
39-
|
40-
LL | for i in 0..x {
41-
| ^^^^
42-
| |
43-
| calling non-const function `<std::ops::Range<usize> as IntoIterator>::into_iter`
44-
| inside `f` at $DIR/const-fn-error.rs:5:14
45-
...
46-
LL | let a : [i32; f(X)];
47-
| ---- inside `main::{constant#0}` at $DIR/const-fn-error.rs:18:19
48-
49-
error: aborting due to 5 previous errors
37+
error: aborting due to 4 previous errors
5038

51-
Some errors have detailed explanations: E0015, E0080, E0658.
39+
Some errors have detailed explanations: E0015, E0658.
5240
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)