Skip to content

Commit f8827f5

Browse files
committed
Auto merge of #29534 - oli-obk:fix/const_fn_eval, r=dotdash
2 parents 2228bac + b9eacee commit f8827f5

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
10551055
};
10561056
let (
10571057
decl,
1058-
unsafety,
1059-
abi,
10601058
block,
10611059
constness,
10621060
) = 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>,
10651063
Some(ast_map::NodeItem(it)) => match it.node {
10661064
hir::ItemFn(
10671065
ref decl,
1068-
unsafety,
1066+
hir::Unsafety::Normal,
10691067
constness,
1070-
abi,
1068+
abi::Abi::Rust,
10711069
_, // ducktype generics? types are funky in const_eval
10721070
ref block,
1073-
) => (decl, unsafety, abi, block, constness),
1071+
) => (decl, block, constness),
10741072
_ => signal!(e, NonConstPath),
10751073
},
10761074
_ => signal!(e, NonConstPath),
@@ -1080,18 +1078,19 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
10801078
},
10811079
_ => signal!(e, NonConstPath),
10821080
};
1083-
if let ExprTypeChecked = ty_hint {
1084-
// no need to check for constness... either check_const
1085-
// already forbids this or we const eval over whatever
1086-
// we want
1087-
} else {
1088-
// we don't know much about the function, so we force it to be a const fn
1089-
// so compilation will fail later in case the const fn's body is not const
1090-
assert_eq!(constness, hir::Constness::Const)
1081+
match (ty_hint, constness) {
1082+
(ExprTypeChecked, _) => {
1083+
// no need to check for constness... either check_const
1084+
// already forbids this or we const eval over whatever
1085+
// we want
1086+
},
1087+
(_, hir::Constness::Const) => {
1088+
// we don't know much about the function, so we force it to be a const fn
1089+
// so compilation will fail later in case the const fn's body is not const
1090+
},
1091+
_ => signal!(e, NonConstPath),
10911092
}
10921093
assert_eq!(decl.inputs.len(), args.len());
1093-
assert_eq!(unsafety, hir::Unsafety::Normal);
1094-
assert_eq!(abi, abi::Abi::Rust);
10951094

10961095
let mut call_args = NodeMap();
10971096
for (arg, arg_expr) in decl.inputs.iter().zip(args.iter()) {

src/test/compile-fail/const-call.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(const_fn)]
12+
13+
const unsafe fn g(x: usize) -> usize {
14+
x
15+
}
16+
17+
fn f(x: usize) -> usize {
18+
x
19+
}
20+
21+
fn main() {
22+
let _ = [0; f(2)]; //~ ERROR: non-constant path in constant expression [E0307]
23+
let _ = [0; g(2)]; //~ ERROR: non-constant path in constant expression [E0307]
24+
}

0 commit comments

Comments
 (0)