Skip to content

Commit 12b846a

Browse files
committed
Auto merge of #23038 - nikomatsakis:issue-22978-defaulted-coherence, r=flaper87
Fixes #22978. r? @flaper87
2 parents b83b26b + 17358d1 commit 12b846a

18 files changed

+299
-131
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/impls.rs

-47
This file was deleted.

src/librustc_typeck/coherence/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use syntax::visit;
4949
use util::nodemap::{DefIdMap, FnvHashMap};
5050
use util::ppaux::Repr;
5151

52-
mod impls;
5352
mod orphan;
5453
mod overlap;
5554
mod unsafety;
@@ -583,7 +582,6 @@ pub fn check_coherence(crate_context: &CrateCtxt) {
583582
inference_context: new_infer_ctxt(crate_context.tcx),
584583
inherent_impls: RefCell::new(FnvHashMap()),
585584
}.check(crate_context.tcx.map.krate());
586-
impls::check(crate_context.tcx);
587585
unsafety::check(crate_context.tcx);
588586
orphan::check(crate_context.tcx);
589587
overlap::check(crate_context.tcx);

src/librustc_typeck/coherence/orphan.rs

+108-5
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
3737
a trait or new type instead");
3838
}
3939
}
40-
}
4140

42-
impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
43-
fn visit_item(&mut self, item: &ast::Item) {
41+
/// Checks exactly one impl for orphan rules and other such
42+
/// restrictions. In this fn, it can happen that multiple errors
43+
/// apply to a specific impl, so just return after reporting one
44+
/// to prevent inundating the user with a bunch of similar error
45+
/// reports.
46+
fn check_item(&self, item: &ast::Item) {
4447
let def_id = ast_util::local_def(item.id);
4548
match item.node {
4649
ast::ItemImpl(_, _, _, None, _, _) => {
@@ -63,13 +66,15 @@ impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
6366
span_err!(self.tcx.sess, item.span, E0118,
6467
"no base type found for inherent implementation; \
6568
implement a trait or new type instead");
69+
return;
6670
}
6771
}
6872
}
6973
ast::ItemImpl(_, _, _, Some(_), _, _) => {
7074
// "Trait" impl
7175
debug!("coherence2::orphan check: trait impl {}", item.repr(self.tcx));
72-
let trait_def_id = ty::impl_trait_ref(self.tcx, def_id).unwrap().def_id;
76+
let trait_ref = ty::impl_trait_ref(self.tcx, def_id).unwrap();
77+
let trait_def_id = trait_ref.def_id;
7378
match traits::orphan_check(self.tcx, def_id) {
7479
Ok(()) => { }
7580
Err(traits::OrphanCheckErr::NoLocalInputType) => {
@@ -80,6 +85,7 @@ impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
8085
types defined in this crate; \
8186
only traits defined in the current crate can be \
8287
implemented for arbitrary types");
88+
return;
8389
}
8490
}
8591
Err(traits::OrphanCheckErr::UncoveredTy(param_ty)) => {
@@ -89,9 +95,100 @@ impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
8995
some local type (e.g. `MyStruct<T>`); only traits defined in \
9096
the current crate can be implemented for a type parameter",
9197
param_ty.user_string(self.tcx));
98+
return;
9299
}
93100
}
94101
}
102+
103+
// In addition to the above rules, we restrict impls of defaulted traits
104+
// so that they can only be implemented on structs/enums. To see why this
105+
// restriction exists, consider the following example (#22978). Imagine
106+
// that crate A defines a defaulted trait `Foo` and a fn that operates
107+
// on pairs of types:
108+
//
109+
// ```
110+
// // Crate A
111+
// trait Foo { }
112+
// impl Foo for .. { }
113+
// fn two_foos<A:Foo,B:Foo>(..) {
114+
// one_foo::<(A,B)>(..)
115+
// }
116+
// fn one_foo<T:Foo>(..) { .. }
117+
// ```
118+
//
119+
// This type-checks fine; in particular the fn
120+
// `two_foos` is able to conclude that `(A,B):Foo`
121+
// because `A:Foo` and `B:Foo`.
122+
//
123+
// Now imagine that crate B comes along and does the following:
124+
//
125+
// ```
126+
// struct A { }
127+
// struct B { }
128+
// impl Foo for A { }
129+
// impl Foo for B { }
130+
// impl !Send for (A, B) { }
131+
// ```
132+
//
133+
// This final impl is legal according to the orpan
134+
// rules, but it invalidates the reasoning from
135+
// `two_foos` above.
136+
debug!("trait_ref={} trait_def_id={} trait_has_default_impl={}",
137+
trait_ref.repr(self.tcx),
138+
trait_def_id.repr(self.tcx),
139+
ty::trait_has_default_impl(self.tcx, trait_def_id));
140+
if
141+
ty::trait_has_default_impl(self.tcx, trait_def_id) &&
142+
trait_def_id.krate != ast::LOCAL_CRATE
143+
{
144+
let self_ty = trait_ref.self_ty();
145+
let opt_self_def_id = match self_ty.sty {
146+
ty::ty_struct(self_def_id, _) | ty::ty_enum(self_def_id, _) =>
147+
Some(self_def_id),
148+
ty::ty_uniq(..) =>
149+
self.tcx.lang_items.owned_box(),
150+
_ =>
151+
None
152+
};
153+
154+
let msg = match opt_self_def_id {
155+
// We only want to permit structs/enums, but not *all* structs/enums.
156+
// They must be local to the current crate, so that people
157+
// can't do `unsafe impl Send for Rc<SomethingLocal>` or
158+
// `impl !Send for Box<SomethingLocalAndSend>`.
159+
Some(self_def_id) => {
160+
if self_def_id.krate == ast::LOCAL_CRATE {
161+
None
162+
} else {
163+
Some(format!(
164+
"cross-crate traits with a default impl, like `{}`, \
165+
can only be implemented for a struct/enum type \
166+
defined in the current crate",
167+
ty::item_path_str(self.tcx, trait_def_id)))
168+
}
169+
}
170+
_ => {
171+
Some(format!(
172+
"cross-crate traits with a default impl, like `{}`, \
173+
can only be implemented for a struct/enum type, \
174+
not `{}`",
175+
ty::item_path_str(self.tcx, trait_def_id),
176+
self_ty.user_string(self.tcx)))
177+
}
178+
};
179+
180+
if let Some(msg) = msg {
181+
span_err!(self.tcx.sess, item.span, E0321, "{}", msg);
182+
return;
183+
}
184+
}
185+
186+
// Disallow *all* explicit impls of `Sized` for now.
187+
if Some(trait_def_id) == self.tcx.lang_items.sized_trait() {
188+
span_err!(self.tcx.sess, item.span, E0322,
189+
"explicit impls for the `Sized` trait are not permitted");
190+
return;
191+
}
95192
}
96193
ast::ItemDefaultImpl(..) => {
97194
// "Trait" impl
@@ -100,14 +197,20 @@ impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
100197
if trait_ref.def_id.krate != ast::LOCAL_CRATE {
101198
span_err!(self.tcx.sess, item.span, E0318,
102199
"cannot create default implementations for traits outside the \
103-
crate they're defined in; define a new trait instead.");
200+
crate they're defined in; define a new trait instead");
201+
return;
104202
}
105203
}
106204
_ => {
107205
// Not an impl
108206
}
109207
}
208+
}
209+
}
110210

211+
impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
212+
fn visit_item(&mut self, item: &ast::Item) {
213+
self.check_item(item);
111214
visit::walk_item(self, item);
112215
}
113216
}

0 commit comments

Comments
 (0)