Skip to content

Commit 82011e1

Browse files
committed
Structural changes for associated constants
Introduces new variants and types in syntax::ast, middle::ty, and middle::def.
1 parent 215127c commit 82011e1

40 files changed

+414
-242
lines changed

src/librustc/metadata/decoder.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,8 @@ pub fn get_provided_trait_methods<'tcx>(intr: Rc<IdentInterner>,
952952
cdata,
953953
did.node,
954954
tcx);
955-
match trait_item {
956-
ty::MethodTraitItem(ref method) => {
957-
result.push((*method).clone())
958-
}
959-
ty::TypeTraitItem(_) => {}
955+
if let ty::MethodTraitItem(ref method) = trait_item {
956+
result.push((*method).clone())
960957
}
961958
}
962959
true

src/librustc/metadata/encoder.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,11 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext,
378378
let impl_item = ty::impl_or_trait_item(
379379
ecx.tcx,
380380
method_did.def_id());
381-
match impl_item {
382-
ty::MethodTraitItem(ref m) => {
383-
encode_reexported_static_method(rbml_w,
384-
exp,
385-
m.def_id,
386-
m.name);
387-
}
388-
ty::TypeTraitItem(_) => {}
381+
if let ty::MethodTraitItem(ref m) = impl_item {
382+
encode_reexported_static_method(rbml_w,
383+
exp,
384+
m.def_id,
385+
m.name);
389386
}
390387
}
391388
}
@@ -1195,6 +1192,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11951192
for &item_def_id in items {
11961193
rbml_w.start_tag(tag_item_impl_item);
11971194
match item_def_id {
1195+
ty::ConstTraitItemId(_) => {}
11981196
ty::MethodTraitItemId(item_def_id) => {
11991197
encode_def_id(rbml_w, item_def_id);
12001198
encode_item_sort(rbml_w, 'r');
@@ -1232,6 +1230,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12321230
});
12331231

12341232
match ty::impl_or_trait_item(tcx, trait_item_def_id.def_id()) {
1233+
ty::ConstTraitItem(_) => {}
12351234
ty::MethodTraitItem(ref method_type) => {
12361235
encode_info_for_method(ecx,
12371236
rbml_w,
@@ -1276,6 +1275,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12761275
for &method_def_id in &*ty::trait_item_def_ids(tcx, def_id) {
12771276
rbml_w.start_tag(tag_item_trait_item);
12781277
match method_def_id {
1278+
ty::ConstTraitItemId(_) => {}
12791279
ty::MethodTraitItemId(method_def_id) => {
12801280
encode_def_id(rbml_w, method_def_id);
12811281
encode_item_sort(rbml_w, 'r');
@@ -1321,6 +1321,9 @@ fn encode_info_for_item(ecx: &EncodeContext,
13211321
ty::impl_or_trait_item(tcx, item_def_id.def_id());
13221322
let is_nonstatic_method;
13231323
match trait_item_type {
1324+
ty::ConstTraitItem(_) => {
1325+
is_nonstatic_method = false;
1326+
}
13241327
ty::MethodTraitItem(method_ty) => {
13251328
let method_def_id = item_def_id.def_id();
13261329

@@ -1365,6 +1368,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
13651368
let trait_item = &*ms[i];
13661369
encode_attributes(rbml_w, &trait_item.attrs);
13671370
match trait_item.node {
1371+
ast::ConstTraitItem(_, _) => {}
13681372
ast::MethodTraitItem(ref sig, ref body) => {
13691373
// If this is a static method, we've already
13701374
// encoded this.

src/librustc/middle/astencode.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ impl tr for def::Def {
466466
def::DefForeignMod(did) => { def::DefForeignMod(did.tr(dcx)) }
467467
def::DefStatic(did, m) => { def::DefStatic(did.tr(dcx), m) }
468468
def::DefConst(did) => { def::DefConst(did.tr(dcx)) }
469+
def::DefAssociatedConst(did, p) => {
470+
def::DefAssociatedConst(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
471+
}
469472
def::DefLocal(nid) => { def::DefLocal(dcx.tr_id(nid)) }
470473
def::DefVariant(e_did, v_did, is_s) => {
471474
def::DefVariant(e_did.tr(dcx), v_did.tr(dcx), is_s)

src/librustc/middle/dead.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
7272
fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
7373
self.tcx.def_map.borrow().get(id).map(|def| {
7474
match def.full_def() {
75-
def::DefConst(_) => {
75+
def::DefConst(_) | def::DefAssociatedConst(..) => {
7676
self.check_def_id(def.def_id())
7777
}
7878
_ if self.ignore_non_const_paths => (),
@@ -114,14 +114,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
114114
let trait_item = ty::trait_item(self.tcx,
115115
trait_ref.def_id,
116116
index);
117-
match trait_item {
118-
ty::MethodTraitItem(method) => {
119-
self.check_def_id(method.def_id);
120-
}
121-
ty::TypeTraitItem(typedef) => {
122-
self.check_def_id(typedef.def_id);
123-
}
124-
}
117+
self.check_def_id(trait_item.def_id());
125118
}
126119
}
127120
}
@@ -365,6 +358,7 @@ impl<'v> Visitor<'v> for LifeSeeder {
365358
ast::ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
366359
for impl_item in impl_items {
367360
match impl_item.node {
361+
ast::ConstImplItem(..) => {}
368362
ast::MethodImplItem(..) => {
369363
if opt_trait.is_some() ||
370364
has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
@@ -584,6 +578,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
584578
// Overwrite so that we don't warn the trait method itself.
585579
fn visit_trait_item(&mut self, trait_method: &ast::TraitItem) {
586580
match trait_method.node {
581+
ast::ConstTraitItem(_, _) => {}
587582
ast::MethodTraitItem(_, Some(ref body)) => {
588583
visit::walk_block(self, body)
589584
}

src/librustc/middle/def.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum Def {
2828
DefForeignMod(ast::DefId),
2929
DefStatic(ast::DefId, bool /* is_mutbl */),
3030
DefConst(ast::DefId),
31+
DefAssociatedConst(ast::DefId /* const */, MethodProvenance),
3132
DefLocal(ast::NodeId),
3233
DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
3334
DefTy(ast::DefId, bool /* is_enum */),
@@ -140,7 +141,8 @@ impl Def {
140141
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
141142
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
142143
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
143-
DefMethod(id, _) | DefConst(id) | DefSelfTy(Some(id), None)=> {
144+
DefMethod(id, _) | DefConst(id) | DefAssociatedConst(id, _) |
145+
DefSelfTy(Some(id), None)=> {
144146
id
145147
}
146148
DefLocal(id) |

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl OverloadedCallType {
234234
ty::MethodTraitItem(ref method_descriptor) => {
235235
(*method_descriptor).clone()
236236
}
237-
ty::TypeTraitItem(_) => {
237+
_ => {
238238
tcx.sess.bug("overloaded call method wasn't in method map")
239239
}
240240
};
@@ -1183,6 +1183,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
11831183
}
11841184

11851185
Some(def::DefConst(..)) |
1186+
Some(def::DefAssociatedConst(..)) |
11861187
Some(def::DefLocal(..)) => {
11871188
// This is a leaf (i.e. identifier binding
11881189
// or constant value to match); thus no

src/librustc/middle/infer/error_reporting.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,8 +843,8 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
843843
Some(&sig.explicit_self.node),
844844
item.span))
845845
}
846-
ast::TypeImplItem(_) => None,
847-
ast::MacImplItem(_) => self.tcx.sess.bug("unexpanded macro")
846+
ast::MacImplItem(_) => self.tcx.sess.bug("unexpanded macro"),
847+
_ => None,
848848
}
849849
},
850850
ast_map::NodeTraitItem(item) => {
@@ -1723,8 +1723,8 @@ fn lifetimes_in_scope(tcx: &ty::ctxt,
17231723
taken.push_all(&sig.generics.lifetimes);
17241724
Some(ii.id)
17251725
}
1726-
ast::TypeImplItem(_) => None,
1727-
ast::MacImplItem(_) => tcx.sess.bug("unexpanded macro")
1726+
ast::MacImplItem(_) => tcx.sess.bug("unexpanded macro"),
1727+
_ => None,
17281728
}
17291729
}
17301730
_ => None

src/librustc/middle/mem_categorization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
589589

590590
match def {
591591
def::DefStruct(..) | def::DefVariant(..) | def::DefConst(..) |
592-
def::DefFn(..) | def::DefMethod(..) => {
592+
def::DefAssociatedConst(..) | def::DefFn(..) | def::DefMethod(..) => {
593593
Ok(self.cat_rvalue_node(id, span, expr_ty))
594594
}
595595
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
@@ -1286,7 +1286,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
12861286
try!(self.cat_pattern_(cmt_field, &**subpat, op));
12871287
}
12881288
}
1289-
Some(def::DefConst(..)) => {
1289+
Some(def::DefConst(..)) | Some(def::DefAssociatedConst(..)) => {
12901290
for subpat in subpats {
12911291
try!(self.cat_pattern_(cmt.clone(), &**subpat, op));
12921292
}

src/librustc/middle/pat_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
6262
match pat.node {
6363
ast::PatIdent(_, _, None) | ast::PatEnum(..) => {
6464
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
65-
Some(DefConst(..)) => true,
65+
Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
6666
_ => false
6767
}
6868
}

src/librustc/middle/reachable.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for ReachableContext<'a, 'tcx> {
113113
// If this path leads to a constant, then we need to
114114
// recurse into the constant to continue finding
115115
// items that are reachable.
116-
def::DefConst(..) => {
116+
def::DefConst(..) | def::DefAssociatedConst(..) => {
117117
self.worklist.push(def_id.node);
118118
}
119119

@@ -183,12 +183,14 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
183183
}
184184
Some(ast_map::NodeTraitItem(trait_method)) => {
185185
match trait_method.node {
186+
ast::ConstTraitItem(_, ref default) => default.is_some(),
186187
ast::MethodTraitItem(_, ref body) => body.is_some(),
187188
ast::TypeTraitItem(..) => false,
188189
}
189190
}
190191
Some(ast_map::NodeImplItem(impl_item)) => {
191192
match impl_item.node {
193+
ast::ConstImplItem(..) => true,
192194
ast::MethodImplItem(ref sig, _) => {
193195
if generics_require_inlining(&sig.generics) ||
194196
attr::requests_inline(&impl_item.attrs) {
@@ -303,9 +305,13 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
303305
}
304306
ast_map::NodeTraitItem(trait_method) => {
305307
match trait_method.node {
308+
ast::ConstTraitItem(_, None) |
306309
ast::MethodTraitItem(_, None) => {
307310
// Keep going, nothing to get exported
308311
}
312+
ast::ConstTraitItem(_, Some(ref expr)) => {
313+
self.visit_expr(&*expr);
314+
}
309315
ast::MethodTraitItem(_, Some(ref body)) => {
310316
visit::walk_block(self, body);
311317
}
@@ -314,6 +320,9 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
314320
}
315321
ast_map::NodeImplItem(impl_item) => {
316322
match impl_item.node {
323+
ast::ConstImplItem(_, ref expr) => {
324+
self.visit_expr(&*expr);
325+
}
317326
ast::MethodImplItem(ref sig, ref body) => {
318327
let did = self.tcx.map.get_parent_did(search_item);
319328
if method_might_be_inlined(self.tcx, sig, impl_item, did) {

src/librustc/middle/traits/object_safety.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ fn object_safety_violations_for_trait<'tcx>(tcx: &ty::ctxt<'tcx>,
100100
.map(|code| ObjectSafetyViolation::Method(m.clone(), code))
101101
.into_iter()
102102
}
103-
ty::TypeTraitItem(_) => {
104-
None.into_iter()
105-
}
103+
_ => None.into_iter(),
106104
}
107105
})
108106
.collect();

src/librustc/middle/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ fn confirm_impl_candidate<'cx,'tcx>(
863863
for impl_item in impl_items {
864864
let assoc_type = match *impl_or_trait_items_map.get(&impl_item.def_id()).unwrap() {
865865
ty::TypeTraitItem(ref assoc_type) => assoc_type.clone(),
866-
ty::MethodTraitItem(..) => { continue; }
866+
ty::ConstTraitItem(..) | ty::MethodTraitItem(..) => { continue; }
867867
};
868868

869869
if assoc_type.name != obligation.predicate.item_name {

src/librustc/middle/traits/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ pub fn get_vtable_index_of_object_method<'tcx>(tcx: &ty::ctxt<'tcx>,
434434
for trait_item in &**trait_items {
435435
match *trait_item {
436436
ty::MethodTraitItem(_) => method_count += 1,
437-
ty::TypeTraitItem(_) => {}
437+
_ => {}
438438
}
439439
}
440440
}
@@ -445,14 +445,14 @@ pub fn get_vtable_index_of_object_method<'tcx>(tcx: &ty::ctxt<'tcx>,
445445
for trait_item in trait_items.iter().take(method_offset_in_trait) {
446446
match *trait_item {
447447
ty::MethodTraitItem(_) => method_count += 1,
448-
ty::TypeTraitItem(_) => {}
448+
_ => {}
449449
}
450450
}
451451

452452
// the item at the offset we were given really ought to be a method
453453
assert!(match trait_items[method_offset_in_trait] {
454454
ty::MethodTraitItem(_) => true,
455-
ty::TypeTraitItem(_) => false
455+
_ => false
456456
});
457457

458458
method_count

0 commit comments

Comments
 (0)