From 953418685769424388fb9ea1f4b2beaceedb6857 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 22 Oct 2021 17:54:20 -0300 Subject: [PATCH] Hide negative coherence checks under negative_impls feature flag --- .../rustc_trait_selection/src/traits/coherence.rs | 9 +++++---- .../coherence-overlap-negate-not-use-feature-gate.rs | 8 ++++++++ ...herence-overlap-negate-not-use-feature-gate.stderr | 11 +++++++++++ .../coherence-overlap-negate-use-feature-gate.rs | 11 +++++++++++ .../ui/coherence/coherence-overlap-negative-trait.rs | 2 ++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.rs create mode 100644 src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.stderr create mode 100644 src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 35fd3153680..2d61675b856 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -233,10 +233,11 @@ fn overlap_within_probe( .unwrap_or(false) } else { !selcx.predicate_may_hold_fatal(o) - || o.flip_polarity(tcx) - .as_ref() - .map(|o| selcx.infcx().predicate_must_hold_modulo_regions(o)) - .unwrap_or(false) + || tcx.features().negative_impls + && o.flip_polarity(tcx) + .as_ref() + .map(|o| selcx.infcx().predicate_must_hold_modulo_regions(o)) + .unwrap_or(false) } }); // FIXME: the call to `selcx.predicate_may_hold_fatal` above should be ported diff --git a/src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.rs b/src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.rs new file mode 100644 index 00000000000..a067736f63a --- /dev/null +++ b/src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.rs @@ -0,0 +1,8 @@ +use std::ops::DerefMut; + +trait Foo {} +impl Foo for T {} +impl Foo for &U {} +//~^ ERROR: conflicting implementations of trait `Foo` for type `&_` [E0119] + +fn main() {} diff --git a/src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.stderr b/src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.stderr new file mode 100644 index 00000000000..4b55001ecc0 --- /dev/null +++ b/src/test/ui/coherence/coherence-overlap-negate-not-use-feature-gate.stderr @@ -0,0 +1,11 @@ +error[E0119]: conflicting implementations of trait `Foo` for type `&_` + --> $DIR/coherence-overlap-negate-not-use-feature-gate.rs:5:1 + | +LL | impl Foo for T {} + | --------------------------- first implementation here +LL | impl Foo for &U {} + | ^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0119`. diff --git a/src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs b/src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs new file mode 100644 index 00000000000..e024eae9819 --- /dev/null +++ b/src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs @@ -0,0 +1,11 @@ +// check-pass + +#![feature(negative_impls)] + +use std::ops::DerefMut; + +trait Foo {} +impl Foo for T {} +impl Foo for &U {} + +fn main() {} diff --git a/src/test/ui/coherence/coherence-overlap-negative-trait.rs b/src/test/ui/coherence/coherence-overlap-negative-trait.rs index 357c4e87c91..ab65163bea4 100644 --- a/src/test/ui/coherence/coherence-overlap-negative-trait.rs +++ b/src/test/ui/coherence/coherence-overlap-negative-trait.rs @@ -3,6 +3,8 @@ // // Check that if we promise to not impl what would overlap it doesn't actually overlap +#![feature(negative_impls)] + extern crate error_lib as lib; use lib::Error;