New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested function scoped type parameters #3562
Conversation
Apologies, I do not know how to assign tags. I believe this proposal should at least have |
Would this also allow using type parameters from an outer function inside the inner function's body, even when they do not appear in the inner function's signature? E.g., fn outer<T: Default>() {
fn inner() {
let _ = T::default();
}
} This would mean that the compiler has to inspect Also, how does this interact with attributes that require a non-generic function, like Finally, should |
No, I am not proposing to allow that example, for the exact reasons you've raised. I'm unsure what you mean by "interacting with With regards to |
I believe they meant something like this: fn foo<T>(x: T) -> T {
// This attribute requires `bar` to not be generic, but this then requires determining that doesn't use `T`
#[no_mangle]
fn bar(y: i32) -> i32 {
y
}
// This cannot be allowed, but notice how it has the same signature as `bar`!
#[no_mangle]
fn baz(y: i32) -> i32 {
T::some_function(y)
}
bar(x)
}
IMO it would be confusing if functions can use generic parameters declared outside and |
I would propose to reject both of your examples, noting the same reasoning that @Jules-Bertholet provided. Whether a function can be Correct me if i'm wrong, but Structs being able to utilize this seems reasonable (although perhaps not productive? may encourage longer functions using more internal structs - happy to take further comment on this one) However, traits and impls would probably not benefit from this, and further complicating trait resolution seems a fool's errand. I do not want to "half-arse" this feature, but these two seem much more work then they're worth (I've also never seen a local trait) |
It might be helpful to add the examples and rationale to the RFC. It could go in the rationale and alternatives section, but I'm not sure if that's the best place. |
Co-authored-by: teor <teor@riseup.net>
I will, yes - I've ran into some busyness, but I'll do it when I find the time. |
Allow type parameters to be used in nested functions. Removes E0401.
This error was first introduced (at the latest) in 2015. After almost ten years of development and changes in Rust, I believe it's time to reconsider it.
TLDR: Allow the below.
Rendered