]> git.lizzy.rs Git - rust.git/commitdiff
CTFE: dynamically make sure we do not call non-const-fn
authorRalf Jung <post@ralfj.de>
Fri, 16 Nov 2018 17:05:08 +0000 (18:05 +0100)
committerRalf Jung <post@ralfj.de>
Fri, 16 Nov 2018 17:05:08 +0000 (18:05 +0100)
src/librustc_mir/const_eval.rs
src/test/ui/consts/const-call.rs
src/test/ui/consts/const-call.stderr

index 7bff76f948e57b8ed123684996f5d693061a51aa..51046399ec2017bed8a78d95f84f155f6fc38bd1 100644 (file)
@@ -365,13 +365,19 @@ fn find_fn(
         ret: Option<mir::BasicBlock>,
     ) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
         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
-            // that for const fn!
-            if ecx.hook_fn(instance, args, dest)? {
+            // that for const fn!  We certainly do *not* want to actually call the fn
+            // though, so be sure we return here.
+            return if ecx.hook_fn(instance, args, dest)? {
                 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.
         Ok(Some(match ecx.load_mir(instance.def) {
index 18476494300b21febeca61fd40a4b3bf37af454e..bd407192cd7a1079e98050f9bb8b997b8dcf647b 100644 (file)
@@ -15,4 +15,5 @@ fn f(x: usize) -> usize {
 fn main() {
     let _ = [0; f(2)];
     //~^ ERROR calls in constants are limited to constant functions
+    //~| ERROR evaluation of constant value failed
 }
index 81be93e916e8bf0c1fbd3a68794f89bd736d4b74..219fcec51b386f47196541a4d9d62dd2842351a4 100644 (file)
@@ -4,6 +4,13 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
 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`
+
+error: aborting due to 2 previous errors
 
-For more information about this error, try `rustc --explain E0015`.
+Some errors occurred: E0015, E0080.
+For more information about an error, try `rustc --explain E0015`.