Tie boxed future to the receiver lifetime for &self-only methods#298
Open
ashi009 wants to merge 1 commit into
Open
Tie boxed future to the receiver lifetime for &self-only methods#298ashi009 wants to merge 1 commit into
ashi009 wants to merge 1 commit into
Conversation
When the receiver is the only borrowed input and the method has no generic parameters, emit fn m<'life0>(&'life0 self) -> Pin<Box<dyn Future + Send + 'life0>> instead of introducing 'async_trait with 'life0: 'async_trait and Self: 'async_trait outlives bounds. Those bounds defeat the trait solver's global Send/Sync cache, so the captured state is re-proven once per impl (rust-lang/rust#157595). Eligibility is decided from the signature alone because the trait and its impls expand in separate macro invocations and must agree on whether 'life0 is late-bound (E0195).
2dd9a6d to
cceb5cf
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #297; works around rust-lang/rust#157595.
For a method whose only borrowed input is the receiver and which has no generic parameters, the boxed future is now tied to the receiver's lifetime and no
'async_traitis emitted:The two forms are semantically identical — the future still borrows
self, neither is'static— but the avoided outlives bounds defeat the trait solver's globalSend/Synccache, re-proving the captured state once per impl. One trait, 300&self-only impls over a deepArc<Mutex<Vec<…>>>state, rustc 1.96.0:evaluate_obligationtransform_sigskips the'async_traitparam and its outlives bounds for eligible signatures and reuses the receiver's'life0on the future; the inferredSend/Syncbound for default methods is kept (Self: 'life0is implied by&'life0 self)&self/&mut self/self: &Self) as the sole reference input, no generic/const params, noimpl Traitarg, nothing naming'async_trait(newNeedsAsyncTraitvisitor) — because the trait and its impls expand in separate invocations and must agree on whether'life0is late-boundregion_free_receiver_lifetimetest pins the eligible forms (&mut self, borrowing return, owned args, default body);tests/ui/lifetime-span.stderrre-blessed (spans shift on already-invalid code)Limitation. The receiver lifetime becomes late-bound for eligible methods. The one observable break: a trait method written with
where …: 'async_traitwhose impl omits the clause now fails with E0195 (the two sides disagree on early/late-bound). Loud compile error, not UB. GitHub search finds no code with that combination:where Self: 'async_traithas zero hits, and the ~7 repos usingwhere T: 'async_traitall pair it with shapes the gate already excludes.