Skip to content

Commit dbec033

Browse files
committed
Change the data structures for tracking defaulted traits. In the tcx, we
now have a simple set of trait def-ids. During coherence, we use a separate table to track the default impls for any given trait so that we can report a nice error. This fixes various bugs in the metadata encoding that led to `ty::trait_has_default_impl` yielding the wrong values in the cross-crate case. (In particular, default impl def-ids were not included in the list of all impl def-ids; I debated fixing just that, but this approach seemed cleaner overall, since we usually treat the "defaulted" bit on traits as being a property of the trait, and now iterating over a list of impls doesn't intermingle default impls with normal impls.)
1 parent 4d716de commit dbec033

File tree

7 files changed

+51
-63
lines changed

7 files changed

+51
-63
lines changed

src/librustc/metadata/common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,5 @@ pub const tag_codemap: uint = 0xa1;
254254
pub const tag_codemap_filemap: uint = 0xa2;
255255

256256
pub const tag_item_super_predicates: uint = 0xa3;
257+
258+
pub const tag_defaulted_trait: uint = 0xa4;

src/librustc/metadata/csearch.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ pub fn is_associated_type(cstore: &cstore::CStore, def: ast::DefId) -> bool {
407407
decoder::is_associated_type(&*cdata, def.node)
408408
}
409409

410-
pub fn is_default_trait(cstore: &cstore::CStore, def: ast::DefId) -> bool {
411-
let cdata = cstore.get_crate_data(def.krate);
412-
decoder::is_default_trait(&*cdata, def.node)
410+
pub fn is_defaulted_trait(cstore: &cstore::CStore, trait_def_id: ast::DefId) -> bool {
411+
let cdata = cstore.get_crate_data(trait_def_id.krate);
412+
decoder::is_defaulted_trait(&*cdata, trait_def_id.node)
413413
}

src/librustc/metadata/decoder.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1537,12 +1537,11 @@ pub fn is_associated_type(cdata: Cmd, id: ast::NodeId) -> bool {
15371537
}
15381538
}
15391539

1540-
pub fn is_default_trait<'tcx>(cdata: Cmd, id: ast::NodeId) -> bool {
1541-
let item_doc = lookup_item(id, cdata.data());
1542-
match item_family(item_doc) {
1543-
Family::DefaultImpl => true,
1544-
_ => false
1545-
}
1540+
pub fn is_defaulted_trait<'tcx>(cdata: Cmd, trait_id: ast::NodeId) -> bool {
1541+
let trait_doc = lookup_item(trait_id, cdata.data());
1542+
assert!(item_family(trait_doc) == Family::Trait);
1543+
let defaulted_doc = reader::get_doc(trait_doc, tag_defaulted_trait);
1544+
reader::doc_as_u8(defaulted_doc) != 0
15461545
}
15471546

15481547
pub fn get_imported_filemaps(metadata: &[u8]) -> Vec<codemap::FileMap> {

src/librustc/metadata/encoder.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12881288
let trait_predicates = ty::lookup_predicates(tcx, def_id);
12891289
encode_unsafety(rbml_w, trait_def.unsafety);
12901290
encode_paren_sugar(rbml_w, trait_def.paren_sugar);
1291+
encode_defaulted(rbml_w, ty::trait_has_default_impl(tcx, def_id));
12911292
encode_associated_type_names(rbml_w, &trait_def.associated_type_names);
12921293
encode_generics(rbml_w, ecx, &trait_def.generics, &trait_predicates, tag_item_generics);
12931294
encode_predicates(rbml_w, ecx, &ty::lookup_super_predicates(tcx, def_id),
@@ -1660,6 +1661,11 @@ fn encode_paren_sugar(rbml_w: &mut Encoder, paren_sugar: bool) {
16601661
rbml_w.wr_tagged_u8(tag_paren_sugar, byte);
16611662
}
16621663

1664+
fn encode_defaulted(rbml_w: &mut Encoder, is_defaulted: bool) {
1665+
let byte: u8 = if is_defaulted {1} else {0};
1666+
rbml_w.wr_tagged_u8(tag_defaulted_trait, byte);
1667+
}
1668+
16631669
fn encode_associated_type_names(rbml_w: &mut Encoder, names: &[ast::Name]) {
16641670
rbml_w.start_tag(tag_associated_type_names);
16651671
for &name in names {

src/librustc/middle/ty.rs

+18-36
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,8 @@ pub struct ctxt<'tcx> {
757757
/// Maps a trait onto a list of impls of that trait.
758758
pub trait_impls: RefCell<DefIdMap<Rc<RefCell<Vec<ast::DefId>>>>>,
759759

760-
/// Maps a trait onto a list of *default* trait implementations
761-
default_trait_impls: RefCell<DefIdMap<ast::DefId>>,
760+
/// A set of traits that have a default impl
761+
traits_with_default_impls: RefCell<DefIdMap<()>>,
762762

763763
/// Maps a DefId of a type to a list of its inherent impls.
764764
/// Contains implementations of methods that are inherent to a type.
@@ -2591,7 +2591,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
25912591
destructor_for_type: RefCell::new(DefIdMap()),
25922592
destructors: RefCell::new(DefIdSet()),
25932593
trait_impls: RefCell::new(DefIdMap()),
2594-
default_trait_impls: RefCell::new(DefIdMap()),
2594+
traits_with_default_impls: RefCell::new(DefIdMap()),
25952595
inherent_impls: RefCell::new(DefIdMap()),
25962596
impl_items: RefCell::new(DefIdMap()),
25972597
used_unsafe: RefCell::new(NodeSet()),
@@ -5975,32 +5975,22 @@ pub fn item_variances(tcx: &ctxt, item_id: ast::DefId) -> Rc<ItemVariances> {
59755975
|| Rc::new(csearch::get_item_variances(&tcx.sess.cstore, item_id)))
59765976
}
59775977

5978-
pub fn trait_default_impl(tcx: &ctxt, trait_def_id: DefId) -> Option<ast::DefId> {
5979-
match tcx.default_trait_impls.borrow().get(&trait_def_id) {
5980-
Some(id) => Some(*id),
5981-
None => None
5982-
}
5983-
}
5984-
59855978
pub fn trait_has_default_impl(tcx: &ctxt, trait_def_id: DefId) -> bool {
5979+
populate_implementations_for_trait_if_necessary(tcx, trait_def_id);
59865980
match tcx.lang_items.to_builtin_kind(trait_def_id) {
59875981
Some(BoundSend) | Some(BoundSync) => true,
5988-
_ => tcx.default_trait_impls.borrow().contains_key(&trait_def_id)
5982+
_ => tcx.traits_with_default_impls.borrow().contains_key(&trait_def_id),
59895983
}
59905984
}
59915985

59925986
/// Records a trait-to-implementation mapping.
5993-
pub fn record_default_trait_implementation(tcx: &ctxt,
5994-
trait_def_id: DefId,
5995-
impl_def_id: DefId) {
5996-
5987+
pub fn record_trait_has_default_impl(tcx: &ctxt, trait_def_id: DefId) {
59975988
// We're using the latest implementation found as the reference one.
59985989
// Duplicated implementations are caught and reported in the coherence
59995990
// step.
6000-
tcx.default_trait_impls.borrow_mut().insert(trait_def_id, impl_def_id);
5991+
tcx.traits_with_default_impls.borrow_mut().insert(trait_def_id, ());
60015992
}
60025993

6003-
60045994
/// Records a trait-to-implementation mapping.
60055995
pub fn record_trait_implementation(tcx: &ctxt,
60065996
trait_def_id: DefId,
@@ -6031,8 +6021,7 @@ pub fn populate_implementations_for_type_if_necessary(tcx: &ctxt,
60316021
debug!("populate_implementations_for_type_if_necessary: searching for {:?}", type_id);
60326022

60336023
let mut inherent_impls = Vec::new();
6034-
csearch::each_implementation_for_type(&tcx.sess.cstore, type_id,
6035-
|impl_def_id| {
6024+
csearch::each_implementation_for_type(&tcx.sess.cstore, type_id, |impl_def_id| {
60366025
let impl_items = csearch::get_impl_items(&tcx.sess.cstore, impl_def_id);
60376026

60386027
// Record the trait->implementation mappings, if applicable.
@@ -6078,27 +6067,20 @@ pub fn populate_implementations_for_trait_if_necessary(
60786067
if trait_id.krate == LOCAL_CRATE {
60796068
return
60806069
}
6070+
60816071
if tcx.populated_external_traits.borrow().contains(&trait_id) {
60826072
return
60836073
}
60846074

6085-
csearch::each_implementation_for_trait(&tcx.sess.cstore, trait_id,
6086-
|implementation_def_id|{
6087-
let impl_items = csearch::get_impl_items(&tcx.sess.cstore, implementation_def_id);
6075+
if csearch::is_defaulted_trait(&tcx.sess.cstore, trait_id) {
6076+
record_trait_has_default_impl(tcx, trait_id);
6077+
}
60886078

6089-
if csearch::is_default_trait(&tcx.sess.cstore, implementation_def_id) {
6090-
record_default_trait_implementation(tcx, trait_id,
6091-
implementation_def_id);
6092-
tcx.populated_external_traits.borrow_mut().insert(trait_id);
6079+
csearch::each_implementation_for_trait(&tcx.sess.cstore, trait_id, |implementation_def_id| {
6080+
let impl_items = csearch::get_impl_items(&tcx.sess.cstore, implementation_def_id);
60936081

6094-
// Nothing else to do for default trait implementations since
6095-
// they are not allowed to have type parameters, methods, or any
6096-
// other item that could be associated to a trait implementation.
6097-
return;
6098-
} else {
6099-
// Record the trait->implementation mapping.
6100-
record_trait_implementation(tcx, trait_id, implementation_def_id);
6101-
}
6082+
// Record the trait->implementation mapping.
6083+
record_trait_implementation(tcx, trait_id, implementation_def_id);
61026084

61036085
// For any methods that use a default implementation, add them to
61046086
// the map. This is a bit unfortunate.
@@ -6108,8 +6090,8 @@ pub fn populate_implementations_for_trait_if_necessary(
61086090
MethodTraitItem(method) => {
61096091
if let Some(source) = method.provided_source {
61106092
tcx.provided_method_sources
6111-
.borrow_mut()
6112-
.insert(method_def_id, source);
6093+
.borrow_mut()
6094+
.insert(method_def_id, source);
61136095
}
61146096
}
61156097
TypeTraitItem(_) => {}

src/librustc_typeck/coherence/overlap.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ use syntax::ast;
2020
use syntax::ast_util;
2121
use syntax::visit;
2222
use syntax::codemap::Span;
23+
use util::nodemap::DefIdMap;
2324
use util::ppaux::Repr;
2425

2526
pub fn check(tcx: &ty::ctxt) {
26-
let mut overlap = OverlapChecker { tcx: tcx };
27+
let mut overlap = OverlapChecker { tcx: tcx, default_impls: DefIdMap() };
2728
overlap.check_for_overlapping_impls();
2829

2930
// this secondary walk specifically checks for impls of defaulted
@@ -33,6 +34,9 @@ pub fn check(tcx: &ty::ctxt) {
3334

3435
struct OverlapChecker<'cx, 'tcx:'cx> {
3536
tcx: &'cx ty::ctxt<'tcx>,
37+
38+
// maps from a trait def-id to an impl id
39+
default_impls: DefIdMap<ast::NodeId>,
3640
}
3741

3842
impl<'cx, 'tcx> OverlapChecker<'cx, 'tcx> {
@@ -134,24 +138,19 @@ impl<'cx, 'tcx,'v> visit::Visitor<'v> for OverlapChecker<'cx, 'tcx> {
134138
fn visit_item(&mut self, item: &'v ast::Item) {
135139
match item.node {
136140
ast::ItemDefaultImpl(_, _) => {
141+
// look for another default impl; note that due to the
142+
// general orphan/coherence rules, it must always be
143+
// in this crate.
137144
let impl_def_id = ast_util::local_def(item.id);
138-
match ty::impl_trait_ref(self.tcx, impl_def_id) {
139-
Some(ref trait_ref) => {
140-
match ty::trait_default_impl(self.tcx, trait_ref.def_id) {
141-
Some(other_impl) if other_impl != impl_def_id => {
142-
self.report_overlap_error(trait_ref.def_id,
143-
other_impl,
144-
impl_def_id);
145-
}
146-
Some(_) => {}
147-
None => {
148-
self.tcx.sess.bug(
149-
&format!("no default implementation recorded for `{:?}`",
150-
item));
151-
}
152-
}
145+
let trait_ref = ty::impl_trait_ref(self.tcx, impl_def_id).unwrap();
146+
let prev_default_impl = self.default_impls.insert(trait_ref.def_id, item.id);
147+
match prev_default_impl {
148+
Some(prev_id) => {
149+
self.report_overlap_error(trait_ref.def_id,
150+
impl_def_id,
151+
ast_util::local_def(prev_id));
153152
}
154-
_ => {}
153+
None => { }
155154
}
156155
}
157156
_ => {}

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ fn convert_item(ccx: &CrateCtxt, it: &ast::Item) {
979979
None,
980980
None);
981981

982-
ty::record_default_trait_implementation(tcx, trait_ref.def_id, local_def(it.id))
982+
ty::record_trait_has_default_impl(tcx, trait_ref.def_id);
983983
}
984984
ast::ItemImpl(_, _,
985985
ref generics,

0 commit comments

Comments
 (0)