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
Existential types: Unused closure environment is part of concrete type #57678
This isn't a duplicate of that issue-- the error message here is "correct" in a sense. The issue here is that the closure being generated is still generic over type parameters, even when they're unused. We don't have any logic today (AFAIK) for detecting when a type parameter goes unused in a closure, so by default they capture all generic parameters in scope. Desugaring the argument-position impl Copy makes this more obvious:
error: type parameter `A` is part of concrete type but not used in parameter list for existential type
--> src/main.rs:4:19
|
4 | fn test<A>() -> T {
| ___________________^
5 | | || {}
6 | | }
| |_^
The fix here is to detect when a type parameter is unused in a closure and make it so that the closure is no longer generic over that parameter. I remember talking with @nikomatsakis about this in the past and I think they indicated that this was hard to do in the current system.
That would indeed be quite hard to do. For one thing, the set of generics is determined quite early on -- this would require us to wait until we see what the closure does. But also, it's non-trivial to figure out when the type parameter was important to the closure.
I guess I'll close this for now as "working as intended", though it'd definitely be nice to drop unnecessary type parameters from closures anyways (for codegen purposes).
Activity
Centril commentedon Jan 17, 2019
cc @cramertj @alexreg @nikomatsakis
alexreg commentedon Jan 17, 2019
I'm confident this is a duplicate of #42940, but let's wait for others to confirm.
cramertj commentedon Jan 17, 2019
This isn't a duplicate of that issue-- the error message here is "correct" in a sense. The issue here is that the closure being generated is still generic over type parameters, even when they're unused. We don't have any logic today (AFAIK) for detecting when a type parameter goes unused in a closure, so by default they capture all generic parameters in scope. Desugaring the argument-position
impl Copymakes this more obvious:The fix here is to detect when a type parameter is unused in a closure and make it so that the closure is no longer generic over that parameter. I remember talking with @nikomatsakis about this in the past and I think they indicated that this was hard to do in the current system.
nikomatsakis commentedon Jan 22, 2019
That would indeed be quite hard to do. For one thing, the set of generics is determined quite early on -- this would require us to wait until we see what the closure does. But also, it's non-trivial to figure out when the type parameter was important to the closure.
cramertj commentedon Jan 22, 2019
I guess I'll close this for now as "working as intended", though it'd definitely be nice to drop unnecessary type parameters from closures anyways (for codegen purposes).