Repository navigation
do not prefer trivially true candidates with -Znext-solver
#163928
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
Draft
lcnr
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
lcnr:only-merge-eq-modulo-regions
base: main
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.
+165
−173
Draft
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -25,7 +25,10 @@ use rustc_type_ir::inherent::*; | |
| use rustc_type_ir::lang_items::SolverTraitLangItem; | ||
| pub use rustc_type_ir::solve::*; | ||
| use rustc_type_ir::transmute::Condition; | ||
| use rustc_type_ir::{self as ty, Const, Interner, Region, TraitClause, TypeVisitableExt, Upcast}; | ||
| use rustc_type_ir::{ | ||
| self as ty, BoundVarIndexKind, Const, Interner, Region, TraitClause, TypeFlags, | ||
| TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, Upcast, | ||
| }; | ||
| use thin_vec::{ThinVec, thin_vec}; | ||
| use tracing::instrument; | ||
|
|
||
|
|
@@ -89,25 +92,107 @@ fn equal_response_modulo_region_constraints<I: Interner>( | |
| b: &CanonicalResponse<I>, | ||
| ) -> bool { | ||
| let CanonicalResponse { | ||
| max_universe: a_max_universe, | ||
| var_kinds: a_var_kinds, | ||
| value: | ||
| Response { | ||
| var_values: a_var_values, | ||
| certainty: a_certainty, | ||
| external_constraints: a_external_constraints, | ||
| }, | ||
| max_universe: _, // implied by `var_kinds` being equal. | ||
| var_kinds, | ||
| value: Response { var_values, certainty, external_constraints }, | ||
| } = a; | ||
|
|
||
| let ExternalConstraintsData { region_constraints: _, opaque_types, normalization_nested_goals } = | ||
| &**a_external_constraints; | ||
| &**external_constraints; | ||
|
|
||
| a_max_universe == &b.max_universe | ||
| && a_var_kinds == &b.var_kinds | ||
| && a_var_values == &b.value.var_values | ||
| && a_certainty == &b.value.certainty | ||
| let eq_up_to_var_kinds = var_values == &b.value.var_values | ||
| && certainty == &b.value.certainty | ||
| && opaque_types == &b.value.external_constraints.opaque_types | ||
| && normalization_nested_goals == &b.value.external_constraints.normalization_nested_goals | ||
| && normalization_nested_goals == &b.value.external_constraints.normalization_nested_goals; | ||
|
|
||
| if !eq_up_to_var_kinds { | ||
| return false; | ||
| } | ||
|
|
||
| if var_kinds == &b.var_kinds { | ||
| true | ||
| } else { | ||
| // If only the `var_kinds` are different, we have to check whether they only | ||
| // differ due to region constraints. | ||
| // | ||
| // FIXME: This is only only sufficient if region constraints are canonicalized | ||
| // last, which they are not. This means this check incorrectly returns `false` if | ||
| // either the `opaque_types` or `normalization_nested_goals` reference variables | ||
| // not mentioned before. The only way this happens is if they mention placeholders | ||
| // or higher-ranked existential variables created inside of the query. This feels | ||
| // very unlikely and maybe not even worth fixing. | ||
| let num_shared: usize = | ||
| var_kinds.iter().zip(b.var_kinds.iter()).take_while(|(a, b)| a == b).count(); | ||
| response_only_references_shared_vars_outside_of_region_constraints(num_shared, a) | ||
| && response_only_references_shared_vars_outside_of_region_constraints(num_shared, b) | ||
| } | ||
| } | ||
|
|
||
| /// This is slightly hacky. If responses differ in their `var_values`, then even if their | ||
| /// `opaque_types` are structurally equal, they could still be different because the | ||
| /// same `var_value` maps to a different value when instantiating the response. | ||
| /// | ||
| /// To handle this, we make sure that everything but the region constraints only reference | ||
| /// `var_values` which are the same for both responses. | ||
| fn response_only_references_shared_vars_outside_of_region_constraints<I: Interner>( | ||
| num_shared: usize, | ||
| response: &CanonicalResponse<I>, | ||
| ) -> bool { | ||
| struct OnlyReferencesSharedVars { | ||
| num_shared: usize, | ||
| } | ||
| impl<I: Interner> TypeVisitor<I> for OnlyReferencesSharedVars { | ||
| type Result = Result<(), ()>; | ||
|
|
||
| fn visit_ty(&mut self, t: I::Ty) -> Result<(), ()> { | ||
| if let ty::Bound(BoundVarIndexKind::Canonical, b) = t.kind() { | ||
| if b.var.as_usize() >= self.num_shared { Err(()) } else { Ok(()) } | ||
| } else if t.has_type_flags(TypeFlags::HAS_CANONICAL_BOUND) { | ||
| t.super_visit_with(self) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn visit_region(&mut self, r: Region<I>) -> Result<(), ()> { | ||
| if let ty::ReBound(BoundVarIndexKind::Canonical, b) = r.kind() { | ||
| if b.var.as_usize() >= self.num_shared { Err(()) } else { Ok(()) } | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn visit_const(&mut self, c: Const<I>) -> Result<(), ()> { | ||
| if let ty::ConstKind::Bound(BoundVarIndexKind::Canonical, b) = c.kind() { | ||
| if b.var.as_usize() >= self.num_shared { Err(()) } else { Ok(()) } | ||
| } else if c.has_type_flags(TypeFlags::HAS_CANONICAL_BOUND) { | ||
| c.super_visit_with(self) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn visit_predicate<P: rustc_type_ir::PredicateProxy<I>>(&mut self, p: P) -> Result<(), ()> { | ||
| if p.has_type_flags(TypeFlags::HAS_CANONICAL_BOUND) { | ||
| p.super_visit_with(self) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let CanonicalResponse { | ||
| max_universe: _, | ||
| var_kinds: _, | ||
| value: Response { var_values, certainty: _, external_constraints }, | ||
| } = response; | ||
| let ExternalConstraintsData { region_constraints: _, opaque_types, normalization_nested_goals } = | ||
| &**external_constraints; | ||
|
|
||
| let mut visitor = OnlyReferencesSharedVars { num_shared }; | ||
| var_values.visit_with(&mut visitor).is_ok() | ||
| && opaque_types.visit_with(&mut visitor).is_ok() | ||
| && normalization_nested_goals.visit_with(&mut visitor).is_ok() | ||
| } | ||
|
|
||
| impl<'a, D, I> EvalCtxt<'a, D> | ||
|
|
@@ -322,66 +407,50 @@ where | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| enum MergeCandidateInfo { | ||
| AlwaysApplicable(usize), | ||
| EqualResponse, | ||
| } | ||
|
|
||
| impl<D, I> EvalCtxt<'_, D> | ||
| where | ||
| D: SolverDelegate<Interner = I>, | ||
| I: Interner, | ||
| { | ||
| /// Try to merge multiple possible ways to prove a goal, if that is not possible returns `None`. | ||
| /// We only merge candidates which are equal modulo region constraints. Long-term, we want to | ||
| /// emit an OR of the constraints of all candidates, for now we fail with ambiguity unless all | ||
| /// candidates have the same constraints or one candidate has no region constraints at all. | ||
| /// | ||
| /// In this case we tend to flounder and return ambiguity by calling `[EvalCtxt::flounder]`. | ||
| #[instrument(level = "trace", skip(self), ret)] | ||
| fn try_merge_candidates( | ||
| &mut self, | ||
| candidates: &[Candidate<I>], | ||
| ) -> Option<(CanonicalResponse<I>, MergeCandidateInfo)> { | ||
| if candidates.is_empty() { | ||
| ) -> Option<CanonicalResponse<I>> { | ||
| let Some((first, rest)) = candidates.split_first() else { | ||
| return None; | ||
| } | ||
|
|
||
| let always_applicable = candidates.iter().enumerate().find(|(_, candidate)| { | ||
| candidate.result.value.certainty == Certainty::Yes | ||
| && has_no_inference_or_external_constraints(candidate.result) | ||
| }); | ||
| }; | ||
|
|
||
| if let Some((i, candidate)) = always_applicable { | ||
| return Some((candidate.result, MergeCandidateInfo::AlwaysApplicable(i))); | ||
| // Check whether all candidates are equal modulo regions. | ||
| if rest.iter().any(|candidate| { | ||
|
Member
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. comment about what the behaviour of this fn is supposed to be, rn its kinda hard to figure it out imo |
||
| !equal_response_modulo_region_constraints(&first.result, &candidate.result) | ||
| }) { | ||
| return None; | ||
| } | ||
|
|
||
| let one: CanonicalResponse<I> = candidates[0].result; | ||
|
|
||
| if candidates[1..] | ||
| .iter() | ||
| .all(|candidate| equal_response_modulo_region_constraints(&one, &candidate.result)) | ||
| { | ||
| let region_constraints = &one.value.external_constraints.region_constraints; | ||
| if candidates[1..].iter().all(|candidate| { | ||
| &candidate.result.value.external_constraints.region_constraints | ||
| == region_constraints | ||
| }) { | ||
| return Some((one, MergeCandidateInfo::EqualResponse)); | ||
| } | ||
|
|
||
| // If candidates differ only in region constraints, their merged region | ||
| // constraints are an `Or` of their respective constraints. If one of them | ||
| // has no region constraints, the `Or` constraint evaluates to `true`. | ||
| // | ||
| // This is a special case of `-Zassumptions-on-binders` and should be | ||
| // replaced eventually. | ||
| if let Some(candidate) = candidates.iter().find(|candidate| { | ||
| candidate.result.value.external_constraints.region_constraints.is_empty() | ||
| }) { | ||
| return Some((candidate.result, MergeCandidateInfo::EqualResponse)); | ||
| } | ||
| // Merging constraints is trivial if they are same. | ||
| let region_constraints = &first.result.value.external_constraints.region_constraints; | ||
| if candidates[1..].iter().all(|candidate| { | ||
| &candidate.result.value.external_constraints.region_constraints == region_constraints | ||
| }) { | ||
| return Some(first.result); | ||
| } | ||
|
|
||
| None | ||
| // If candidates differ only in region constraints, their merged region | ||
| // constraints are an OR of their respective constraints. We don't support | ||
| // OR constraints yet, so the only way we can merge differing candidates | ||
| // if one of them has no region constraints as then the OR constraint | ||
| // is trivially true. | ||
| candidates | ||
| .iter() | ||
| .find(|c| c.result.value.external_constraints.region_constraints.is_empty()) | ||
| .map(|c| c.result) | ||
| } | ||
|
|
||
| fn bail_with_ambiguity(&mut self, candidates: &[Candidate<I>]) -> CanonicalResponse<I> { | ||
|
|
||
This file contains hidden or 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
Submodule gcc
updated
7 files
| +10 −44 | gcc/jit/jit-playback.cc | |
| +3 −9 | gcc/jit/jit-playback.h | |
| +4 −18 | gcc/jit/jit-recording.cc | |
| +0 −15 | gcc/jit/jit-recording.h | |
| +0 −47 | gcc/jit/libgccjit.cc | |
| +0 −9 | gcc/jit/libgccjit.h | |
| +0 −10 | gcc/jit/libgccjit.map |
29 changes: 29 additions & 0 deletions
29
tests/ui/traits/next-solver/assembly/item-bound-eq-modulo-placeholder-constraint.rs
This file contains hidden or 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,29 @@ | ||
| //@ revisions: current next | ||
| //@ ignore-compare-mode-next-solver (explicit revisions) | ||
| //@[next] compile-flags: -Znext-solver | ||
| //@ check-pass | ||
|
|
||
| // When proving `C::Val<'a>: for<'b> PartialEq<C::Val<'b>>` there are | ||
| // two candidates: | ||
| // - `C::Val<'a>: PartialEq<C::Val<'a>>` | ||
| // - `for<'b> C::Val<'a>: PartialEq<C::Val<'b>>` | ||
| // | ||
| // The first candidate results in an `'!b == 'a` constraint while the | ||
| // second one has no region constraints. The constraint of the first | ||
| // candidate means the canonical response has an entry for `'b` in its | ||
| // `var_values`. This previously caused us to not prefer the second | ||
| // constraint. | ||
| // | ||
| // FIXME(trait-system-refactor-initiative#305): Now, actually, the first | ||
| // candidate should just result in a leak check failure, but doesn't. | ||
| // That's a separate issue though :> | ||
| trait Cursor { | ||
| type Val<'a>: PartialEq + for<'b> PartialEq<Self::Val<'b>>; | ||
| } | ||
| impl<C: Cursor> Cursor for &C { | ||
| // for<'b> C::Val<'a>: PartialEq<C::Val<'b>> | ||
| // - C::Val<'a>: PartialEq<C::Val<'a>>` | ||
| // - for<'b> C::Val<'a>: PartialEq<C::Val<'b>> | ||
| type Val<'a> = C::Val<'a>; | ||
| } | ||
| fn main() {} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
i dont understand the mental model behind what ur doing here xd slash in general somewhat confused by the concept of comparing canonical responses with different vars
that combined with it apparently just being wrong in the presence of opaque types and nested goals?
View changes since the review