Skip to content

Commit 303dbcc

Browse files
committed
CTFE: dynamically make sure we do not call non-const-fn
1 parent 6b9b97b commit 303dbcc

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,19 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
365365
ret: Option<mir::BasicBlock>,
366366
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
367367
debug!("eval_fn_call: {:?}", instance);
368-
if !ecx.tcx.is_const_fn(instance.def_id()) {
368+
// Execution might have wandered off into other crates, so we cannot to a stability-
369+
// sensitive check here. But we can at least rule out functions that are not const
370+
// at all.
371+
if !ecx.tcx.is_const_fn_raw(instance.def_id()) {
369372
// Some functions we support even if they are non-const -- but avoid testing
370-
// that for const fn!
371-
if ecx.hook_fn(instance, args, dest)? {
373+
// that for const fn! We certainly do *not* want to actually call the fn
374+
// though, so be sure we return here.
375+
return if ecx.hook_fn(instance, args, dest)? {
372376
ecx.goto_block(ret)?; // fully evaluated and done
373-
return Ok(None);
374-
}
377+
Ok(None)
378+
} else {
379+
err!(MachineError(format!("calling non-const function `{}`", instance)))
380+
};
375381
}
376382
// This is a const fn. Call it.
377383
Ok(Some(match ecx.load_mir(instance.def) {

src/test/ui/consts/const-call.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ fn f(x: usize) -> usize {
1515
fn main() {
1616
let _ = [0; f(2)];
1717
//~^ ERROR calls in constants are limited to constant functions
18+
//~| ERROR evaluation of constant value failed
1819
}

src/test/ui/consts/const-call.stderr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
44
LL | let _ = [0; f(2)];
55
| ^^^^
66

7-
error: aborting due to previous error
7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/const-call.rs:16:17
9+
|
10+
LL | let _ = [0; f(2)];
11+
| ^^^^ calling non-const function `f`
12+
13+
error: aborting due to 2 previous errors
814

9-
For more information about this error, try `rustc --explain E0015`.
15+
Some errors occurred: E0015, E0080.
16+
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)