From adcdd605be9cbdb338d4ecc2410cde87272f2191 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 17 Mar 2017 14:16:29 -0400 Subject: [PATCH] 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. --- src/librustc/ty/mod.rs | 3 +++ src/libsyntax/feature_gate.rs | 3 +++ ...herence-conflicting-negative-trait-impl.rs | 1 + src/test/compile-fail/coherence-impls-send.rs | 1 + ...overlapping-impls-requires-feature-gate.rs | 17 ++++++++++++ ...lap-doesnt-conflict-with-specialization.rs | 27 +++++++++++++++++++ .../overlap-permitted-for-marker-traits.rs | 7 +++++ 7 files changed, 59 insertions(+) create mode 100644 src/test/compile-fail/overlapping-impls-requires-feature-gate.rs create mode 100644 src/test/run-pass/overlap-doesnt-conflict-with-specialization.rs diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 2ae77046a90e..3da9383762bc 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -2230,6 +2230,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { /// Returns true if the impls are the same polarity and are implementing /// a trait which contains no items pub fn impls_are_allowed_to_overlap(self, def_id1: DefId, def_id2: DefId) -> bool { + if !self.sess.features.borrow().overlapping_marker_traits { + return false; + } let trait1_is_empty = self.impl_trait_ref(def_id1) .map_or(false, |trait_ref| { self.associated_item_def_ids(trait_ref.def_id).is_empty() diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 8b62416dcbdb..6e455234196d 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -349,6 +349,9 @@ declare_features! ( // Allows module-level inline assembly by way of global_asm!() (active, global_asm, "1.18.0", Some(35119)), + + // Allows overlapping impls of marker traits + (active, overlapping_marker_traits, "1.18.0", Some(29864)), ); declare_features! ( diff --git a/src/test/compile-fail/coherence-conflicting-negative-trait-impl.rs b/src/test/compile-fail/coherence-conflicting-negative-trait-impl.rs index d841e8c41d98..8e9d1eff3458 100644 --- a/src/test/compile-fail/coherence-conflicting-negative-trait-impl.rs +++ b/src/test/compile-fail/coherence-conflicting-negative-trait-impl.rs @@ -9,6 +9,7 @@ // except according to those terms. #![feature(optin_builtin_traits)] +#![feature(overlapping_marker_traits)] trait MyTrait {} diff --git a/src/test/compile-fail/coherence-impls-send.rs b/src/test/compile-fail/coherence-impls-send.rs index d0e6bc6a1c69..9caaee41aeb1 100644 --- a/src/test/compile-fail/coherence-impls-send.rs +++ b/src/test/compile-fail/coherence-impls-send.rs @@ -9,6 +9,7 @@ // except according to those terms. #![feature(optin_builtin_traits)] +#![feature(overlapping_marker_traits)] use std::marker::Copy; diff --git a/src/test/compile-fail/overlapping-impls-requires-feature-gate.rs b/src/test/compile-fail/overlapping-impls-requires-feature-gate.rs new file mode 100644 index 000000000000..bf2b06dd8ba0 --- /dev/null +++ b/src/test/compile-fail/overlapping-impls-requires-feature-gate.rs @@ -0,0 +1,17 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait MyMarker {} + +impl MyMarker for T {} +impl MyMarker for Vec {} +//~^ ERROR E0119 + +fn main() {} diff --git a/src/test/run-pass/overlap-doesnt-conflict-with-specialization.rs b/src/test/run-pass/overlap-doesnt-conflict-with-specialization.rs new file mode 100644 index 000000000000..ed45d81c0d6a --- /dev/null +++ b/src/test/run-pass/overlap-doesnt-conflict-with-specialization.rs @@ -0,0 +1,27 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(overlapping_marker_traits)] +#![feature(specialization)] + +trait MyMarker {} + +impl MyMarker for T {} +impl MyMarker for Vec {} + +fn foo(t: T) -> T { + t +} + +fn main() { + assert_eq!(1, foo(1)); + assert_eq!(2.0, foo(2.0)); + assert_eq!(vec![1], foo(vec![1])); +} diff --git a/src/test/run-pass/overlap-permitted-for-marker-traits.rs b/src/test/run-pass/overlap-permitted-for-marker-traits.rs index b0b1930d274d..45085c093fc9 100644 --- a/src/test/run-pass/overlap-permitted-for-marker-traits.rs +++ b/src/test/run-pass/overlap-permitted-for-marker-traits.rs @@ -8,11 +8,18 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(overlapping_marker_traits)] +#![feature(optin_builtin_traits)] + trait MyMarker {} impl MyMarker for T {} impl MyMarker for T {} +struct MyStruct; +impl !Send for MyStruct {} +impl !Send for MyStruct {} + fn foo(t: T) -> T { t }