-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #113108 - compiler-errors:normalize-opaques-with-late-b…
…ound-vars-again, r=jackh726 Normalize opaques with late-bound vars again We have a hack in the compiler where if an opaque has escaping late-bound vars, we skip revealing it even though we *could* reveal it from a technical perspective. First of all, this is weird, since we really should be revealing all opaques in `Reveal::All` mode. Second of all, it causes subtle bugs (linked below). I attempted to fix this in #100980, which was unfortunately reverted due to perf regressions on codebases that used really deeply nested futures in some interesting ways. The worst of which was #103423, which caused the project to hang on build. Another one was #104842, which was just a slow-down, but not a hang. I took some time afterwards to investigate how to rework `normalize_erasing_regions` to take advantage of better caching, but that effort kinda fizzled out (#104133). However, recently, I was made aware of more bugs whose root cause is not revealing opaques during codegen. That made me want to fix this again -- in the process, interestingly, I took the the minimized example from #103423 (comment), and it doesn't seem to hang any more... Thinking about this harder, there have been some changes to the way we lower and typecheck async futures that may have reduced the pathologically large number of outlives obligations (see description of #103423) that we were encountering when normalizing opaques with bound vars the last time around: * #104321 (lower `async { .. }` directly as a generator that implements `Future`, removing the `from_generator` shim) * #104833 (removing an `identity_future` fn that was wrapping desugared future generators) ... so given that I can see: * No significant regression on rust perf bot (#107620 (comment)) * No timeouts in crater run I did (#107620 (comment), rechecked failing crates in #107620 (comment)) ... and given that this PR: * Fixes #104601 * Fixes #107557 * Fixes #109464 * Allows us to remove a `DefiningAnchor::Bubble` from codegen (75a8f68) I'm inclined to give this another shot at landing this. Best case, it just works -- worst case, we get more examples to study how we need to improve the compiler to make this work. r? types
- Loading branch information
Showing
5 changed files
with
95 additions
and
24 deletions.
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
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
64 changes: 64 additions & 0 deletions
64
tests/ui/async-await/in-trait/normalize-opaque-with-bound-vars.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,64 @@ | ||
// build-pass | ||
// edition:2021 | ||
// compile-flags: -Cdebuginfo=2 | ||
|
||
// We were not normalizing opaques with escaping bound vars during codegen, | ||
// leading to later errors during debuginfo computation. | ||
|
||
#![feature(async_fn_in_trait)] | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct SharedState {} | ||
|
||
pub trait State { | ||
async fn execute(self, shared_state: &SharedState); | ||
} | ||
|
||
pub trait StateComposer { | ||
fn and_then<T, F>(self, map_fn: F) -> AndThen<Self, F> | ||
where | ||
Self: State + Sized, | ||
T: State, | ||
F: FnOnce() -> T, | ||
{ | ||
AndThen { previous: self, map_fn } | ||
} | ||
} | ||
|
||
impl<T> StateComposer for T where T: State {} | ||
pub struct AndThen<T, F> { | ||
previous: T, | ||
map_fn: F, | ||
} | ||
|
||
impl<T, U, F> State for AndThen<T, F> | ||
where | ||
T: State, | ||
U: State, | ||
F: FnOnce() -> U, | ||
{ | ||
async fn execute(self, shared_state: &SharedState) | ||
where | ||
Self: Sized, | ||
{ | ||
self.previous.execute(shared_state).await; | ||
(self.map_fn)().execute(shared_state).await | ||
} | ||
} | ||
|
||
pub struct SomeState {} | ||
|
||
impl State for SomeState { | ||
async fn execute(self, shared_state: &SharedState) {} | ||
} | ||
|
||
pub fn main() { | ||
let shared_state = SharedState {}; | ||
async { | ||
SomeState {} | ||
.and_then(|| SomeState {}) | ||
.and_then(|| SomeState {}) | ||
.execute(&shared_state) | ||
.await; | ||
}; | ||
} |
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 @@ | ||
// build-pass | ||
// edition:2021 | ||
// compile-flags: -Cdebuginfo=2 | ||
|
||
// We were not normalizing opaques with escaping bound vars during codegen, | ||
// leading to later linker errors because of differences in mangled symbol name. | ||
|
||
fn func<T>() -> impl Sized {} | ||
|
||
trait Trait<'a> { | ||
type Assoc; | ||
|
||
fn call() { | ||
let _ = async { | ||
let _value = func::<Self::Assoc>(); | ||
std::future::ready(()).await | ||
}; | ||
} | ||
} | ||
|
||
impl Trait<'static> for () { | ||
type Assoc = (); | ||
} | ||
|
||
fn main() { | ||
<()>::call(); | ||
} |