Skip to content

Commit dc138b3

Browse files
committed
use DefId's in const eval for cross-crate const fn's
1 parent ac919fc commit dc138b3

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

src/librustc_const_eval/eval.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc::ty::util::IntTypeExt;
2727
use rustc::ty::subst::Substs;
2828
use rustc::traits::Reveal;
2929
use rustc::util::common::ErrorReported;
30-
use rustc::util::nodemap::NodeMap;
30+
use rustc::util::nodemap::DefIdMap;
3131
use rustc::lint;
3232

3333
use graphviz::IntoCow;
@@ -413,7 +413,7 @@ pub fn eval_const_expr_checked<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
413413
eval_const_expr_partial(tcx, e, ExprTypeChecked, None)
414414
}
415415

416-
pub type FnArgMap<'a> = Option<&'a NodeMap<ConstVal>>;
416+
pub type FnArgMap<'a> = Option<&'a DefIdMap<ConstVal>>;
417417

418418
#[derive(Clone, Debug)]
419419
pub struct ConstEvalErr {
@@ -835,9 +835,8 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
835835
ConstVal::Struct(e.id)
836836
}
837837
Def::Local(def_id) => {
838-
let id = tcx.map.as_local_node_id(def_id).unwrap();
839-
debug!("Def::Local({:?}): {:?}", id, fn_args);
840-
if let Some(val) = fn_args.and_then(|args| args.get(&id)) {
838+
debug!("Def::Local({:?}): {:?}", def_id, fn_args);
839+
if let Some(val) = fn_args.and_then(|args| args.get(&def_id)) {
841840
val.clone()
842841
} else {
843842
signal!(e, NonConstPath);
@@ -863,7 +862,7 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
863862
let result = result.as_ref().expect("const fn has no result expression");
864863
assert_eq!(decl.inputs.len(), args.len());
865864

866-
let mut call_args = NodeMap();
865+
let mut call_args = DefIdMap();
867866
for (arg, arg_expr) in decl.inputs.iter().zip(args.iter()) {
868867
let arg_hint = ty_hint.erase_hint();
869868
let arg_val = eval_const_expr_partial(
@@ -873,7 +872,7 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
873872
fn_args
874873
)?;
875874
debug!("const call arg: {:?}", arg);
876-
let old = call_args.insert(arg.pat.id, arg_val);
875+
let old = call_args.insert(tcx.expect_def(arg.pat.id).def_id(), arg_val);
877876
assert!(old.is_none());
878877
}
879878
debug!("const call({:?})", call_args);
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 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+
#![crate_type = "lib"]
13+
14+
const fn foo(i: i32) -> i32 {
15+
i
16+
}
17+
18+
pub const FOO: i32 = foo(1);

src/test/run-pass/issue-36954.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 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+
// aux-build:issue-36954.rs
12+
13+
extern crate issue_36954 as lib;
14+
15+
fn main() {
16+
let _ = lib::FOO;
17+
}

0 commit comments

Comments
 (0)