Skip to content

Commit f2c0ecd

Browse files
oli-obkOliver 'ker' Schneider
authored and
Oliver 'ker' Schneider
committed
enable cross crate and unsafe const fn
1 parent bff5292 commit f2c0ecd

File tree

5 files changed

+18
-85
lines changed

5 files changed

+18
-85
lines changed

src/librustc/middle/const_eval.rs

+11-58
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use util::num::ToPrimitive;
2727
use util::nodemap::NodeMap;
2828

2929
use graphviz::IntoCow;
30-
use syntax::{ast, abi};
30+
use syntax::ast;
3131
use rustc_front::hir::Expr;
3232
use rustc_front::hir;
3333
use rustc_front::intravisit::FnKind;
@@ -1089,19 +1089,16 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
10891089
hir::ExprCall(ref callee, ref args) => {
10901090
let sub_ty_hint = ty_hint.erase_hint();
10911091
let callee_val = try!(eval_const_expr_partial(tcx, callee, sub_ty_hint, fn_args));
1092-
let (decl, block, constness) = try!(get_fn_def(tcx, e, callee_val));
1093-
match (ty_hint, constness) {
1094-
(ExprTypeChecked, _) => {
1095-
// no need to check for constness... either check_const
1096-
// already forbids this or we const eval over whatever
1097-
// we want
1098-
},
1099-
(_, hir::Constness::Const) => {
1100-
// we don't know much about the function, so we force it to be a const fn
1101-
// so compilation will fail later in case the const fn's body is not const
1102-
},
1103-
_ => signal!(e, NonConstPath),
1104-
}
1092+
let did = match callee_val {
1093+
Function(did) => did,
1094+
callee => signal!(e, CallOn(callee)),
1095+
};
1096+
let (decl, result) = if let Some(fn_like) = lookup_const_fn_by_id(tcx, did) {
1097+
(fn_like.decl(), &fn_like.body().expr)
1098+
} else {
1099+
signal!(e, NonConstPath)
1100+
};
1101+
let result = result.as_ref().expect("const fn has no result expression");
11051102
assert_eq!(decl.inputs.len(), args.len());
11061103

11071104
let mut call_args = NodeMap();
@@ -1116,7 +1113,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
11161113
let old = call_args.insert(arg.pat.id, arg_val);
11171114
assert!(old.is_none());
11181115
}
1119-
let result = block.expr.as_ref().unwrap();
11201116
debug!("const call({:?})", call_args);
11211117
try!(eval_const_expr_partial(tcx, &**result, ty_hint, Some(&call_args)))
11221118
},
@@ -1397,46 +1393,3 @@ pub fn compare_lit_exprs<'tcx>(tcx: &ty::ctxt<'tcx>,
13971393
};
13981394
compare_const_vals(&a, &b)
13991395
}
1400-
1401-
1402-
// returns Err if callee is not `Function`
1403-
// `e` is only used for error reporting/spans
1404-
fn get_fn_def<'a>(tcx: &'a ty::ctxt,
1405-
e: &hir::Expr,
1406-
callee: ConstVal)
1407-
-> Result<(&'a hir::FnDecl, &'a hir::Block, hir::Constness), ConstEvalErr> {
1408-
let did = match callee {
1409-
Function(did) => did,
1410-
callee => signal!(e, CallOn(callee)),
1411-
};
1412-
debug!("fn call: {:?}", tcx.map.get_if_local(did));
1413-
match tcx.map.get_if_local(did) {
1414-
None => signal!(e, UnimplementedConstVal("calling non-local const fn")), // non-local
1415-
Some(ast_map::NodeItem(it)) => match it.node {
1416-
hir::ItemFn(
1417-
ref decl,
1418-
hir::Unsafety::Normal,
1419-
constness,
1420-
abi::Abi::Rust,
1421-
_, // ducktype generics? types are funky in const_eval
1422-
ref block,
1423-
) => Ok((&**decl, &**block, constness)),
1424-
_ => signal!(e, NonConstPath),
1425-
},
1426-
Some(ast_map::NodeImplItem(it)) => match it.node {
1427-
hir::ImplItemKind::Method(
1428-
hir::MethodSig {
1429-
ref decl,
1430-
unsafety: hir::Unsafety::Normal,
1431-
constness,
1432-
abi: abi::Abi::Rust,
1433-
.. // ducktype generics? types are funky in const_eval
1434-
},
1435-
ref block,
1436-
) => Ok((decl, block, constness)),
1437-
_ => signal!(e, NonConstPath),
1438-
},
1439-
Some(ast_map::NodeTraitItem(..)) => signal!(e, NonConstPath),
1440-
Some(_) => signal!(e, UnimplementedConstVal("calling struct, tuple or variant")),
1441-
}
1442-
}

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

-5
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@
1010

1111
#![feature(const_fn)]
1212

13-
const unsafe fn g(x: usize) -> usize {
14-
x
15-
}
16-
1713
fn f(x: usize) -> usize {
1814
x
1915
}
2016

2117
fn main() {
2218
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]
2419
}

src/test/compile-fail/const-fn-stability-calls-2.rs

-22
This file was deleted.

src/test/run-pass/const-fn-cross-crate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ const FOO: usize = foo();
2222

2323
fn main() {
2424
assert_eq!(FOO, 22);
25+
let _: [i32; foo()] = [42; 22];
2526
}

src/test/run-pass/const-fn.rs

+6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ const fn sub(x: u32, y: u32) -> u32 {
2020
x - y
2121
}
2222

23+
const unsafe fn div(x: u32, y: u32) -> u32 {
24+
x / y
25+
}
26+
2327
const SUM: u32 = add(44, 22);
2428
const DIFF: u32 = sub(44, 22);
29+
const DIV: u32 = unsafe{div(44, 22)};
2530

2631
fn main() {
2732
assert_eq!(SUM, 66);
2833
assert!(SUM != 88);
2934

3035
assert_eq!(DIFF, 22);
36+
assert_eq!(DIV, 2);
3137

3238
let _: [&'static str; sub(100, 99) as usize] = ["hi"];
3339
}

0 commit comments

Comments
 (0)