You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Strange borrowing issue in inherent async method. #68950
Not sure whether it's a bug, but it is indeed strange...
structA;// this compilesfntest(s:&mutA,arg:&str,handler:Box<dynFn() + 'static>){}// this doesn'timplA{asyncfntest2(&mutself,arg:&str,handler:Box<dynFn() + 'static>){}}
test2 errors with:
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
--> src/lib.rs:8:76
|
8 | async fn test2(&mut self, arg: &str, handler: Box<dyn Fn() + 'static>) {
| ^
|
This appears to have the same underlying cause as #64552 (which @csmoe is working on in #68362).
When you write handler: Box<dyn FnOnce() + 'static> (or handler: Box<dyn FnOnce()>, which desugars to the same thing), the AST lowering doesn't create a lifetime parameter in the created opaque type, since you don't need a lifetime parameter for 'static. However,
However, when we create the generator type, we forget that the region is 'static:
Thus, we end up trying to process a member constraint for some newly created region _#0r. Since by construction we have no constraints for it, we cannot prove that it is equal to any of the regions in the member constraint (even though it's "really" 'static).
As a workaround, we could special-case 'static during the creation of the generator type, and leave it as-is instead of creating a fresh late-bound region.
As in #64552, I believe the full solution is for generator types to carry 'extra' region constraints with them in some form, so that we don't lose the lexical region information that we had prior to the lowering.
Activity
crlf0710 commentedon Feb 8, 2020
@rustbot modify labels to +A-async-await +C-bug +T-compiler
crlf0710 commentedon Feb 8, 2020
Seems similar to #68759 ....
Aaron1011 commentedon Feb 11, 2020
This appears to have the same underlying cause as #64552 (which @csmoe is working on in #68362).
When you write
handler: Box<dyn FnOnce() + 'static>(orhandler: Box<dyn FnOnce()>, which desugars to the same thing), the AST lowering doesn't create a lifetime parameter in the created opaque type, since you don't need a lifetime parameter for'static. However,However, when we create the generator type, we forget that the region is
'static:rust/src/librustc_typeck/check/generator_interior.rs
Lines 124 to 144 in 9b9d2af
Thus, we end up trying to process a member constraint for some newly created region
_#0r. Since by construction we have no constraints for it, we cannot prove that it is equal to any of the regions in the member constraint (even though it's "really"'static).As a workaround, we could special-case
'staticduring the creation of the generator type, and leave it as-is instead of creating a fresh late-bound region.As in #64552, I believe the full solution is for generator types to carry 'extra' region constraints with them in some form, so that we don't lose the lexical region information that we had prior to the lowering.
tmandry commentedon Mar 3, 2020
Thanks @Aaron1011. I'm going to close this as a duplicate of #64552, but note on the issue that we should add a test with the code from this issue.