Skip to content

Commit 3adcd1c

Browse files
authored
Rollup merge of #41309 - frewsxcv:sg-implement-rfc-1268, r=nikomatsakis
Implement RFC 1268. Rebased version of #40097. Tracking issue: #29864.
2 parents 3f79bdc + ae9f571 commit 3adcd1c

28 files changed

+203
-22
lines changed

src/doc/unstable-book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
- [optin_builtin_traits](optin-builtin-traits.md)
137137
- [option_entry](option-entry.md)
138138
- [osstring_shrink_to_fit](osstring-shrink-to-fit.md)
139+
- [overlapping_marker_traits](overlapping-marker-traits.md)
139140
- [panic_abort](panic-abort.md)
140141
- [panic_runtime](panic-runtime.md)
141142
- [panic_unwind](panic-unwind.md)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `overlapping_marker_traits`
2+
3+
The tracking issue for this feature is: [#29864]
4+
5+
[#29864]: https://github.com/rust-lang/rust/issues/29864
6+
7+
------------------------

src/librustc/traits/select.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
17361736
if other.evaluation == EvaluatedToOk {
17371737
if let ImplCandidate(victim_def) = victim.candidate {
17381738
let tcx = self.tcx().global_tcx();
1739-
return traits::specializes(tcx, other_def, victim_def);
1739+
return traits::specializes(tcx, other_def, victim_def) ||
1740+
tcx.impls_are_allowed_to_overlap(other_def, victim_def);
17401741
}
17411742
}
17421743

src/librustc/traits/specialize/specialization_graph.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ impl<'a, 'gcx, 'tcx> Children {
113113
possible_sibling,
114114
impl_def_id);
115115
if let Some(impl_header) = overlap {
116+
if tcx.impls_are_allowed_to_overlap(impl_def_id, possible_sibling) {
117+
return Ok((false, false));
118+
}
119+
116120
let le = specializes(tcx, impl_def_id, possible_sibling);
117121
let ge = specializes(tcx, possible_sibling, impl_def_id);
118122

src/librustc/ty/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,25 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22272227
queries::impl_trait_ref::get(self, DUMMY_SP, id)
22282228
}
22292229

2230+
/// Returns true if the impls are the same polarity and are implementing
2231+
/// a trait which contains no items
2232+
pub fn impls_are_allowed_to_overlap(self, def_id1: DefId, def_id2: DefId) -> bool {
2233+
if !self.sess.features.borrow().overlapping_marker_traits {
2234+
return false;
2235+
}
2236+
let trait1_is_empty = self.impl_trait_ref(def_id1)
2237+
.map_or(false, |trait_ref| {
2238+
self.associated_item_def_ids(trait_ref.def_id).is_empty()
2239+
});
2240+
let trait2_is_empty = self.impl_trait_ref(def_id2)
2241+
.map_or(false, |trait_ref| {
2242+
self.associated_item_def_ids(trait_ref.def_id).is_empty()
2243+
});
2244+
self.trait_impl_polarity(def_id1) == self.trait_impl_polarity(def_id2)
2245+
&& trait1_is_empty
2246+
&& trait2_is_empty
2247+
}
2248+
22302249
// Returns `ty::VariantDef` if `def` refers to a struct,
22312250
// or variant or their constructors, panics otherwise.
22322251
pub fn expect_variant_def(self, def: Def) -> &'tcx VariantDef {

src/libsyntax/feature_gate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ declare_features! (
349349

350350
// Allows module-level inline assembly by way of global_asm!()
351351
(active, global_asm, "1.18.0", Some(35119)),
352+
353+
// Allows overlapping impls of marker traits
354+
(active, overlapping_marker_traits, "1.18.0", Some(29864)),
352355
);
353356

354357
declare_features! (

src/test/compile-fail/E0120.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
trait MyTrait {}
11+
trait MyTrait { fn foo() {} }
1212

1313
impl Drop for MyTrait {
1414
//~^ ERROR E0120

src/test/compile-fail/auxiliary/trait_impl_conflict.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
pub trait Foo {
12+
fn foo() {}
1213
}
1314

1415
impl Foo for isize {

src/test/compile-fail/coherence-conflicting-negative-trait-impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#![feature(optin_builtin_traits)]
12+
#![feature(overlapping_marker_traits)]
1213

1314
trait MyTrait {}
1415

@@ -20,8 +21,8 @@ impl<T: MyTrait> !Send for TestType<T> {}
2021
//~^ ERROR conflicting implementations of trait `std::marker::Send`
2122

2223
unsafe impl<T:'static> Send for TestType<T> {}
23-
//~^ ERROR conflicting implementations of trait `std::marker::Send`
2424

2525
impl !Send for TestType<i32> {}
26+
//~^ ERROR conflicting implementations of trait `std::marker::Send`
2627

2728
fn main() {}

src/test/compile-fail/coherence-default-trait-impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#![feature(optin_builtin_traits)]
1212

13-
trait MyTrait {}
13+
trait MyTrait { fn foo() {} }
1414

1515
impl MyTrait for .. {}
1616
//~^ ERROR redundant default implementations of trait `MyTrait`

0 commit comments

Comments
 (0)