From 02529d2cbecfd4edb6f481c8f0aaad9ad1fadfbb Mon Sep 17 00:00:00 2001 From: lcnr Date: Thu, 10 Aug 2023 12:56:53 +0200 Subject: [PATCH] add and move trait solver cycle tests --- .../runaway-impl-candidate-selection.rs | 0 .../runaway-impl-candidate-selection.stderr | 0 .../fixpoint-exponential-growth.rs | 0 .../fixpoint-exponential-growth.stderr | 0 .../incompleteness-unstable-result.rs | 0 .../incompleteness-unstable-result.stderr | 0 .../double-cycle-inductive-coinductive.rs | 37 ++++++++++++++ .../double-cycle-inductive-coinductive.stderr | 29 +++++++++++ .../cycles/inductive-cycle-but-err.rs | 48 +++++++++++++++++++ .../cycles/inductive-cycle-but-err.stderr | 16 +++++++ .../cycles/inductive-cycle-but-ok.rs | 44 +++++++++++++++++ ...cycle-discarded-coinductive-constraints.rs | 36 ++++++++++++++ .../leak-check-coinductive-cycle.rs | 2 +- .../{ => cycles}/provisional-result-done.rs | 0 .../new-solver/dyn-any-dont-prefer-impl.rs | 4 ++ .../{ => overflow}/exponential-trait-goals.rs | 0 .../exponential-trait-goals.stderr | 0 .../recursive-self-normalization-2.rs | 0 .../recursive-self-normalization-2.stderr | 0 .../recursive-self-normalization.rs | 0 .../recursive-self-normalization.stderr | 0 21 files changed, 215 insertions(+), 1 deletion(-) rename tests/ui/traits/new-solver/{ => assembly}/runaway-impl-candidate-selection.rs (100%) rename tests/ui/traits/new-solver/{ => assembly}/runaway-impl-candidate-selection.stderr (100%) rename tests/ui/traits/new-solver/{ => cycles}/coinduction/fixpoint-exponential-growth.rs (100%) rename tests/ui/traits/new-solver/{ => cycles}/coinduction/fixpoint-exponential-growth.stderr (100%) rename tests/ui/traits/new-solver/{ => cycles}/coinduction/incompleteness-unstable-result.rs (100%) rename tests/ui/traits/new-solver/{ => cycles}/coinduction/incompleteness-unstable-result.stderr (100%) create mode 100644 tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.rs create mode 100644 tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.stderr create mode 100644 tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.rs create mode 100644 tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.stderr create mode 100644 tests/ui/traits/new-solver/cycles/inductive-cycle-but-ok.rs create mode 100644 tests/ui/traits/new-solver/cycles/inductive-cycle-discarded-coinductive-constraints.rs rename tests/ui/traits/new-solver/{ => cycles}/leak-check-coinductive-cycle.rs (100%) rename tests/ui/traits/new-solver/{ => cycles}/provisional-result-done.rs (100%) rename tests/ui/traits/new-solver/{ => overflow}/exponential-trait-goals.rs (100%) rename tests/ui/traits/new-solver/{ => overflow}/exponential-trait-goals.stderr (100%) rename tests/ui/traits/new-solver/{ => overflow}/recursive-self-normalization-2.rs (100%) rename tests/ui/traits/new-solver/{ => overflow}/recursive-self-normalization-2.stderr (100%) rename tests/ui/traits/new-solver/{ => overflow}/recursive-self-normalization.rs (100%) rename tests/ui/traits/new-solver/{ => overflow}/recursive-self-normalization.stderr (100%) diff --git a/tests/ui/traits/new-solver/runaway-impl-candidate-selection.rs b/tests/ui/traits/new-solver/assembly/runaway-impl-candidate-selection.rs similarity index 100% rename from tests/ui/traits/new-solver/runaway-impl-candidate-selection.rs rename to tests/ui/traits/new-solver/assembly/runaway-impl-candidate-selection.rs diff --git a/tests/ui/traits/new-solver/runaway-impl-candidate-selection.stderr b/tests/ui/traits/new-solver/assembly/runaway-impl-candidate-selection.stderr similarity index 100% rename from tests/ui/traits/new-solver/runaway-impl-candidate-selection.stderr rename to tests/ui/traits/new-solver/assembly/runaway-impl-candidate-selection.stderr diff --git a/tests/ui/traits/new-solver/coinduction/fixpoint-exponential-growth.rs b/tests/ui/traits/new-solver/cycles/coinduction/fixpoint-exponential-growth.rs similarity index 100% rename from tests/ui/traits/new-solver/coinduction/fixpoint-exponential-growth.rs rename to tests/ui/traits/new-solver/cycles/coinduction/fixpoint-exponential-growth.rs diff --git a/tests/ui/traits/new-solver/coinduction/fixpoint-exponential-growth.stderr b/tests/ui/traits/new-solver/cycles/coinduction/fixpoint-exponential-growth.stderr similarity index 100% rename from tests/ui/traits/new-solver/coinduction/fixpoint-exponential-growth.stderr rename to tests/ui/traits/new-solver/cycles/coinduction/fixpoint-exponential-growth.stderr diff --git a/tests/ui/traits/new-solver/coinduction/incompleteness-unstable-result.rs b/tests/ui/traits/new-solver/cycles/coinduction/incompleteness-unstable-result.rs similarity index 100% rename from tests/ui/traits/new-solver/coinduction/incompleteness-unstable-result.rs rename to tests/ui/traits/new-solver/cycles/coinduction/incompleteness-unstable-result.rs diff --git a/tests/ui/traits/new-solver/coinduction/incompleteness-unstable-result.stderr b/tests/ui/traits/new-solver/cycles/coinduction/incompleteness-unstable-result.stderr similarity index 100% rename from tests/ui/traits/new-solver/coinduction/incompleteness-unstable-result.stderr rename to tests/ui/traits/new-solver/cycles/coinduction/incompleteness-unstable-result.stderr diff --git a/tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.rs b/tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.rs new file mode 100644 index 00000000000..5617e45adf6 --- /dev/null +++ b/tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.rs @@ -0,0 +1,37 @@ +// compile-flags: -Ztrait-solver=next +#![feature(rustc_attrs)] + +// Test that having both an inductive and a coinductive cycle +// is handled correctly. + +#[rustc_coinductive] +trait Trait {} +impl Trait for T {} + +trait Inductive {} +impl Inductive for T {} +#[rustc_coinductive] +trait Coinductive {} +impl Coinductive for T {} + +fn impls_trait() {} + +#[rustc_coinductive] +trait TraitRev {} +impl TraitRev for T {} + +trait InductiveRev {} +impl InductiveRev for T {} +#[rustc_coinductive] +trait CoinductiveRev {} +impl CoinductiveRev for T {} + +fn impls_trait_rev() {} + +fn main() { + impls_trait::<()>(); + //~^ ERROR overflow evaluating the requirement + + impls_trait_rev::<()>(); + //~^ ERROR overflow evaluating the requirement +} diff --git a/tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.stderr b/tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.stderr new file mode 100644 index 00000000000..4b8846da535 --- /dev/null +++ b/tests/ui/traits/new-solver/cycles/double-cycle-inductive-coinductive.stderr @@ -0,0 +1,29 @@ +error[E0275]: overflow evaluating the requirement `(): Trait` + --> $DIR/double-cycle-inductive-coinductive.rs:32:5 + | +LL | impls_trait::<()>(); + | ^^^^^^^^^^^^^^^^^ + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`double_cycle_inductive_coinductive`) +note: required by a bound in `impls_trait` + --> $DIR/double-cycle-inductive-coinductive.rs:17:19 + | +LL | fn impls_trait() {} + | ^^^^^ required by this bound in `impls_trait` + +error[E0275]: overflow evaluating the requirement `(): TraitRev` + --> $DIR/double-cycle-inductive-coinductive.rs:35:5 + | +LL | impls_trait_rev::<()>(); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`double_cycle_inductive_coinductive`) +note: required by a bound in `impls_trait_rev` + --> $DIR/double-cycle-inductive-coinductive.rs:29:23 + | +LL | fn impls_trait_rev() {} + | ^^^^^^^^ required by this bound in `impls_trait_rev` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0275`. diff --git a/tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.rs b/tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.rs new file mode 100644 index 00000000000..cda98789886 --- /dev/null +++ b/tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.rs @@ -0,0 +1,48 @@ +// compile-flags: -Ztrait-solver=next +#![feature(trivial_bounds, marker_trait_attr)] +#![allow(trivial_bounds)] +// This previously triggered a bug in the provisional cache. +// +// This has the proof tree +// - `MultipleCandidates: Trait` proven via impl-one +// - `MultipleNested: Trait` via impl +// - `MultipleCandidates: Trait` (inductive cycle ~> OVERFLOW) +// - `DoesNotImpl: Trait` (ERR) +// - `MultipleCandidates: Trait` proven via impl-two +// - `MultipleNested: Trait` (in provisional cache ~> OVERFLOW) +// +// We previously incorrectly treated the `MultipleCandidates: Trait` as +// overflow because it was in the cache and reached via an inductive cycle. +// It should be `NoSolution`. + +struct MultipleCandidates; +struct MultipleNested; +struct DoesNotImpl; + +#[marker] +trait Trait {} + +// impl-one +impl Trait for MultipleCandidates +where + MultipleNested: Trait +{} + +// impl-two +impl Trait for MultipleCandidates +where + MultipleNested: Trait, +{} + +impl Trait for MultipleNested +where + MultipleCandidates: Trait, + DoesNotImpl: Trait, +{} + +fn impls_trait() {} + +fn main() { + impls_trait::(); + //~^ ERROR the trait bound `MultipleCandidates: Trait` is not satisfied +} diff --git a/tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.stderr b/tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.stderr new file mode 100644 index 00000000000..57227321a6d --- /dev/null +++ b/tests/ui/traits/new-solver/cycles/inductive-cycle-but-err.stderr @@ -0,0 +1,16 @@ +error[E0277]: the trait bound `MultipleCandidates: Trait` is not satisfied + --> $DIR/inductive-cycle-but-err.rs:46:19 + | +LL | impls_trait::(); + | ^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `MultipleCandidates` + | + = help: the trait `Trait` is implemented for `MultipleCandidates` +note: required by a bound in `impls_trait` + --> $DIR/inductive-cycle-but-err.rs:43:19 + | +LL | fn impls_trait() {} + | ^^^^^ required by this bound in `impls_trait` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/traits/new-solver/cycles/inductive-cycle-but-ok.rs b/tests/ui/traits/new-solver/cycles/inductive-cycle-but-ok.rs new file mode 100644 index 00000000000..d4851eb694b --- /dev/null +++ b/tests/ui/traits/new-solver/cycles/inductive-cycle-but-ok.rs @@ -0,0 +1,44 @@ +// compile-flags: -Ztrait-solver=next +// check-pass +#![feature(trivial_bounds, marker_trait_attr)] +#![allow(trivial_bounds)] + +// This previously triggered a bug in the provisional cache. +// +// This has the proof tree +// - `Root: Trait` proven via impl +// - `MultipleCandidates: Trait` +// - candidate: overflow-impl +// - `Root: Trait` (inductive cycle ~> OVERFLOW) +// - candidate: trivial-impl ~> YES +// - merge respones ~> YES +// - `MultipleCandidates: Trait` (in provisional cache ~> OVERFLOW) +// +// We previously incorrectly treated the `MultipleCandidates: Trait` as +// overflow because it was in the cache and reached via an inductive cycle. +// It should be `YES`. + +struct Root; +struct MultipleCandidates; + +#[marker] +trait Trait {} +impl Trait for Root +where + MultipleCandidates: Trait, + MultipleCandidates: Trait, +{} + +// overflow-impl +impl Trait for MultipleCandidates +where + Root: Trait, +{} +// trivial-impl +impl Trait for MultipleCandidates {} + +fn impls_trait() {} + +fn main() { + impls_trait::(); +} diff --git a/tests/ui/traits/new-solver/cycles/inductive-cycle-discarded-coinductive-constraints.rs b/tests/ui/traits/new-solver/cycles/inductive-cycle-discarded-coinductive-constraints.rs new file mode 100644 index 00000000000..530e6d0ecf3 --- /dev/null +++ b/tests/ui/traits/new-solver/cycles/inductive-cycle-discarded-coinductive-constraints.rs @@ -0,0 +1,36 @@ +// check-pass +// compile-flags: -Ztrait-solver=next +#![feature(rustc_attrs, marker_trait_attr)] +#[rustc_coinductive] +trait Trait {} + +impl Trait for (T, U) +where + (U, T): Trait, + (T, U): Inductive, + (): ConstrainToU32, +{} + +trait ConstrainToU32 {} +impl ConstrainToU32 for () {} + +// We only prefer the candidate without an inductive cycle +// once the inductive cycle has the same constraints as the +// other goal. +#[marker] +trait Inductive {} +impl Inductive for (T, U) +where + (T, U): Trait, +{} + +impl Inductive for (u32, u32) {} + +fn impls_trait() +where + (T, U): Trait, +{} + +fn main() { + impls_trait::<_, _>(); +} diff --git a/tests/ui/traits/new-solver/leak-check-coinductive-cycle.rs b/tests/ui/traits/new-solver/cycles/leak-check-coinductive-cycle.rs similarity index 100% rename from tests/ui/traits/new-solver/leak-check-coinductive-cycle.rs rename to tests/ui/traits/new-solver/cycles/leak-check-coinductive-cycle.rs index 1f7d4a49c90..a6d31872673 100644 --- a/tests/ui/traits/new-solver/leak-check-coinductive-cycle.rs +++ b/tests/ui/traits/new-solver/cycles/leak-check-coinductive-cycle.rs @@ -1,5 +1,5 @@ -// check-pass // compile-flags: -Ztrait-solver=next +// check-pass #![feature(rustc_attrs)] #[rustc_coinductive] diff --git a/tests/ui/traits/new-solver/provisional-result-done.rs b/tests/ui/traits/new-solver/cycles/provisional-result-done.rs similarity index 100% rename from tests/ui/traits/new-solver/provisional-result-done.rs rename to tests/ui/traits/new-solver/cycles/provisional-result-done.rs diff --git a/tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs b/tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs index 7d15b8c6392..af35a6195e0 100644 --- a/tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs +++ b/tests/ui/traits/new-solver/dyn-any-dont-prefer-impl.rs @@ -1,6 +1,10 @@ // compile-flags: -Ztrait-solver=next // check-pass +// Test that selection prefers the builtin trait object impl for `Any` +// instead of the user defined impl. Both impls apply to the trait +// object. + use std::any::Any; fn needs_usize(_: &usize) {} diff --git a/tests/ui/traits/new-solver/exponential-trait-goals.rs b/tests/ui/traits/new-solver/overflow/exponential-trait-goals.rs similarity index 100% rename from tests/ui/traits/new-solver/exponential-trait-goals.rs rename to tests/ui/traits/new-solver/overflow/exponential-trait-goals.rs diff --git a/tests/ui/traits/new-solver/exponential-trait-goals.stderr b/tests/ui/traits/new-solver/overflow/exponential-trait-goals.stderr similarity index 100% rename from tests/ui/traits/new-solver/exponential-trait-goals.stderr rename to tests/ui/traits/new-solver/overflow/exponential-trait-goals.stderr diff --git a/tests/ui/traits/new-solver/recursive-self-normalization-2.rs b/tests/ui/traits/new-solver/overflow/recursive-self-normalization-2.rs similarity index 100% rename from tests/ui/traits/new-solver/recursive-self-normalization-2.rs rename to tests/ui/traits/new-solver/overflow/recursive-self-normalization-2.rs diff --git a/tests/ui/traits/new-solver/recursive-self-normalization-2.stderr b/tests/ui/traits/new-solver/overflow/recursive-self-normalization-2.stderr similarity index 100% rename from tests/ui/traits/new-solver/recursive-self-normalization-2.stderr rename to tests/ui/traits/new-solver/overflow/recursive-self-normalization-2.stderr diff --git a/tests/ui/traits/new-solver/recursive-self-normalization.rs b/tests/ui/traits/new-solver/overflow/recursive-self-normalization.rs similarity index 100% rename from tests/ui/traits/new-solver/recursive-self-normalization.rs rename to tests/ui/traits/new-solver/overflow/recursive-self-normalization.rs diff --git a/tests/ui/traits/new-solver/recursive-self-normalization.stderr b/tests/ui/traits/new-solver/overflow/recursive-self-normalization.stderr similarity index 100% rename from tests/ui/traits/new-solver/recursive-self-normalization.stderr rename to tests/ui/traits/new-solver/overflow/recursive-self-normalization.stderr