Type inference for closure return types #1202
y1ca1
started this conversation in
Feature requests
Replies: 2 comments 1 reply
-
|
[retreat 2025] @y1ca1 is this still relevant? |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
If the type is very long an inconvenient to write, you could use this workaround: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The problem
Consider the following simple closure:
The use of type hole
_here is valid in both Rust and Verus. However, when we want to add spec to the closure, one has to explicitly annotate the return type likelet ok1 = |x: u8| -> (o: u8) ensures x == o { x }. Otherwise, Verus complains.Verus outputs:
Why care
Type inference for closure return types is especially useful when it comes to nested closures. Consider the following valid Rust/Verus code:
The inferred closure type is
impl Fn(u8) -> impl Fn(u8) -> (u8, u8). Specifically, the type of the inner closure (or the return type of the outer closure) isimpl Fn(u8) -> (u8, u8), which is not allowed to be written explicitly in Rust for reasons I have yet to explore.So there is no way if we want to add some specs for a nested closure like:
Link to code
I am curious if it's inherently hard to support this feature/there's any workaround/pointers to code if I decide to make a PR? Thanks!
Beta Was this translation helpful? Give feedback.
All reactions