Skip to content

enable cross crate and unsafe const fn #30899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 11 additions & 58 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use util::num::ToPrimitive;
use util::nodemap::NodeMap;

use graphviz::IntoCow;
use syntax::{ast, abi};
use syntax::ast;
use rustc_front::hir::Expr;
use rustc_front::hir;
use rustc_front::intravisit::FnKind;
Expand Down Expand Up @@ -1089,19 +1089,16 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
hir::ExprCall(ref callee, ref args) => {
let sub_ty_hint = ty_hint.erase_hint();
let callee_val = try!(eval_const_expr_partial(tcx, callee, sub_ty_hint, fn_args));
let (decl, block, constness) = try!(get_fn_def(tcx, e, callee_val));
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),
}
let did = match callee_val {
Function(did) => did,
callee => signal!(e, CallOn(callee)),
};
let (decl, result) = if let Some(fn_like) = lookup_const_fn_by_id(tcx, did) {
(fn_like.decl(), &fn_like.body().expr)
} else {
signal!(e, NonConstPath)
};
let result = result.as_ref().expect("const fn has no result expression");
assert_eq!(decl.inputs.len(), args.len());

let mut call_args = NodeMap();
Expand All @@ -1116,7 +1113,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
let old = call_args.insert(arg.pat.id, arg_val);
assert!(old.is_none());
}
let result = block.expr.as_ref().unwrap();
debug!("const call({:?})", call_args);
try!(eval_const_expr_partial(tcx, &**result, ty_hint, Some(&call_args)))
},
Expand Down Expand Up @@ -1397,46 +1393,3 @@ pub fn compare_lit_exprs<'tcx>(tcx: &ty::ctxt<'tcx>,
};
compare_const_vals(&a, &b)
}


// returns Err if callee is not `Function`
// `e` is only used for error reporting/spans
fn get_fn_def<'a>(tcx: &'a ty::ctxt,
e: &hir::Expr,
callee: ConstVal)
-> Result<(&'a hir::FnDecl, &'a hir::Block, hir::Constness), ConstEvalErr> {
let did = match callee {
Function(did) => did,
callee => signal!(e, CallOn(callee)),
};
debug!("fn call: {:?}", tcx.map.get_if_local(did));
match tcx.map.get_if_local(did) {
None => signal!(e, UnimplementedConstVal("calling non-local const fn")), // non-local
Some(ast_map::NodeItem(it)) => match it.node {
hir::ItemFn(
ref decl,
hir::Unsafety::Normal,
constness,
abi::Abi::Rust,
_, // ducktype generics? types are funky in const_eval
ref block,
) => Ok((&**decl, &**block, constness)),
_ => signal!(e, NonConstPath),
},
Some(ast_map::NodeImplItem(it)) => match it.node {
hir::ImplItemKind::Method(
hir::MethodSig {
ref decl,
unsafety: hir::Unsafety::Normal,
constness,
abi: abi::Abi::Rust,
.. // ducktype generics? types are funky in const_eval
},
ref block,
) => Ok((decl, block, constness)),
_ => signal!(e, NonConstPath),
},
Some(ast_map::NodeTraitItem(..)) => signal!(e, NonConstPath),
Some(_) => signal!(e, UnimplementedConstVal("calling struct, tuple or variant")),
}
}
5 changes: 0 additions & 5 deletions src/test/compile-fail/const-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@

#![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]
}
22 changes: 0 additions & 22 deletions src/test/compile-fail/const-fn-stability-calls-2.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/test/run-pass/const-fn-cross-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ const FOO: usize = foo();

fn main() {
assert_eq!(FOO, 22);
let _: [i32; foo()] = [42; 22];
}
6 changes: 6 additions & 0 deletions src/test/run-pass/const-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ const fn sub(x: u32, y: u32) -> u32 {
x - y
}

const unsafe fn div(x: u32, y: u32) -> u32 {
x / y
}

const SUM: u32 = add(44, 22);
const DIFF: u32 = sub(44, 22);
const DIV: u32 = unsafe{div(44, 22)};

fn main() {
assert_eq!(SUM, 66);
assert!(SUM != 88);

assert_eq!(DIFF, 22);
assert_eq!(DIV, 2);

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