Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer param-env candidates that do no inference in new solver #109579

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/rustc_trait_selection/src/solve/assembly.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Code shared by trait and projection goals for candidate assembly.

use crate::solve::CanonicalResponseExt;

#[cfg(doc)]
use super::trait_goals::structural_traits::*;
use super::{EvalCtxt, SolverMode};
Expand All @@ -18,7 +20,7 @@ use std::fmt::Debug;
///
/// It consists of both the `source`, which describes how that goal would be proven,
/// and the `result` when using the given `source`.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub(super) struct Candidate<'tcx> {
pub(super) source: CandidateSource,
pub(super) result: CanonicalResponse<'tcx>,
Expand Down Expand Up @@ -510,12 +512,19 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
&mut self,
mut candidates: Vec<Candidate<'tcx>>,
) -> QueryResult<'tcx> {
match candidates.len() {
0 => return Err(NoSolution),
1 => return Ok(candidates.pop().unwrap().result),
match &candidates[..] {
[] => return Err(NoSolution),
[candidate] => return Ok(candidate.result),
_ => {}
}

if let Some(candidate) = candidates.iter().find(|candidate| {
candidate.result.value.certainty == Certainty::Yes
&& candidate.result.has_no_inference_or_external_constraints()
}) {
return Ok(candidate.result);
}

if candidates.len() > 1 {
let mut i = 0;
'outer: while i < candidates.len() {
Expand Down Expand Up @@ -556,6 +565,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
) -> bool {
// FIXME: implement this
match (candidate.source, other.source) {
(CandidateSource::ParamEnv(_), CandidateSource::ParamEnv(_)) => false,
(_, CandidateSource::ParamEnv(_)) if other.result.has_only_region_constraints() => true,
(CandidateSource::Impl(_), _)
| (CandidateSource::ParamEnv(_), _)
| (CandidateSource::AliasBound, _)
Expand Down
13 changes: 10 additions & 3 deletions compiler/rustc_trait_selection/src/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// FIXME: uses of `infcx.at` need to enable deferred projection equality once that's implemented.

use itertools::Itertools;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::canonical::{Canonical, CanonicalVarValues};
use rustc_infer::traits::query::NoSolution;
Expand Down Expand Up @@ -51,6 +52,8 @@ enum SolverMode {

trait CanonicalResponseExt {
fn has_no_inference_or_external_constraints(&self) -> bool;

fn has_only_region_constraints(&self) -> bool;
}

impl<'tcx> CanonicalResponseExt for Canonical<'tcx, Response<'tcx>> {
Expand All @@ -59,6 +62,11 @@ impl<'tcx> CanonicalResponseExt for Canonical<'tcx, Response<'tcx>> {
&& self.value.var_values.is_identity()
&& self.value.external_constraints.opaque_types.is_empty()
}

fn has_only_region_constraints(&self) -> bool {
self.value.var_values.is_identity()
&& self.value.external_constraints.opaque_types.is_empty()
}
}

impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
Expand Down Expand Up @@ -300,9 +308,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {

// FIXME(-Ztrait-solver=next): We should instead try to find a `Certainty::Yes` response with
// a subset of the constraints that all the other responses have.
let one = candidates[0];
if candidates[1..].iter().all(|resp| resp == &one) {
return Ok(one);
if candidates.iter().all_equal() {
return Ok(candidates[0]);
}

if let Some(response) = candidates.iter().find(|response| {
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/traits/new-solver/prefer-candidate-no-constraints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// compile-flags: -Ztrait-solver=next
// check-pass

trait Foo {}

impl<T> Foo for T {}

trait Bar {}

struct Wrapper<'a, T>(&'a T);

impl<'a, T> Bar for Wrapper<'a, T> where &'a T: Foo {}
// We need to satisfy `&'a T: Foo` when checking that this impl is WF
// that can either be satisfied via the param-env, or via an impl.
//
// When satisfied via the param-env, since each lifetime is canonicalized
// separately, we end up getting extra region constraints.
//
// However, when satisfied via the impl, there are no region constraints,
// and we can short-circuit a response with no external constraints.

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/traits/new-solver/prefer-param-env-on-ambiguity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// compile-flags: -Ztrait-solver=next
// check-pass

trait Foo<'a> {}
trait Bar<'a> {}

impl<'a, T: Bar<'a>> Foo<'a> for T {}
impl<T> Bar<'static> for T {}

fn main() {}