-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Using type
with local type parameters has confusing error
#37892
Comments
Why is this disallowed? |
Nested items are independent from their parent item for everything except for privacy and name resolution. This includes type parameters. The suggestion is in fact correct - this can be fixed by something like struct Foo;
impl Foo {
fn method<T>(&self) {
type X<This, T> = (This, T);
}
}
fn main() { } |
I don't think the user will understand |
Hi @arielb1, |
cc #47574 error[E0401]: can't use type parameters from outer function
--> src/main.rs:6:19
|
4 | impl Foo {
| ---- `Self` type implicitly declared here, by this `impl`
5 | fn method<T>(&self) {
6 | type X = (Self, T);
| ^^^^
| |
| use of type variable from outer function
| use a type here instead
error[E0401]: can't use type parameters from outer function
--> src/main.rs:6:25
|
5 | fn method<T>(&self) {
| ------ - type variable from outer function
| |
| try adding a local type parameter in this method instead
6 | type X = (Self, T);
| ^ use of type variable from outer function
error: aborting due to 2 previous errors |
It seems to me what this is missing is a note explaining the same thing as #37892 (comment). |
Just had the same issue, just a little different implementation: enum MyEnum {}
impl MyEnum {
fn do_something(result: impl FnOnce()) {
result();
}
fn do_something_extra() {
fn inner() {
Self::do_something(move || {}); // can't use generic parameters from outer function E0401
MyEnum::do_something(move || {}); // no error
}
inner();
}
} It seams that accessing |
Using
type
with local type parameters orSelf
should have a clearer error.Reproduce with: (playpen link)
Current errors have a suggestion ("try..") that doesn't apply.
The text was updated successfully, but these errors were encountered: