CTFE: dynamically make sure we do not call non-const-fn
This commit is contained in:
parent
6b9b97bd9b
commit
303dbccf04
3 changed files with 21 additions and 7 deletions
|
@ -365,13 +365,19 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
|
||||||
ret: Option<mir::BasicBlock>,
|
ret: Option<mir::BasicBlock>,
|
||||||
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
|
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
|
||||||
debug!("eval_fn_call: {:?}", instance);
|
debug!("eval_fn_call: {:?}", instance);
|
||||||
if !ecx.tcx.is_const_fn(instance.def_id()) {
|
// Execution might have wandered off into other crates, so we cannot to a stability-
|
||||||
|
// sensitive check here. But we can at least rule out functions that are not const
|
||||||
|
// at all.
|
||||||
|
if !ecx.tcx.is_const_fn_raw(instance.def_id()) {
|
||||||
// Some functions we support even if they are non-const -- but avoid testing
|
// Some functions we support even if they are non-const -- but avoid testing
|
||||||
// that for const fn!
|
// that for const fn! We certainly do *not* want to actually call the fn
|
||||||
if ecx.hook_fn(instance, args, dest)? {
|
// though, so be sure we return here.
|
||||||
|
return if ecx.hook_fn(instance, args, dest)? {
|
||||||
ecx.goto_block(ret)?; // fully evaluated and done
|
ecx.goto_block(ret)?; // fully evaluated and done
|
||||||
return Ok(None);
|
Ok(None)
|
||||||
}
|
} else {
|
||||||
|
err!(MachineError(format!("calling non-const function `{}`", instance)))
|
||||||
|
};
|
||||||
}
|
}
|
||||||
// This is a const fn. Call it.
|
// This is a const fn. Call it.
|
||||||
Ok(Some(match ecx.load_mir(instance.def) {
|
Ok(Some(match ecx.load_mir(instance.def) {
|
||||||
|
|
|
@ -15,4 +15,5 @@ fn f(x: usize) -> usize {
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = [0; f(2)];
|
let _ = [0; f(2)];
|
||||||
//~^ ERROR calls in constants are limited to constant functions
|
//~^ ERROR calls in constants are limited to constant functions
|
||||||
|
//~| ERROR evaluation of constant value failed
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,13 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
|
||||||
LL | let _ = [0; f(2)];
|
LL | let _ = [0; f(2)];
|
||||||
| ^^^^
|
| ^^^^
|
||||||
|
|
||||||
error: aborting due to previous error
|
error[E0080]: evaluation of constant value failed
|
||||||
|
--> $DIR/const-call.rs:16:17
|
||||||
|
|
|
||||||
|
LL | let _ = [0; f(2)];
|
||||||
|
| ^^^^ calling non-const function `f`
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0015`.
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
Some errors occurred: E0015, E0080.
|
||||||
|
For more information about an error, try `rustc --explain E0015`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue