From 97c11ffb227c906465d339b36286e6fb99051e3a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 22 May 2023 23:48:06 +0000 Subject: [PATCH] Strongly prefer alias and param-env bounds --- .../src/solve/assembly/mod.rs | 14 ++-- .../dyn-star/param-env-infer.current.stderr | 2 +- tests/ui/dyn-star/param-env-infer.next.stderr | 65 +++++++++++++++++-- tests/ui/dyn-star/param-env-infer.rs | 3 +- ..._eq_dont_use_normalizes_to_if_substs_eq.rs | 4 +- ...dont_use_normalizes_to_if_substs_eq.stderr | 9 --- .../alias_eq_substs_eq_not_intercrate.rs | 4 +- .../alias_eq_substs_eq_not_intercrate.stderr | 9 --- 8 files changed, 78 insertions(+), 32 deletions(-) delete mode 100644 tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.stderr delete mode 100644 tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.stderr diff --git a/compiler/rustc_trait_selection/src/solve/assembly/mod.rs b/compiler/rustc_trait_selection/src/solve/assembly/mod.rs index 4e06832e65b..f32ff0442a4 100644 --- a/compiler/rustc_trait_selection/src/solve/assembly/mod.rs +++ b/compiler/rustc_trait_selection/src/solve/assembly/mod.rs @@ -2,7 +2,6 @@ use super::search_graph::OverflowHandler; use super::{EvalCtxt, SolverMode}; -use crate::solve::CanonicalResponseExt; use crate::traits::coherence; use rustc_data_structures::fx::FxIndexSet; use rustc_hir::def_id::DefId; @@ -744,13 +743,18 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { SolverMode::Normal => { let param_env_responses = candidates .iter() - .filter(|c| matches!(c.source, CandidateSource::ParamEnv(_))) + .filter(|c| { + matches!( + c.source, + CandidateSource::ParamEnv(_) | CandidateSource::AliasBound + ) + }) .map(|c| c.result) .collect::>(); if let Some(result) = self.try_merge_responses(¶m_env_responses) { - if result.has_only_region_constraints() { - return Ok(result); - } + // We strongly prefer alias and param-env bounds here, even if they affect inference. + // See https://github.com/rust-lang/trait-system-refactor-initiative/issues/11. + return Ok(result); } } } diff --git a/tests/ui/dyn-star/param-env-infer.current.stderr b/tests/ui/dyn-star/param-env-infer.current.stderr index d0785c887d8..b3af7be7950 100644 --- a/tests/ui/dyn-star/param-env-infer.current.stderr +++ b/tests/ui/dyn-star/param-env-infer.current.stderr @@ -8,7 +8,7 @@ LL | #![feature(dyn_star, pointer_like_trait)] = note: `#[warn(incomplete_features)]` on by default error[E0282]: type annotations needed - --> $DIR/param-env-infer.rs:12:10 + --> $DIR/param-env-infer.rs:13:10 | LL | t as _ | ^ cannot infer type diff --git a/tests/ui/dyn-star/param-env-infer.next.stderr b/tests/ui/dyn-star/param-env-infer.next.stderr index d0785c887d8..64d76bb04b1 100644 --- a/tests/ui/dyn-star/param-env-infer.next.stderr +++ b/tests/ui/dyn-star/param-env-infer.next.stderr @@ -7,12 +7,67 @@ LL | #![feature(dyn_star, pointer_like_trait)] = note: see issue #102425 for more information = note: `#[warn(incomplete_features)]` on by default -error[E0282]: type annotations needed - --> $DIR/param-env-infer.rs:12:10 +error[E0391]: cycle detected when computing type of `make_dyn_star::{opaque#0}` + --> $DIR/param-env-infer.rs:11:60 | -LL | t as _ - | ^ cannot infer type +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: ...which requires borrow-checking `make_dyn_star`... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...which requires promoting constants in MIR for `make_dyn_star`... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...which requires preparing `make_dyn_star` for borrow checking... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...which requires unsafety-checking `make_dyn_star`... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...which requires building MIR for `make_dyn_star`... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...which requires match-checking `make_dyn_star`... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...which requires building THIR for `make_dyn_star`... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...which requires type-checking `make_dyn_star`... + --> $DIR/param-env-infer.rs:11:1 + | +LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: ...which requires computing layout of `make_dyn_star::{opaque#0}`... + = note: ...which requires normalizing `make_dyn_star::{opaque#0}`... + = note: ...which again requires computing type of `make_dyn_star::{opaque#0}`, completing the cycle +note: cycle used when checking item types in top-level module + --> $DIR/param-env-infer.rs:5:1 + | +LL | / #![feature(dyn_star, pointer_like_trait)] +LL | | +LL | | +LL | | use std::fmt::Debug; +... | +LL | | +LL | | fn main() {} + | |____________^ error: aborting due to previous error; 1 warning emitted -For more information about this error, try `rustc --explain E0282`. +For more information about this error, try `rustc --explain E0391`. diff --git a/tests/ui/dyn-star/param-env-infer.rs b/tests/ui/dyn-star/param-env-infer.rs index 9039dde5e93..1fb16d76853 100644 --- a/tests/ui/dyn-star/param-env-infer.rs +++ b/tests/ui/dyn-star/param-env-infer.rs @@ -9,8 +9,9 @@ use std::fmt::Debug; use std::marker::PointerLike; fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a { + //[next]~^ ERROR cycle detected when computing type of `make_dyn_star::{opaque#0}` t as _ - //~^ ERROR type annotations needed + //[current]~^ ERROR type annotations needed } fn main() {} diff --git a/tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.rs b/tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.rs index 531203d9c64..3c7fc7403b1 100644 --- a/tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.rs +++ b/tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.rs @@ -1,5 +1,8 @@ // compile-flags: -Ztrait-solver=next +// check-pass +// (should not pass, should be turned into a coherence-only test) + // check that when computing `alias-eq(<() as Foo>::Assoc, <() as Foo>::Assoc)` // we do not infer `?0 = u8` via the `for (): Foo` impl or `?0 = u16` by // relating substs as either could be a valid solution. @@ -36,7 +39,6 @@ where { // `<() as Foo>::Assoc == <() as Foo<_, STOP>>::Assoc` let _: <() as Foo>::Assoc = output::<_, T>(); - //~^ error: type annotations needed // let _: <() as Foo>::Assoc = output::(); // OK // let _: <() as Foo>::Assoc = output::(); // OK diff --git a/tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.stderr b/tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.stderr deleted file mode 100644 index a6712332c37..00000000000 --- a/tests/ui/traits/new-solver/alias_eq_dont_use_normalizes_to_if_substs_eq.stderr +++ /dev/null @@ -1,9 +0,0 @@ -error[E0282]: type annotations needed - --> $DIR/alias_eq_dont_use_normalizes_to_if_substs_eq.rs:38:41 - | -LL | let _: <() as Foo>::Assoc = output::<_, T>(); - | ^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `output` - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0282`. diff --git a/tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.rs b/tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.rs index d4cc380fa21..b036411be83 100644 --- a/tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.rs +++ b/tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.rs @@ -1,5 +1,8 @@ // compile-flags: -Ztrait-solver=next +// check-pass +// (should not pass, should be turned into a coherence-only test) + // check that a `alias-eq(::Assoc, ::Assoc)` goal fails. // FIXME(deferred_projection_equality): add a test that this is true during coherence @@ -14,7 +17,6 @@ fn needs_a() -> T::Assoc { fn bar() { let _: <_ as TraitB>::Assoc = needs_a::(); - //~^ error: type annotations needed } fn main() {} diff --git a/tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.stderr b/tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.stderr deleted file mode 100644 index d063d8fce11..00000000000 --- a/tests/ui/traits/new-solver/alias_eq_substs_eq_not_intercrate.stderr +++ /dev/null @@ -1,9 +0,0 @@ -error[E0282]: type annotations needed - --> $DIR/alias_eq_substs_eq_not_intercrate.rs:16:12 - | -LL | let _: <_ as TraitB>::Assoc = needs_a::(); - | ^^^^^^^^^^^^^^^^^^^^ cannot infer type for associated type `<_ as TraitB>::Assoc` - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0282`.