Skip to content

Commit 2890508

Browse files
committed
Auto merge of #21689 - FlaPer87:oibit-send-and-friends, r=nikomatsakis
This is one more step towards completing #13231 This series of commits add support for default trait implementations. The changes in this PR don't break existing code and they are expected to preserve the existing behavior in the compiler as far as built-in bounds checks go. The PR adds negative implementations of `Send`/`Sync` for some types and it removes the special cases for `Send`/`Sync` during the trait obligations checks. That is, it now fully relies on the traits check rather than lang items. Once this patch lands and a new snapshot is created, it'll be possible to add default impls for `Send` and `Sync` and remove entirely the use of `BuiltinBound::{BoundSend,BoundSync}` for positive implementations as well. This PR also removes the restriction on negative implementations. That is, it is now possible to add negative implementations for traits other than `Send`/`Sync`
2 parents c4fe7d6 + 3dcc631 commit 2890508

Some content is hidden

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

68 files changed

+1208
-176
lines changed

src/libcore/cell.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
use clone::Clone;
145145
use cmp::PartialEq;
146146
use default::Default;
147-
use marker::{Copy, Send};
147+
use marker::{Copy, Send, Sync};
148148
use ops::{Deref, DerefMut, Drop};
149149
use option::Option;
150150
use option::Option::{None, Some};
@@ -660,6 +660,8 @@ pub struct UnsafeCell<T> {
660660
pub value: T,
661661
}
662662

663+
impl<T> !Sync for UnsafeCell<T> {}
664+
663665
impl<T> UnsafeCell<T> {
664666
/// Construct a new instance of `UnsafeCell` which will wrap the specified
665667
/// value.

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#![feature(staged_api)]
6969
#![feature(unboxed_closures)]
7070
#![feature(rustc_attrs)]
71+
#![feature(optin_builtin_traits)]
7172

7273
#[macro_use]
7374
mod macros;

src/libcore/marker.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ pub unsafe trait Send : MarkerTrait {
3939
// empty.
4040
}
4141

42+
impl<T> !Send for *const T { }
43+
impl<T> !Send for *mut T { }
44+
impl !Send for Managed { }
45+
4246
/// Types with a constant size known at compile-time.
4347
#[stable(feature = "rust1", since = "1.0.0")]
4448
#[lang="sized"]
@@ -204,6 +208,10 @@ pub unsafe trait Sync : MarkerTrait {
204208
// Empty
205209
}
206210

211+
impl<T> !Sync for *const T { }
212+
impl<T> !Sync for *mut T { }
213+
impl !Sync for Managed { }
214+
207215
/// A type which is considered "not POD", meaning that it is not
208216
/// implicitly copyable. This is typically embedded in other types to
209217
/// ensure that they are never copied, even if they lack a destructor.

src/librustc/metadata/csearch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,7 @@ pub fn is_associated_type(cstore: &cstore::CStore, def: ast::DefId) -> bool {
410410
decoder::is_associated_type(&*cdata, def.node)
411411
}
412412

413+
pub fn is_default_trait(cstore: &cstore::CStore, def: ast::DefId) -> bool {
414+
let cdata = cstore.get_crate_data(def.krate);
415+
decoder::is_default_trait(&*cdata, def.node)
416+
}

src/librustc/metadata/decoder.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ enum Family {
126126
TupleVariant, // v
127127
StructVariant, // V
128128
Impl, // i
129+
DefaultImpl, // d
129130
Trait, // I
130131
Struct, // S
131132
PublicField, // g
@@ -151,6 +152,7 @@ fn item_family(item: rbml::Doc) -> Family {
151152
'v' => TupleVariant,
152153
'V' => StructVariant,
153154
'i' => Impl,
155+
'd' => DefaultImpl,
154156
'I' => Trait,
155157
'S' => Struct,
156158
'g' => PublicField,
@@ -355,9 +357,9 @@ fn item_to_def_like(item: rbml::Doc, did: ast::DefId, cnum: ast::CrateNum)
355357
let enum_did = item_reqd_and_translated_parent_item(cnum, item);
356358
DlDef(def::DefVariant(enum_did, did, false))
357359
}
358-
Trait => DlDef(def::DefTrait(did)),
360+
Trait => DlDef(def::DefaultImpl(did)),
359361
Enum => DlDef(def::DefTy(did, true)),
360-
Impl => DlImpl(did),
362+
Impl | DefaultImpl => DlImpl(did),
361363
PublicField | InheritedField => DlField,
362364
}
363365
}
@@ -480,7 +482,7 @@ pub fn get_impl_trait<'tcx>(cdata: Cmd,
480482
let item_doc = lookup_item(id, cdata.data());
481483
let fam = item_family(item_doc);
482484
match fam {
483-
Family::Impl => {
485+
Family::Impl | Family::DefaultImpl => {
484486
reader::maybe_get_doc(item_doc, tag_item_trait_ref).map(|tp| {
485487
doc_trait_ref(tp, tcx, cdata)
486488
})
@@ -1356,7 +1358,7 @@ pub fn get_trait_of_item(cdata: Cmd, id: ast::NodeId, tcx: &ty::ctxt)
13561358
let parent_item_doc = lookup_item(parent_item_id.node, cdata.data());
13571359
match item_family(parent_item_doc) {
13581360
Trait => Some(item_def_id(parent_item_doc, cdata)),
1359-
Impl => {
1361+
Impl | DefaultImpl => {
13601362
reader::maybe_get_doc(parent_item_doc, tag_item_trait_ref)
13611363
.map(|_| item_trait_ref(parent_item_doc, tcx, cdata).def_id)
13621364
}
@@ -1561,3 +1563,12 @@ pub fn is_associated_type(cdata: Cmd, id: ast::NodeId) -> bool {
15611563
Some(item) => item_sort(item) == 't',
15621564
}
15631565
}
1566+
1567+
1568+
pub fn is_default_trait<'tcx>(cdata: Cmd, id: ast::NodeId) -> bool {
1569+
let item_doc = lookup_item(id, cdata.data());
1570+
match item_family(item_doc) {
1571+
Family::DefaultImpl => true,
1572+
_ => false
1573+
}
1574+
}

src/librustc/metadata/encoder.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,18 @@ fn encode_info_for_item(ecx: &EncodeContext,
11931193
None => {}
11941194
}
11951195
}
1196+
ast::ItemDefaultImpl(unsafety, ref ast_trait_ref) => {
1197+
add_to_index(item, rbml_w, index);
1198+
rbml_w.start_tag(tag_items_data_item);
1199+
encode_def_id(rbml_w, def_id);
1200+
encode_family(rbml_w, 'd');
1201+
encode_name(rbml_w, item.ident.name);
1202+
encode_unsafety(rbml_w, unsafety);
1203+
1204+
let trait_ref = ty::node_id_to_trait_ref(tcx, ast_trait_ref.ref_id);
1205+
encode_trait_ref(rbml_w, ecx, &*trait_ref, tag_item_trait_ref);
1206+
rbml_w.end_tag();
1207+
}
11961208
ast::ItemImpl(unsafety, polarity, _, ref opt_trait, ref ty, ref ast_items) => {
11971209
// We need to encode information about the default methods we
11981210
// have inherited, so we drive this based on the impl structure.

src/librustc/middle/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl tr for def::Def {
440440
def::DefVariant(e_did, v_did, is_s) => {
441441
def::DefVariant(e_did.tr(dcx), v_did.tr(dcx), is_s)
442442
},
443-
def::DefTrait(did) => def::DefTrait(did.tr(dcx)),
443+
def::DefaultImpl(did) => def::DefaultImpl(did.tr(dcx)),
444444
def::DefTy(did, is_enum) => def::DefTy(did.tr(dcx), is_enum),
445445
def::DefAssociatedTy(did) => def::DefAssociatedTy(did.tr(dcx)),
446446
def::DefAssociatedPath(def::TyParamProvenance::FromSelf(did), ident) =>

src/librustc/middle/def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub enum Def {
3838
// type `U` (indicated by the Ident).
3939
// FIXME(#20301) -- should use Name
4040
DefAssociatedPath(TyParamProvenance, ast::Ident),
41-
DefTrait(ast::DefId),
41+
DefaultImpl(ast::DefId),
4242
DefPrimTy(ast::PrimTy),
4343
DefTyParam(ParamSpace, u32, ast::DefId, ast::Name),
4444
DefUse(ast::DefId),
@@ -135,7 +135,7 @@ impl Def {
135135
DefFn(id, _) | DefStaticMethod(id, _) | DefMod(id) |
136136
DefForeignMod(id) | DefStatic(id, _) |
137137
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(id) |
138-
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
138+
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefaultImpl(id) |
139139
DefMethod(id, _, _) | DefConst(id) |
140140
DefAssociatedPath(TyParamProvenance::FromSelf(id), _) |
141141
DefAssociatedPath(TyParamProvenance::FromParam(id), _) => {

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
579579
Ok(self.cat_rvalue_node(id, span, expr_ty))
580580
}
581581
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
582-
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
582+
def::DefaultImpl(_) | def::DefTy(..) | def::DefPrimTy(_) |
583583
def::DefTyParam(..) | def::DefTyParamBinder(..) | def::DefRegion(_) |
584584
def::DefLabel(_) | def::DefSelfTy(..) |
585585
def::DefAssociatedTy(..) | def::DefAssociatedPath(..)=> {

src/librustc/middle/reachable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
290290
ast::ItemTy(..) | ast::ItemStatic(_, _, _) |
291291
ast::ItemMod(..) | ast::ItemForeignMod(..) |
292292
ast::ItemImpl(..) | ast::ItemTrait(..) |
293-
ast::ItemStruct(..) | ast::ItemEnum(..) => {}
293+
ast::ItemStruct(..) | ast::ItemEnum(..) |
294+
ast::ItemDefaultImpl(..) => {}
294295

295296
_ => {
296297
self.tcx.sess.span_bug(item.span,

0 commit comments

Comments
 (0)