-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
more eagerly instantiate binders #119849
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
more eagerly instantiate binders #119849
Conversation
|
@bors try |
This comment was marked as outdated.
This comment was marked as outdated.
…<try> eagerly instantiate binders to avoid relying on `sub` The old solver sometimes incorrectly used `sub`, change it to explicitly instantiate binders and use `eq` instead. While doing so I also moved the instantiation before the normalize calls. This caused some observable changes, will explain these inline. This PR therefore requires a crater run and an FCP. r? types
| obligation.cause.span, | ||
| HigherRankedType, | ||
| candidate, | ||
| ); |
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.
By instantiating before we normalize, we allow more projections to be replaced by infer vars if their normalization is ambiguous, e.g. for<'a> <?0 as Trait<'a>>::Assoc stays as is while <?0 as Trait<'!a>>::Assoc gets replaced with an inference variable ?term and result in a nested Projection(<?0 as Trait<'!a>>::Assoc, ?term) goal.
| obligation.cause.span, | ||
| HigherRankedType, | ||
| unnormalized_upcast_trait_ref, | ||
| ); |
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.
same here, can allow additional normalization
| obligation.cause.span, | ||
| HigherRankedType, | ||
| self_ty_trait_ref, | ||
| ); |
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.
and here
| } | ||
| }) | ||
| .map_err(|_| ()) | ||
| ); |
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.
and here
c010b64 to
23e69c0
Compare
|
|
||
| fn build_expression<A: Scalar, B: Scalar, O: Scalar>( | ||
| ) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O { | ||
| cmp_eq |
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 previously compiled because of incompleteness when proving for<'a, 'b> cmp_eq<?0, ?1, ?2>: Fn(A::RefType<'a>, B::RefType<'b>) -> O
This constrained ?0 to A even though there could have been another type for which <?x as Scalar>::RefType<'a> eq <A as Scalar>::RefType<'a>> holds, this is related to #102048 and is fixed because we now normalize for<'a> <?x as Scalar>::RefType<'a> after instantiating the binder, causing it to be replaced with a fresh infer var.
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 believe the (new) behavior is correct -- #108918 (comment).
I don't believe this test should have ever compiled because it relies on the incompleteness of not normalizing aliases with escaping bound regions.
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.
Yes, should have stated so explicitly, this breakage is desired imo
| // | ||
| // The `<Q as AsyncQueryFunction<'?y>>::SendDb`: 'a` bound then results in an error | ||
| // during typeck. | ||
| pub fn get_async<'a>(&'a mut self) { |
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 one is interesting, have to figure out why this test previously compiled
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 previously compiled as in match_poly_trait_ref we used sup which ends up using Sub with lhs and rhs flipped. Not flipping them results in the region infer var of the normalized self type to not be the root var. We then use this self type for the implied bounds, canonicalization of which does opportunistically resolve regions, so the resulting implied bound uses the semantically equal, but not structurally equal root infer var.
Computing the verify bounds for the alias outlives then does a structural equality check which now fails, as we never opportunistically resolve regions in outlives bounds.
The following diff fixes this:
diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs
index c0a99e5cc41..a17b099bf4f 100644
--- a/compiler/rustc_infer/src/infer/outlives/obligations.rs
+++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs
@@ -62,6 +62,7 @@
use crate::infer::outlives::components::{push_outlives_components, Component};
use crate::infer::outlives::env::RegionBoundPairs;
use crate::infer::outlives::verify::VerifyBoundCx;
+use crate::infer::resolve::OpportunisticRegionResolver;
use crate::infer::{
self, GenericKind, InferCtxt, RegionObligation, SubregionOrigin, UndoLog, VerifyBound,
};
@@ -71,6 +72,7 @@
use rustc_middle::traits::query::NoSolution;
use rustc_middle::ty::{self, GenericArgsRef, Region, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{GenericArgKind, PolyTypeOutlivesPredicate};
+use rustc_middle::ty::TypeFoldable;
use rustc_span::DUMMY_SP;
use smallvec::smallvec;
@@ -177,6 +179,7 @@ pub fn process_registered_region_obligations(
.no_bound_vars()
.expect("started with no bound vars, should end with no bound vars");
+ let (sup_type, sub_region) = (sup_type, sub_region).fold_with(&mut OpportunisticRegionResolver::new(self));
debug!(?sup_type, ?sub_region, ?origin);
let outlives = &mut TypeOutlives::new(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.
Does that diff result in any other differences?
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.
See the diagnostic changes in #121743
|
Test changes overall look fine, other than Haven't really reviewed the code changes, but I'll do that soonish, since we have to wait for process anyways. |
| self.infcx | ||
| .at(&obligation.cause, obligation.param_env) | ||
| .sup( | ||
| .eq( |
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.
these changes are currently breaking, changed them to match the new solver for now (but both solvers should probably instantiate eagerly to allow instantiating hr trait objects when upcasting)
we need tests for all of this though 😁
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.
Consequence of this is what? That we can't do:
trait Supertrait<'a, 'b> {}
trait Subtrait: for<'a, 'b> Supertrait<'a, 'b> {}
let upcast: &dyn for<'a> Supertrait<'a, 'a> = todo!() as &dyn Subtrait;
Because of higher-ranked eq?
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.
Nope, the consequence is that it fixes a previously unknown unsoundness 🤣
#![feature(trait_upcasting)]
trait Supertrait<'a, 'b> {
fn cast(&self, x: &'a str) -> &'b str;
}
trait Subtrait<'a, 'b>: Supertrait<'a, 'b> {}
impl<'a> Supertrait<'a, 'a> for () {
fn cast(&self, x: &'a str) -> &'a str {
x
}
}
impl<'a> Subtrait<'a, 'a> for () {}
fn unsound(x: &dyn for<'a> Subtrait<'a, 'a>) -> &dyn for<'a, 'b> Supertrait<'a, 'b> {
x
}
fn transmute<'a, 'b>(x: &'a str) -> &'b str {
unsound(&()).cast(x)
}
fn main() {
let x;
{
let mut temp = String::from("hello there");
x = transmute(temp.as_str());
}
println!("{x}");
}|
@bors try |
|
☀️ Try build successful - checks-actions |
|
@rust-timer build 21bc403 @craterbot check |
This comment has been minimized.
This comment has been minimized.
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
Finished benchmarking commit (21bc403): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 666.39s -> 665.793s (-0.09%) |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
|
650afe8 to
323069f
Compare
compiler-errors
left a comment
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.
r=me when green
|
@bors r+ |
|
☀️ Test successful - checks-actions |
|
Finished benchmarking commit (fd27e87): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDNext Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 668s -> 668.42s (0.06%) |
|
@lcnr I assume that the perf. hit was expected, given that this is a trait solver fix? |
|
yeah. I think so. By being able to replace more associated types with inference vars, the trait solver has to do more work |
|
Ok, marking as triaged. @rustbot label: +perf-regression-triaged |
The old solver sometimes incorrectly used
sub, change it to explicitly instantiate binders and useeqinstead. While doing so I also moved the instantiation before the normalize calls. This caused some observable changes, will explain these inline. This PR therefore requires a crater run and an FCP.r? types