diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2016-06-03 23:15:00 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2016-06-10 01:03:54 +0300 |
| commit | ee4e55398b39509bde4cc4983d615dbde406355c (patch) | |
| tree | 1b5f331cb8fd834678574e19a5b0dc9308b7fd85 /src/librustc_const_eval/eval.rs | |
| parent | 4c30f6405c80fe2cd5f1789838ded06020a50457 (diff) | |
| download | rust-ee4e55398b39509bde4cc4983d615dbde406355c.tar.gz rust-ee4e55398b39509bde4cc4983d615dbde406355c.zip | |
Introduce TyCtxt::expect_def/expect_resolution helpers and use them where possible
Diffstat (limited to 'src/librustc_const_eval/eval.rs')
| -rw-r--r-- | src/librustc_const_eval/eval.rs | 39 |
1 files changed, 14 insertions, 25 deletions
diff --git a/src/librustc_const_eval/eval.rs b/src/librustc_const_eval/eval.rs index 5637b44335e..c3db252584c 100644 --- a/src/librustc_const_eval/eval.rs +++ b/src/librustc_const_eval/eval.rs @@ -19,7 +19,7 @@ use rustc::hir::map as ast_map; use rustc::hir::map::blocks::FnLikeNode; use rustc::middle::cstore::{self, InlinedItem}; use rustc::traits; -use rustc::hir::def::Def; +use rustc::hir::def::{Def, PathResolution}; use rustc::hir::def_id::DefId; use rustc::hir::pat_util::def_to_path; use rustc::ty::{self, Ty, TyCtxt, subst}; @@ -276,11 +276,11 @@ pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, .collect()), None), hir::ExprCall(ref callee, ref args) => { - let def = *tcx.def_map.borrow().get(&callee.id).unwrap(); + let def = tcx.expect_def(callee.id); if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) { - entry.insert(def); + entry.insert(PathResolution::new(def)); } - let path = match def.full_def() { + let path = match def { Def::Struct(def_id) => def_to_path(tcx, def_id), Def::Variant(_, variant_did) => def_to_path(tcx, variant_did), Def::Fn(..) | Def::Method(..) => return Ok(P(hir::Pat { @@ -322,12 +322,9 @@ pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, } hir::ExprPath(_, ref path) => { - let opt_def = tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()); - match opt_def { - Some(Def::Struct(..)) | Some(Def::Variant(..)) => - PatKind::Path(path.clone()), - Some(Def::Const(def_id)) | - Some(Def::AssociatedConst(def_id)) => { + match tcx.expect_def(expr.id) { + Def::Struct(..) | Def::Variant(..) => PatKind::Path(path.clone()), + Def::Const(def_id) | Def::AssociatedConst(def_id) => { let substs = Some(tcx.node_id_item_substs(expr.id).substs); let (expr, _ty) = lookup_const_by_id(tcx, def_id, substs).unwrap(); return const_expr_to_pat(tcx, expr, pat_id, span); @@ -714,21 +711,13 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, } } hir::ExprPath(..) => { - let opt_def = if let Some(def) = tcx.def_map.borrow().get(&e.id) { - // After type-checking, def_map contains definition of the - // item referred to by the path. During type-checking, it - // can contain the raw output of path resolution, which - // might be a partially resolved path. - // FIXME: There's probably a better way to make sure we don't - // panic here. - if def.depth != 0 { - signal!(e, UnresolvedPath); - } - def.full_def() - } else { - signal!(e, NonConstPath); - }; - match opt_def { + // This function can be used before type checking when not all paths are fully resolved. + // FIXME: There's probably a better way to make sure we don't panic here. + let resolution = tcx.expect_resolution(e.id); + if resolution.depth != 0 { + signal!(e, UnresolvedPath); + } + match resolution.base_def { Def::Const(def_id) | Def::AssociatedConst(def_id) => { let substs = if let ExprTypeChecked = ty_hint { |
