Skip to content

Conversation

@lcnr
Copy link
Contributor

@lcnr lcnr commented Jan 11, 2024

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

@lcnr
Copy link
Contributor Author

lcnr commented Jan 11, 2024

@bors try

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 11, 2024
@lcnr lcnr added the needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. label Jan 11, 2024
@bors

This comment was marked as outdated.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 11, 2024
…<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,
);
Copy link
Contributor Author

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,
);
Copy link
Contributor Author

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,
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

}
})
.map_err(|_| ())
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

@lcnr lcnr force-pushed the eagerly-instantiate-binders branch from c010b64 to 23e69c0 Compare January 11, 2024 12:41

fn build_expression<A: Scalar, B: Scalar, O: Scalar>(
) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O {
cmp_eq
Copy link
Contributor Author

@lcnr lcnr Jan 11, 2024

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.

Copy link
Member

@compiler-errors compiler-errors Jan 11, 2024

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.

Copy link
Contributor Author

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) {
Copy link
Contributor Author

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

Copy link
Contributor Author

@lcnr lcnr Feb 27, 2024

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(

Copy link
Member

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?

Copy link
Member

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

@jackh726
Copy link
Member

Test changes overall look fine, other than gluon_salsa that I have to think more about.

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(
Copy link
Contributor Author

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 😁

Copy link
Member

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?

Copy link
Contributor Author

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}");
}

@lcnr
Copy link
Contributor Author

lcnr commented Jan 12, 2024

@bors try

@bors
Copy link
Collaborator

bors commented Jan 12, 2024

⌛ Trying commit 23e69c0 with merge 21bc403...

@bors
Copy link
Collaborator

bors commented Jan 12, 2024

☀️ Try build successful - checks-actions
Build commit: 21bc403 (21bc403d557a2516df70ea80fb19b94177beede5)

@lcnr
Copy link
Contributor Author

lcnr commented Jan 12, 2024

@rust-timer build 21bc403

@craterbot check

@rust-timer

This comment has been minimized.

@craterbot
Copy link
Collaborator

👌 Experiment pr-119849 created and queued.
🤖 Automatically detected try build 21bc403
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 12, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (21bc403): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Benchmarking 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 @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.5% [0.2%, 0.9%] 13
Regressions ❌
(secondary)
1.2% [0.3%, 1.5%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.5%, -1.8%] 6
All ❌✅ (primary) 0.5% [0.2%, 0.9%] 13

Max RSS (memory usage)

Results

This 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.

mean range count
Regressions ❌
(primary)
1.3% [1.3%, 1.3%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-3.3%, -3.3%] 1
All ❌✅ (primary) 1.3% [1.3%, 1.3%] 2

Cycles

Results

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.7% [1.7%, 1.7%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 666.39s -> 665.793s (-0.09%)
Artifact size: 308.38 MiB -> 308.37 MiB (-0.00%)

@rustbot rustbot added the perf-regression Performance regression. label Jan 12, 2024
@craterbot
Copy link
Collaborator

🚧 Experiment pr-119849 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-119849 is completed!
📊 1539 regressed and 5 fixed (407280 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot removed the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Jan 17, 2024
@compiler-errors compiler-errors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 12, 2024
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Mar 14, 2024
@lcnr lcnr force-pushed the eagerly-instantiate-binders branch from 650afe8 to 323069f Compare March 14, 2024 16:19
Copy link
Member

@compiler-errors compiler-errors left a 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

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 14, 2024

📌 Commit c8f0f17 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 14, 2024
@bors
Copy link
Collaborator

bors commented Mar 14, 2024

⌛ Testing commit c8f0f17 with merge fd27e87...

@bors
Copy link
Collaborator

bors commented Mar 14, 2024

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing fd27e87 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 14, 2024
@bors bors merged commit fd27e87 into rust-lang:master Mar 14, 2024
@rustbot rustbot added this to the 1.78.0 milestone Mar 14, 2024
@lcnr lcnr deleted the eagerly-instantiate-binders branch March 14, 2024 22:00
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (fd27e87): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.2%, 1.1%] 14
Regressions ❌
(secondary)
0.5% [0.4%, 1.0%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.2% [-1.4%, -1.1%] 6
All ❌✅ (primary) 0.7% [0.2%, 1.1%] 14

Max RSS (memory usage)

Results

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
-3.8% [-3.8%, -3.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.8% [-3.8%, -3.8%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 668s -> 668.42s (0.06%)
Artifact size: 311.58 MiB -> 311.47 MiB (-0.04%)

@Kobzol
Copy link
Member

Kobzol commented Mar 19, 2024

@lcnr I assume that the perf. hit was expected, given that this is a trait solver fix?

@lcnr
Copy link
Contributor Author

lcnr commented Mar 19, 2024

yeah. I think so. By being able to replace more associated types with inference vars, the trait solver has to do more work

@Kobzol
Copy link
Member

Kobzol commented Mar 19, 2024

Ok, marking as triaged.

@rustbot label: +perf-regression-triaged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-testsuite Area: The testsuite used to check the correctness of rustc disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.