Reduced example:
trait TraitF {
fn f();
}
trait TraitG {
fn g();
}
struct Foo<T>(T);
impl<T> Foo<T> {
fn foo() {}
fn bar() {
struct Bar<F, T>(F, T);
impl<F, T> TraitF for Bar<F, T>
where
F: FnOnce(T),
{
fn f() {}
}
impl<F> TraitG for Bar<F>
where
F: FnOnce(T),
{
fn g() {}
}
}
}
(Playground)
Errors:
Compiling playground v0.0.1 (/playground)
error[E0401]: can't use generic parameters from outer function
--> src/lib.rs:22:23
|
10 | impl<T> Foo<T> {
| - type parameter from outer function
...
18 | fn f() {}
| - try adding a local generic parameter in this method instead
...
22 | F: FnOnce(T),
| ^ use of generic parameter from outer function
error[E0107]: wrong number of type arguments: expected 2, found 1
--> src/lib.rs:20:28
|
20 | impl<F> TraitG for Bar<F>
| ^^^^^^ expected 2 type arguments
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0107, E0401.
For more information about an error, try `rustc --explain E0107`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
This came up during refactoring, while I was adding the parameter T to Bar (intentionally named the same as the outer T because they are going to be instantiated to the same type anyways). This kind of error message (the first one, E0401) had me puzzled for some time before figuring out that it was nonsensical. I’m referring to the try adding a local generic parameter in this method instead part. The real code of course was quite a bit longer, making the situation actually confusing, in particular:
- The
try adding ... hint points to the wrong place entirely. It should suggest adding a parameter to the impl TraitG (even though such a suggestion might not always make sense, idk).
- The
type parameter from outer function pointer is also somewhat confusing, since when foo’s body is a few lines and bar is super long and nested, it is quite nontrivial to even figure out that “outer function” refers to bar(). (As in: it is easy to forget that you’re even inside of a function body at all if you’ve been focusing on this Bar struct and it’s impls for a while.) Also... what is the distinction between the words function and method here? (My original code has bar(self) but still called it “outer function”.)
@rustbot modify labels: A-diagnostics, T-compiler, D-confusing, C-bug.
Reduced example:
(Playground)
Errors:
This came up during refactoring, while I was adding the parameter
TtoBar(intentionally named the same as the outerTbecause they are going to be instantiated to the same type anyways). This kind of error message (the first one,E0401) had me puzzled for some time before figuring out that it was nonsensical. I’m referring to thetry adding a local generic parameter in this method insteadpart. The real code of course was quite a bit longer, making the situation actually confusing, in particular:try adding ...hint points to the wrong place entirely. It should suggest adding a parameter to theimpl TraitG(even though such a suggestion might not always make sense, idk).type parameter from outer functionpointer is also somewhat confusing, since whenfoo’s body is a few lines andbaris super long and nested, it is quite nontrivial to even figure out that “outer function” refers tobar(). (As in: it is easy to forget that you’re even inside of a function body at all if you’ve been focusing on thisBarstruct and it’simpls for a while.) Also... what is the distinction between the wordsfunctionandmethodhere? (My original code hasbar(self)but still called it “outer function”.)@rustbot modify labels: A-diagnostics, T-compiler, D-confusing, C-bug.