about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-20 09:48:11 +0000
committerbors <bors@rust-lang.org>2015-11-20 09:48:11 +0000
commitf8827f527560a5ddea50a213440a89d3bff2bfea (patch)
treef703c0ecc1caaa71c19eee6ee94cdd84ea1d99e9
parent2228bacd62ca8970a7a59401e78d0c5a34fc0f87 (diff)
parentb9eaceebb2821e1a5abfba1e9b7a85911879f717 (diff)
downloadrust-f8827f527560a5ddea50a213440a89d3bff2bfea.tar.gz
rust-f8827f527560a5ddea50a213440a89d3bff2bfea.zip
Auto merge of #29534 - oli-obk:fix/const_fn_eval, r=dotdash
-rw-r--r--src/librustc/middle/const_eval.rs29
-rw-r--r--src/test/compile-fail/const-call.rs24
2 files changed, 38 insertions, 15 deletions
diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs
index 039c62a904c..e43d1628743 100644
--- a/src/librustc/middle/const_eval.rs
+++ b/src/librustc/middle/const_eval.rs
@@ -1055,8 +1055,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
           };
           let (
               decl,
-              unsafety,
-              abi,
               block,
               constness,
           ) = match try!(eval_const_expr_partial(tcx, callee, sub_ty_hint, fn_args)) {
@@ -1065,12 +1063,12 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
                       Some(ast_map::NodeItem(it)) => match it.node {
                           hir::ItemFn(
                               ref decl,
-                              unsafety,
+                              hir::Unsafety::Normal,
                               constness,
-                              abi,
+                              abi::Abi::Rust,
                               _, // ducktype generics? types are funky in const_eval
                               ref block,
-                          ) => (decl, unsafety, abi, block, constness),
+                          ) => (decl, block, constness),
                           _ => signal!(e, NonConstPath),
                       },
                       _ => signal!(e, NonConstPath),
@@ -1080,18 +1078,19 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
               },
               _ => signal!(e, NonConstPath),
           };
-          if let ExprTypeChecked = ty_hint {
-              // no need to check for constness... either check_const
-              // already forbids this or we const eval over whatever
-              // we want
-          } else {
-              // we don't know much about the function, so we force it to be a const fn
-              // so compilation will fail later in case the const fn's body is not const
-              assert_eq!(constness, hir::Constness::Const)
+          match (ty_hint, constness) {
+              (ExprTypeChecked, _) => {
+                  // no need to check for constness... either check_const
+                  // already forbids this or we const eval over whatever
+                  // we want
+              },
+              (_, hir::Constness::Const) => {
+                  // we don't know much about the function, so we force it to be a const fn
+                  // so compilation will fail later in case the const fn's body is not const
+              },
+              _ => signal!(e, NonConstPath),
           }
           assert_eq!(decl.inputs.len(), args.len());
-          assert_eq!(unsafety, hir::Unsafety::Normal);
-          assert_eq!(abi, abi::Abi::Rust);
 
           let mut call_args = NodeMap();
           for (arg, arg_expr) in decl.inputs.iter().zip(args.iter()) {
diff --git a/src/test/compile-fail/const-call.rs b/src/test/compile-fail/const-call.rs
new file mode 100644
index 00000000000..d49da47a87c
--- /dev/null
+++ b/src/test/compile-fail/const-call.rs
@@ -0,0 +1,24 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(const_fn)]
+
+const unsafe fn g(x: usize) -> usize {
+    x
+}
+
+fn f(x: usize) -> usize {
+    x
+}
+
+fn main() {
+    let _ = [0; f(2)]; //~ ERROR: non-constant path in constant expression [E0307]
+    let _ = [0; g(2)]; //~ ERROR: non-constant path in constant expression [E0307]
+}