Skip to content

Commit b8961ac

Browse files
committed
---
yaml --- r: 273124 b: refs/heads/beta c: 6992280 h: refs/heads/master
1 parent b8ea4e5 commit b8961ac

File tree

7 files changed

+66
-116
lines changed

7 files changed

+66
-116
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: a48bd1718356cdcc4204b57ed282ff8111df5427
26+
refs/heads/beta: 6992280f0003e825ad05daf6b5aed94c63e77773
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/librustc/middle/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
476476
Some(Def::AssociatedConst(did)) |
477477
Some(Def::Const(did)) => match lookup_const_by_id(self.tcx, did,
478478
Some(pat.id), None) {
479-
Some(const_expr) => {
479+
Some((const_expr, _const_ty)) => {
480480
const_expr_to_pat(self.tcx, const_expr, pat.span).map(|new_pat| {
481481

482482
if let Some(ref mut renaming_map) = self.renaming_map {

branches/beta/src/librustc/middle/const_eval.rs

Lines changed: 55 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a TyCtxt<'tcx>,
9898
def_id: DefId,
9999
maybe_ref_id: Option<ast::NodeId>,
100100
param_substs: Option<&'tcx subst::Substs<'tcx>>)
101-
-> Option<&'tcx Expr> {
101+
-> Option<(&'tcx Expr, Option<ty::Ty<'tcx>>)> {
102102
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
103103
match tcx.map.find(node_id) {
104104
None => None,
105105
Some(ast_map::NodeItem(it)) => match it.node {
106-
hir::ItemConst(_, ref const_expr) => {
107-
Some(&const_expr)
106+
hir::ItemConst(ref ty, ref const_expr) => {
107+
Some((&const_expr, ast_ty_to_prim_ty(tcx, ty)))
108108
}
109109
_ => None
110110
},
@@ -123,8 +123,7 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a TyCtxt<'tcx>,
123123
if let Some(param_substs) = param_substs {
124124
substs = substs.subst(tcx, param_substs);
125125
}
126-
resolve_trait_associated_const(tcx, ti, trait_id,
127-
substs)
126+
resolve_trait_associated_const(tcx, ti, trait_id, substs)
128127
}
129128
// Technically, without knowing anything about the
130129
// expression that generates the obligation, we could
@@ -138,25 +137,27 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a TyCtxt<'tcx>,
138137
_ => None
139138
},
140139
Some(ast_map::NodeImplItem(ii)) => match ii.node {
141-
hir::ImplItemKind::Const(_, ref expr) => {
142-
Some(&expr)
140+
hir::ImplItemKind::Const(ref ty, ref expr) => {
141+
Some((&expr, ast_ty_to_prim_ty(tcx, ty)))
143142
}
144143
_ => None
145144
},
146145
Some(_) => None
147146
}
148147
} else {
149148
match tcx.extern_const_statics.borrow().get(&def_id) {
150-
Some(&ast::DUMMY_NODE_ID) => return None,
151-
Some(&expr_id) => {
152-
return Some(tcx.map.expect_expr(expr_id));
149+
Some(&None) => return None,
150+
Some(&Some((expr_id, ty))) => {
151+
return Some((tcx.map.expect_expr(expr_id), ty));
153152
}
154153
None => {}
155154
}
156155
let mut used_ref_id = false;
157-
let expr_id = match tcx.sess.cstore.maybe_get_item_ast(tcx, def_id) {
156+
let expr_ty = match tcx.sess.cstore.maybe_get_item_ast(tcx, def_id) {
158157
cstore::FoundAst::Found(&InlinedItem::Item(ref item)) => match item.node {
159-
hir::ItemConst(_, ref const_expr) => Some(const_expr.id),
158+
hir::ItemConst(ref ty, ref const_expr) => {
159+
Some((&**const_expr, ast_ty_to_prim_ty(tcx, ty)))
160+
},
160161
_ => None
161162
},
162163
cstore::FoundAst::Found(&InlinedItem::TraitItem(trait_id, ref ti)) => match ti.node {
@@ -173,16 +174,17 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a TyCtxt<'tcx>,
173174
if let Some(param_substs) = param_substs {
174175
substs = substs.subst(tcx, param_substs);
175176
}
176-
resolve_trait_associated_const(tcx, ti, trait_id,
177-
substs).map(|e| e.id)
177+
resolve_trait_associated_const(tcx, ti, trait_id, substs)
178178
}
179179
None => None
180180
}
181181
}
182182
_ => None
183183
},
184184
cstore::FoundAst::Found(&InlinedItem::ImplItem(_, ref ii)) => match ii.node {
185-
hir::ImplItemKind::Const(_, ref expr) => Some(expr.id),
185+
hir::ImplItemKind::Const(ref ty, ref expr) => {
186+
Some((&**expr, ast_ty_to_prim_ty(tcx, ty)))
187+
},
186188
_ => None
187189
},
188190
_ => None
@@ -192,10 +194,10 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a TyCtxt<'tcx>,
192194
// lookup with the same def_id may yield a different result.
193195
if !used_ref_id {
194196
tcx.extern_const_statics
195-
.borrow_mut().insert(def_id,
196-
expr_id.unwrap_or(ast::DUMMY_NODE_ID));
197+
.borrow_mut()
198+
.insert(def_id, expr_ty.map(|(e, t)| (e.id, t)));
197199
}
198-
expr_id.map(|id| tcx.map.expect_expr(id))
200+
expr_ty
199201
}
200202
}
201203

@@ -386,7 +388,7 @@ pub fn const_expr_to_pat(tcx: &TyCtxt, expr: &Expr, span: Span) -> P<hir::Pat> {
386388
PatKind::Path(path.clone()),
387389
Some(Def::Const(def_id)) |
388390
Some(Def::AssociatedConst(def_id)) => {
389-
let expr = lookup_const_by_id(tcx, def_id, Some(expr.id), None).unwrap();
391+
let (expr, _ty) = lookup_const_by_id(tcx, def_id, Some(expr.id), None).unwrap();
390392
return const_expr_to_pat(tcx, expr, span);
391393
},
392394
_ => unreachable!(),
@@ -778,90 +780,49 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
778780
if def.depth != 0 {
779781
signal!(e, UnresolvedPath);
780782
}
781-
Some(def.full_def())
783+
def.full_def()
782784
} else {
783-
None
785+
signal!(e, NonConstPath);
784786
};
785-
let (const_expr, const_ty) = match opt_def {
786-
Some(Def::Const(def_id)) => {
787-
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
788-
match tcx.map.find(node_id) {
789-
Some(ast_map::NodeItem(it)) => match it.node {
790-
hir::ItemConst(ref ty, ref expr) => {
791-
(Some(&**expr), Some(&**ty))
792-
}
793-
_ => (None, None)
794-
},
795-
_ => (None, None)
796-
}
787+
match opt_def {
788+
Def::Const(def_id) |
789+
Def::AssociatedConst(def_id) => {
790+
let maybe_ref_id = if let ExprTypeChecked = ty_hint {
791+
Some(e.id)
792+
} else {
793+
None
794+
};
795+
if let Some((e, ty)) = lookup_const_by_id(tcx, def_id, maybe_ref_id, None) {
796+
let item_hint = match ty {
797+
Some(ty) => ty_hint.checked_or(ty),
798+
None => ty_hint,
799+
};
800+
try!(eval_const_expr_partial(tcx, e, item_hint, None))
797801
} else {
798-
(lookup_const_by_id(tcx, def_id, Some(e.id), None), None)
802+
signal!(e, NonConstPath);
799803
}
800-
}
801-
Some(Def::AssociatedConst(def_id)) => {
802-
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
803-
match impl_or_trait_container(tcx, def_id) {
804-
ty::TraitContainer(trait_id) => match tcx.map.find(node_id) {
805-
Some(ast_map::NodeTraitItem(ti)) => match ti.node {
806-
hir::ConstTraitItem(ref ty, _) => {
807-
if let ExprTypeChecked = ty_hint {
808-
let substs = tcx.node_id_item_substs(e.id).substs;
809-
(resolve_trait_associated_const(tcx,
810-
ti,
811-
trait_id,
812-
substs),
813-
Some(&**ty))
814-
} else {
815-
(None, None)
816-
}
817-
}
818-
_ => (None, None)
819-
},
820-
_ => (None, None)
821-
},
822-
ty::ImplContainer(_) => match tcx.map.find(node_id) {
823-
Some(ast_map::NodeImplItem(ii)) => match ii.node {
824-
hir::ImplItemKind::Const(ref ty, ref expr) => {
825-
(Some(&**expr), Some(&**ty))
826-
}
827-
_ => (None, None)
828-
},
829-
_ => (None, None)
830-
},
831-
}
804+
},
805+
Def::Variant(enum_def, variant_def) => {
806+
if let Some(const_expr) = lookup_variant_by_id(tcx, enum_def, variant_def) {
807+
try!(eval_const_expr_partial(tcx, const_expr, ty_hint, None))
832808
} else {
833-
(lookup_const_by_id(tcx, def_id, Some(e.id), None), None)
809+
signal!(e, NonConstPath);
834810
}
835811
}
836-
Some(Def::Variant(enum_def, variant_def)) => {
837-
(lookup_variant_by_id(tcx, enum_def, variant_def), None)
812+
Def::Struct(..) => {
813+
ConstVal::Struct(e.id)
838814
}
839-
Some(Def::Struct(..)) => {
840-
return Ok(ConstVal::Struct(e.id))
841-
}
842-
Some(Def::Local(_, id)) => {
815+
Def::Local(_, id) => {
843816
debug!("Def::Local({:?}): {:?}", id, fn_args);
844817
if let Some(val) = fn_args.and_then(|args| args.get(&id)) {
845-
return Ok(val.clone());
818+
val.clone()
846819
} else {
847-
(None, None)
820+
signal!(e, NonConstPath);
848821
}
849822
},
850-
Some(Def::Method(id)) | Some(Def::Fn(id)) => return Ok(Function(id)),
851-
_ => (None, None)
852-
};
853-
let const_expr = match const_expr {
854-
Some(actual_e) => actual_e,
855-
None => signal!(e, NonConstPath)
856-
};
857-
let item_hint = match const_ty {
858-
Some(ty) => match ast_ty_to_prim_ty(tcx, ty) {
859-
Some(ty) => ty_hint.checked_or(ty),
860-
None => ty_hint.erase_hint(),
861-
},
862-
None => ty_hint.erase_hint(),
863-
};
864-
try!(eval_const_expr_partial(tcx, const_expr, item_hint, fn_args))
823+
Def::Method(id) | Def::Fn(id) => Function(id),
824+
_ => signal!(e, NonConstPath),
825+
}
865826
}
866827
hir::ExprCall(ref callee, ref args) => {
867828
let sub_ty_hint = ty_hint.erase_hint();
@@ -1097,28 +1058,11 @@ fn infer<'tcx>(
10971058
}
10981059
}
10991060

1100-
fn impl_or_trait_container(tcx: &TyCtxt, def_id: DefId) -> ty::ImplOrTraitItemContainer {
1101-
// This is intended to be equivalent to tcx.impl_or_trait_item(def_id).container()
1102-
// for local def_id, but it can be called before tcx.impl_or_trait_items is complete.
1103-
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
1104-
if let Some(ast_map::NodeItem(item)) = tcx.map.find(tcx.map.get_parent_node(node_id)) {
1105-
let container_id = tcx.map.local_def_id(item.id);
1106-
match item.node {
1107-
hir::ItemImpl(..) => return ty::ImplContainer(container_id),
1108-
hir::ItemTrait(..) => return ty::TraitContainer(container_id),
1109-
_ => ()
1110-
}
1111-
}
1112-
panic!("No impl or trait container for {:?}", def_id);
1113-
}
1114-
panic!("{:?} is not local", def_id);
1115-
}
1116-
11171061
fn resolve_trait_associated_const<'a, 'tcx: 'a>(tcx: &'a TyCtxt<'tcx>,
11181062
ti: &'tcx hir::TraitItem,
11191063
trait_id: DefId,
11201064
rcvr_substs: subst::Substs<'tcx>)
1121-
-> Option<&'tcx Expr>
1065+
-> Option<(&'tcx Expr, Option<ty::Ty<'tcx>>)>
11221066
{
11231067
let trait_ref = ty::Binder(
11241068
rcvr_substs.erase_regions().to_trait_ref(tcx, trait_id)
@@ -1151,7 +1095,9 @@ fn resolve_trait_associated_const<'a, 'tcx: 'a>(tcx: &'a TyCtxt<'tcx>,
11511095
.iter().find(|ic| ic.name == ti.name) {
11521096
Some(ic) => lookup_const_by_id(tcx, ic.def_id, None, None),
11531097
None => match ti.node {
1154-
hir::ConstTraitItem(_, Some(ref expr)) => Some(&*expr),
1098+
hir::ConstTraitItem(ref ty, Some(ref expr)) => {
1099+
Some((&*expr, ast_ty_to_prim_ty(tcx, ty)))
1100+
},
11551101
_ => None,
11561102
},
11571103
}

branches/beta/src/librustc/middle/ty/context.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,12 @@ pub struct TyCtxt<'tcx> {
342342
/// FIXME(arielb1): why is this separate from populated_external_types?
343343
pub populated_external_primitive_impls: RefCell<DefIdSet>,
344344

345-
/// These caches are used by const_eval when decoding external constants.
346-
pub extern_const_statics: RefCell<DefIdMap<NodeId>>,
345+
/// Cache used by const_eval when decoding external constants.
346+
/// Contains `None` when the constant has been fetched but doesn't exist.
347+
/// Constains `Some(expr_id, type)` otherwise.
348+
/// `type` is `None` in case it's not a primitive type
349+
pub extern_const_statics: RefCell<DefIdMap<Option<(NodeId, Option<Ty<'tcx>>)>>>,
350+
/// Cache used by const_eval when decoding extern const fns
347351
pub extern_const_fns: RefCell<DefIdMap<NodeId>>,
348352

349353
pub node_lint_levels: RefCell<FnvHashMap<(NodeId, lint::LintId),

branches/beta/src/librustc_mir/hair/cx/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
8787
Def::Const(def_id) | Def::AssociatedConst(def_id) =>
8888
match const_eval::lookup_const_by_id(self.cx.tcx, def_id,
8989
Some(pat.id), None) {
90-
Some(const_expr) => {
90+
Some((const_expr, _const_ty)) => {
9191
let pat = const_eval::const_expr_to_pat(self.cx.tcx, const_expr,
9292
pat.span);
9393
return self.to_pattern(&pat);

branches/beta/src/librustc_passes/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
604604
}
605605
Some(Def::Const(did)) |
606606
Some(Def::AssociatedConst(did)) => {
607-
if let Some(expr) = const_eval::lookup_const_by_id(v.tcx, did,
607+
if let Some((expr, _ty)) = const_eval::lookup_const_by_id(v.tcx, did,
608608
Some(e.id),
609609
None) {
610610
let inner = v.global_expr(Mode::Const, expr);

branches/beta/src/librustc_trans/trans/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub fn get_const_expr<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
227227
}
228228

229229
match const_eval::lookup_const_by_id(ccx.tcx(), def_id, Some(ref_expr.id), Some(param_substs)) {
230-
Some(ref expr) => expr,
230+
Some((ref expr, _ty)) => expr,
231231
None => {
232232
ccx.sess().span_bug(ref_expr.span, "constant item not found")
233233
}

0 commit comments

Comments
 (0)