Skip to content

Commit adcdd60

Browse files
sgriffrewsxcv
authored andcommitted
Put overlapping impls behind feature gate, add tests
I've added some explicit tests that negative impls are allowed to overlap, and also to make sure that the feature doesn't interfere with specialization. I've not added an explicit test for positive overlapping with negative, as that's already tested elsewhere.
1 parent c81c958 commit adcdd60

7 files changed

+59
-0
lines changed

src/librustc/ty/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22302230
/// Returns true if the impls are the same polarity and are implementing
22312231
/// a trait which contains no items
22322232
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+
}
22332236
let trait1_is_empty = self.impl_trait_ref(def_id1)
22342237
.map_or(false, |trait_ref| {
22352238
self.associated_item_def_ids(trait_ref.def_id).is_empty()

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/coherence-conflicting-negative-trait-impl.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
#![feature(optin_builtin_traits)]
12+
#![feature(overlapping_marker_traits)]
1213

1314
trait MyTrait {}
1415

src/test/compile-fail/coherence-impls-send.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
#![feature(optin_builtin_traits)]
12+
#![feature(overlapping_marker_traits)]
1213

1314
use std::marker::Copy;
1415

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
trait MyMarker {}
12+
13+
impl<T> MyMarker for T {}
14+
impl<T> MyMarker for Vec<T> {}
15+
//~^ ERROR E0119
16+
17+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(overlapping_marker_traits)]
12+
#![feature(specialization)]
13+
14+
trait MyMarker {}
15+
16+
impl<T> MyMarker for T {}
17+
impl<T> MyMarker for Vec<T> {}
18+
19+
fn foo<T: MyMarker>(t: T) -> T {
20+
t
21+
}
22+
23+
fn main() {
24+
assert_eq!(1, foo(1));
25+
assert_eq!(2.0, foo(2.0));
26+
assert_eq!(vec![1], foo(vec![1]));
27+
}

src/test/run-pass/overlap-permitted-for-marker-traits.rs

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

11+
#![feature(overlapping_marker_traits)]
12+
#![feature(optin_builtin_traits)]
13+
1114
trait MyMarker {}
1215

1316
impl<T: Copy> MyMarker for T {}
1417
impl<T: Eq> MyMarker for T {}
1518

19+
struct MyStruct;
20+
impl !Send for MyStruct {}
21+
impl !Send for MyStruct {}
22+
1623
fn foo<T: MyMarker>(t: T) -> T {
1724
t
1825
}

0 commit comments

Comments
 (0)