diff options
3 files changed, 29 insertions, 5 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs b/compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs index 2dc48e47efc..8cc57f952ad 100644 --- a/compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs +++ b/compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs @@ -1,3 +1,6 @@ +use rustc_hir as hir; +use rustc_middle::ty::PredicateKind; + use crate::infer::canonical::OriginalQueryValues; use crate::infer::InferCtxt; use crate::traits::{ @@ -46,6 +49,12 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> { &self, obligation: &PredicateObligation<'tcx>, ) -> bool { + if let PredicateKind::Trait(pred) = obligation.predicate.kind().skip_binder() { + if let hir::Constness::Const = pred.constness { + // do not evaluate to holds when we have a const predicate. + return false; + } + } self.evaluate_obligation_no_overflow(obligation).must_apply_considering_regions() } diff --git a/src/test/ui/rfc-2632-const-trait-impl/assoc-type.rs b/src/test/ui/rfc-2632-const-trait-impl/assoc-type.rs index fe3ad24be2a..8d72adb6b61 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/assoc-type.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/assoc-type.rs @@ -1,8 +1,4 @@ -// ignore-test -// -// FIXME: This test should fail since, within a const impl of `Foo`, the bound on `Foo::Bar` should -// require a const impl of `Add` for the associated type. - +// FIXME(fee1-dead): this should have a better error message #![feature(const_trait_impl)] struct NonConstAdd(i32); @@ -21,6 +17,7 @@ trait Foo { impl const Foo for NonConstAdd { type Bar = NonConstAdd; + //~^ ERROR } fn main() {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr b/src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr new file mode 100644 index 00000000000..1fdc963e7b2 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr @@ -0,0 +1,18 @@ +error[E0277]: cannot add `NonConstAdd` to `NonConstAdd` + --> $DIR/assoc-type.rs:19:5 + | +LL | type Bar: std::ops::Add; + | ------------- required by this bound in `Foo::Bar` +... +LL | type Bar = NonConstAdd; + | ^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `NonConstAdd + NonConstAdd` + | + = help: the trait `Add` is not implemented for `NonConstAdd` +help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement + | +LL | impl const Foo for NonConstAdd where NonConstAdd: Add { + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. |
