-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
add a normalizes-to fast path #125334
Open
lcnr
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
lcnr:normalizes-to-rigid-fastpath
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−66
Open
add a normalizes-to fast path #125334
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
compiler/rustc_trait_selection/src/solve/normalizes_to/fast_reject.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||||
use rustc_middle::ty::{self, TyCtxt}; | ||||||
use rustc_span::def_id::DefId; | ||||||
/// We early return `NoSolution` when trying to normalize associated types if | ||||||
/// we know them to be rigid. This is necessary if there are a huge amount of | ||||||
/// rigid associated types in the `ParamEnv` as we would otherwise get hangs | ||||||
/// when trying to normalize each associated type with all other associated types. | ||||||
/// | ||||||
/// See trait-system-refactor-initiative#109 for an example. | ||||||
/// | ||||||
/// ```plain | ||||||
/// is_rigid_alias(alias) :- | ||||||
/// is_placeholder(alias.self_ty), | ||||||
/// no_applicable_blanket_impls(alias.trait_def_id), | ||||||
/// not(may_normalize_via_env(alias)), | ||||||
/// | ||||||
/// may_normalize_via_env(alias) :- exists<projection_clause> { | ||||||
/// projection_clause.def_id == alias.def_id, | ||||||
/// projection_clause.args may_unify alias.args, | ||||||
/// } | ||||||
/// ``` | ||||||
#[instrument(level = "debug", skip(tcx), ret)] | ||||||
pub(super) fn is_rigid_alias<'tcx>( | ||||||
tcx: TyCtxt<'tcx>, | ||||||
param_env: ty::ParamEnv<'tcx>, | ||||||
alias: ty::AliasTerm<'tcx>, | ||||||
) -> bool { | ||||||
// FIXME: This could consider associated types as rigid as long | ||||||
// as it considers the *recursive* item bounds of the alias, | ||||||
// which is non-trivial. We may be forced to handle this case | ||||||
// in the future. | ||||||
alias.self_ty().is_placeholder() | ||||||
&& no_applicable_blanket_impls(tcx, alias.trait_def_id(tcx)) | ||||||
&& !may_normalize_via_env(param_env, alias) | ||||||
} | ||||||
|
||||||
// FIXME: This could check whether the blanket impl has any where-bounds | ||||||
// which definitely don't hold. Doing so is quite annoying, both just in | ||||||
// general, but we also have to be careful about builtin blanket impls, | ||||||
// e.g. `DiscriminantKind`. | ||||||
#[instrument(level = "trace", skip(tcx), ret)] | ||||||
fn no_applicable_blanket_impls<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId) -> bool { | ||||||
// FIXME(ptr_metadata): There's currently a builtin impl for `Pointee` which | ||||||
// applies for all `T` as long as `T: Sized` holds. THis impl should | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// get removed in favor of `Pointee` being a super trait of `Sized`. | ||||||
tcx.trait_impls_of(trait_def_id).blanket_impls().is_empty() | ||||||
&& !tcx.lang_items().pointee_trait().is_some_and(|def_id| trait_def_id == def_id) | ||||||
} | ||||||
|
||||||
#[instrument(level = "trace", ret)] | ||||||
fn may_normalize_via_env<'tcx>(param_env: ty::ParamEnv<'tcx>, alias: ty::AliasTerm<'tcx>) -> bool { | ||||||
for clause in param_env.caller_bounds() { | ||||||
let Some(projection_pred) = clause.as_projection_clause() else { | ||||||
continue; | ||||||
}; | ||||||
|
||||||
if projection_pred.projection_def_id() != alias.def_id { | ||||||
continue; | ||||||
}; | ||||||
|
||||||
let drcx = ty::fast_reject::DeepRejectCtxt { | ||||||
treat_obligation_params: ty::fast_reject::TreatParams::ForLookup, | ||||||
}; | ||||||
if drcx.args_may_unify(alias.args, projection_pred.skip_binder().projection_term.args) { | ||||||
return true; | ||||||
} | ||||||
} | ||||||
|
||||||
false | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/ui/traits/next-solver/normalize/try-unify-rigid-aliases-hang.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//@ check-pass | ||
//@ compile-flags: -Znext-solver | ||
//@ ignore-compare-mode-next-solver (explicit revisions) | ||
|
||
// Minimization of a hang in rayon, cc trait-solver-refactor-initiative#109 | ||
|
||
pub trait ParallelIterator { | ||
type Item; | ||
} | ||
|
||
macro_rules! multizip_impl { | ||
($($T:ident),+) => { | ||
impl<$( $T, )+> ParallelIterator for ($( $T, )+) | ||
where | ||
$( | ||
$T: ParallelIterator, | ||
$T::Item: ParallelIterator, | ||
)+ | ||
{ | ||
type Item = (); | ||
} | ||
} | ||
} | ||
|
||
multizip_impl! { A, B, C, D, E, F, G, H, I, J, K, L } | ||
|
||
fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This treats params always as bound vars (which is true for unsubstituted impl headers), vs when they come from the environment they should be treated as placeholders (param-env candidate matching).
I think drcx should be reworked to make this distinction, so we don't return
true
more than we need to.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this could then be used when matching param-env candidates in the new and old solvers, so we can avoid instantiating binders and doing type equality when it's useless.