Skip to content

Fast path for WF goals in new solver #142223

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,13 @@ impl<'tcx> InferCtxt<'tcx> {
}
}

pub fn shallow_resolve_term(&self, term: ty::Term<'tcx>) -> ty::Term<'tcx> {
match term.kind() {
ty::TermKind::Ty(ty) => self.shallow_resolve(ty).into(),
ty::TermKind::Const(ct) => self.shallow_resolve_const(ct).into(),
}
}

pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
self.inner.borrow_mut().type_variables().root_var(var)
}
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ impl<'tcx> Const<'tcx> {
let reported = tcx.dcx().span_delayed_bug(span, msg);
Const::new_error(tcx, reported)
}

pub fn is_trivially_wf(self) -> bool {
match self.kind() {
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) | ty::ConstKind::Bound(..) => {
true
}
ty::ConstKind::Infer(_)
| ty::ConstKind::Unevaluated(..)
| ty::ConstKind::Value(_)
| ty::ConstKind::Error(_)
| ty::ConstKind::Expr(_) => false,
}
}
}

impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,13 @@ impl<'tcx> Term<'tcx> {
}
}

pub fn is_trivially_wf(&self, tcx: TyCtxt<'tcx>) -> bool {
match self.kind() {
TermKind::Ty(ty) => ty.is_trivially_wf(tcx),
TermKind::Const(ct) => ct.is_trivially_wf(),
}
}

/// Iterator that walks `self` and any types reachable from
/// `self`, in depth-first order. Note that just walks the types
/// that appear in `self`, it does not descend into the fields of
Expand Down
44 changes: 44 additions & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,50 @@ impl<'tcx> Ty<'tcx> {
}
}

pub fn is_trivially_wf(self, tcx: TyCtxt<'tcx>) -> bool {
match *self.kind() {
ty::Bool
| ty::Char
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Str
| ty::Never
| ty::Param(_)
| ty::Placeholder(_)
| ty::Bound(..) => true,

ty::Slice(ty) => ty.is_trivially_wf(tcx) && ty.is_trivially_sized(tcx),
ty::RawPtr(ty, _) => ty.is_trivially_wf(tcx),

ty::FnPtr(sig_tys, _) => {
sig_tys.skip_binder().inputs_and_output.iter().all(|ty| ty.is_trivially_wf(tcx))
}
ty::Ref(_, ty, _) => ty.is_global() && ty.is_trivially_wf(tcx),

ty::Infer(infer) => match infer {
ty::TyVar(_) => false,
ty::IntVar(_) | ty::FloatVar(_) => true,
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => true,
},

ty::Adt(_, _)
| ty::Tuple(_)
| ty::Array(..)
| ty::Foreign(_)
| ty::Pat(_, _)
| ty::FnDef(..)
| ty::UnsafeBinder(..)
| ty::Dynamic(..)
| ty::Closure(..)
| ty::CoroutineClosure(..)
| ty::Coroutine(..)
| ty::CoroutineWitness(..)
| ty::Alias(..)
| ty::Error(_) => false,
}
}

/// If `self` is a primitive, return its [`Symbol`].
pub fn primitive_symbol(self) -> Option<Symbol> {
match self.kind() {
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_trait_selection/src/solve/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
None
}
}
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
if arg.is_trivially_wf(self.tcx) {
Some(Certainty::Yes)
} else if self.shallow_resolve_term(arg).is_infer() {
Some(Certainty::AMBIGUOUS)
} else {
None
}
}
_ => None,
}
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_middle::bug;
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt, TypingMode};
use thin_vec::ThinVec;
use thin_vec::{ThinVec, thin_vec};
use tracing::{debug, debug_span, instrument};

use super::effects::{self, HostEffectObligation};
Expand Down Expand Up @@ -336,7 +336,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
let infcx = self.selcx.infcx;

if sizedness_fast_path(infcx.tcx, obligation.predicate) {
return ProcessResult::Changed(thin_vec::thin_vec![]);
return ProcessResult::Changed(thin_vec![]);
}

if obligation.predicate.has_aliases() {
Expand Down Expand Up @@ -541,6 +541,10 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
}

ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
if term.is_trivially_wf(self.selcx.tcx()) {
return ProcessResult::Changed(thin_vec![]);
}

match wf::obligations(
self.selcx.infcx,
obligation.param_env,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}

ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
if term.is_trivially_wf(self.tcx()) {
return Ok(EvaluatedToOk);
}

// So, there is a bit going on here. First, `WellFormed` predicates
// are coinductive, like trait predicates with auto traits.
// This means that we need to detect if we have recursively
Expand Down
Loading