Skip to content

Commit 0c59543

Browse files
committed
Structural changes for associated constants
Introduces new variants and types in syntax::ast, middle::ty, and middle::def.
1 parent 6cf3b0b commit 0c59543

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+391
-219
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
@@ -379,14 +379,11 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext,
379379
let impl_item = ty::impl_or_trait_item(
380380
ecx.tcx,
381381
method_did.def_id());
382-
match impl_item {
383-
ty::MethodTraitItem(ref m) => {
384-
encode_reexported_static_method(rbml_w,
385-
exp,
386-
m.def_id,
387-
m.name);
388-
}
389-
ty::TypeTraitItem(_) => {}
382+
if let ty::MethodTraitItem(ref m) = impl_item {
383+
encode_reexported_static_method(rbml_w,
384+
exp,
385+
m.def_id,
386+
m.name);
390387
}
391388
}
392389
}
@@ -1196,6 +1193,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11961193
for &item_def_id in items {
11971194
rbml_w.start_tag(tag_item_impl_item);
11981195
match item_def_id {
1196+
ty::ConstTraitItemId(_) => {}
11991197
ty::MethodTraitItemId(item_def_id) => {
12001198
encode_def_id(rbml_w, item_def_id);
12011199
encode_item_sort(rbml_w, 'r');
@@ -1233,6 +1231,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12331231
});
12341232

12351233
match ty::impl_or_trait_item(tcx, trait_item_def_id.def_id()) {
1234+
ty::ConstTraitItem(_) => {}
12361235
ty::MethodTraitItem(ref method_type) => {
12371236
encode_info_for_method(ecx,
12381237
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
@@ -462,6 +462,9 @@ impl tr for def::Def {
462462
def::DefForeignMod(did) => { def::DefForeignMod(did.tr(dcx)) }
463463
def::DefStatic(did, m) => { def::DefStatic(did.tr(dcx), m) }
464464
def::DefConst(did) => { def::DefConst(did.tr(dcx)) }
465+
def::DefAssociatedConst(did, p) => {
466+
def::DefAssociatedConst(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
467+
}
465468
def::DefLocal(nid) => { def::DefLocal(dcx.tr_id(nid)) }
466469
def::DefVariant(e_did, v_did, is_s) => {
467470
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) {
@@ -579,6 +573,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
579573
// Overwrite so that we don't warn the trait method itself.
580574
fn visit_trait_item(&mut self, trait_method: &ast::TraitItem) {
581575
match trait_method.node {
576+
ast::ConstTraitItem(_, _) => {}
582577
ast::MethodTraitItem(_, Some(ref body)) => {
583578
visit::walk_block(self, body)
584579
}

src/librustc/middle/def.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub enum Def {
2727
DefForeignMod(ast::DefId),
2828
DefStatic(ast::DefId, bool /* is_mutbl */),
2929
DefConst(ast::DefId),
30+
DefAssociatedConst(ast::DefId /* const */, MethodProvenance),
3031
DefLocal(ast::NodeId),
3132
DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
3233
DefTy(ast::DefId, bool /* is_enum */),
@@ -128,7 +129,7 @@ impl Def {
128129
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
129130
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
130131
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
131-
DefMethod(id, _) | DefConst(id) => {
132+
DefMethod(id, _) | DefConst(id) | DefAssociatedConst(id, _) => {
132133
id
133134
}
134135
DefLocal(id) |

src/librustc/middle/expr_use_visitor.rs

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

11291129
Some(def::DefConst(..)) |
1130+
Some(def::DefAssociatedConst(..)) |
11301131
Some(def::DefLocal(..)) => {
11311132
// This is a leaf (i.e. identifier binding
11321133
// 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
@@ -842,8 +842,8 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
842842
Some(&sig.explicit_self.node),
843843
item.span))
844844
}
845-
ast::TypeImplItem(_) => None,
846-
ast::MacImplItem(_) => self.tcx.sess.bug("unexpanded macro")
845+
ast::MacImplItem(_) => self.tcx.sess.bug("unexpanded macro"),
846+
_ => None,
847847
}
848848
},
849849
ast_map::NodeTraitItem(item) => {
@@ -1722,8 +1722,8 @@ fn lifetimes_in_scope(tcx: &ty::ctxt,
17221722
taken.push_all(&sig.generics.lifetimes);
17231723
Some(ii.id)
17241724
}
1725-
ast::TypeImplItem(_) => None,
1726-
ast::MacImplItem(_) => tcx.sess.bug("unexpanded macro")
1725+
ast::MacImplItem(_) => tcx.sess.bug("unexpanded macro"),
1726+
_ => None,
17271727
}
17281728
}
17291729
_ => None

src/librustc/middle/mem_categorization.rs

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

577577
match def {
578578
def::DefStruct(..) | def::DefVariant(..) | def::DefConst(..) |
579-
def::DefFn(..) | def::DefMethod(..) => {
579+
def::DefAssociatedConst(..) | def::DefFn(..) | def::DefMethod(..) => {
580580
Ok(self.cat_rvalue_node(id, span, expr_ty))
581581
}
582582
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
@@ -1247,7 +1247,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
12471247
try!(self.cat_pattern_(cmt_field, &**subpat, op));
12481248
}
12491249
}
1250-
Some(def::DefConst(..)) => {
1250+
Some(def::DefConst(..)) | Some(def::DefAssociatedConst(..)) => {
12511251
for subpat in subpats {
12521252
try!(self.cat_pattern_(cmt.clone(), &**subpat, op));
12531253
}

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
@@ -862,7 +862,7 @@ fn confirm_impl_candidate<'cx,'tcx>(
862862
for impl_item in impl_items {
863863
let assoc_type = match *impl_or_trait_items_map.get(&impl_item.def_id()).unwrap() {
864864
ty::TypeTraitItem(ref assoc_type) => assoc_type.clone(),
865-
ty::MethodTraitItem(..) => { continue; }
865+
ty::ConstTraitItem(..) | ty::MethodTraitItem(..) => { continue; }
866866
};
867867

868868
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
@@ -435,7 +435,7 @@ pub fn get_vtable_index_of_object_method<'tcx>(tcx: &ty::ctxt<'tcx>,
435435
for trait_item in &**trait_items {
436436
match *trait_item {
437437
ty::MethodTraitItem(_) => method_count += 1,
438-
ty::TypeTraitItem(_) => {}
438+
_ => {}
439439
}
440440
}
441441
}
@@ -446,14 +446,14 @@ pub fn get_vtable_index_of_object_method<'tcx>(tcx: &ty::ctxt<'tcx>,
446446
for trait_item in trait_items.iter().take(method_offset_in_trait) {
447447
match *trait_item {
448448
ty::MethodTraitItem(_) => method_count += 1,
449-
ty::TypeTraitItem(_) => {}
449+
_ => {}
450450
}
451451
}
452452

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

459459
method_count

0 commit comments

Comments
 (0)