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
Using type with local type parameters has confusing error #37892
Current errors have a suggestion ("try..") that doesn't apply.
error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
--> <anon>:6:19
|
6 | type X = (Self,T);
| ^^^^ use of type variable from outer function
error[E0401]:can't use type parameters from outer function; try using a local type parameter instead
--> <anon>:6:25
|
6 | type X = (Self,T);
| ^ use of type variable from outer function
I don't think the user will understand This in type X<This> to be a "local type parameter"; I didn't. If that's just the issue, it could call it something else.
Hi @arielb1,
I meant that I think that type X = (Self, T) in a function's body can be useful.
You said that nested items are independent. What's a reason?
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
Just had the same issue, just a little different implementation:
enumMyEnum{}implMyEnum{fndo_something(result:implFnOnce()){result();}fndo_something_extra(){fninner(){Self::do_something(move || {});// can't use generic parameters from outer function E0401MyEnum::do_something(move || {});// no error}inner();}}
It seams that accessing Self from inner functions is not allowed. This doesn't seem logical and error is way too confusing (or probably describing something else completely).
After changes on the above linked PR and follow up changes I'll publish after that lands, for the original report:
error[E0401]: can't use `Self` from outer item
--> f16.rs:5:19
|
3 | impl Foo {
| ---- `Self` type implicitly declared here, by this `impl`
4 | fn method<T>(&self) {
5 | type X = (Self, T);
| - ^^^^ use of `Self` from outer item
| |
| `Self` used in this inner type alias
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: refer to the type directly here instead
|
5 - type X = (Self, T);
5 + type X = (Foo, T);
|
error[E0401]: can't use generic parameters from outer item
--> f16.rs:5:25
|
4 | fn method<T>(&self) {
| - type parameter from outer item
5 | type X = (Self, T);
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner type alias
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
5 | type X<T> = (Self, T);
| +++
For the case above.
error[E0401]: can't use `Self` from outer item
--> f16.rs:17:13
|
10 | impl MyEnum {
| ---- `Self` type implicitly declared here, by this `impl`
...
16 | fn inner() {
| ----- `Self` used in this inner function
17 | Self::do_something(move || {}); // can't use generic parameters from outer function E0401
| ^^^^ use of `Self` from outer item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: refer to the type directly here instead
|
17 - Self::do_something(move || {}); // can't use generic parameters from outer function E0401
17 + MyEnum::do_something(move || {}); // can't use generic parameters from outer function E0401
|
Activity
KalitaAlexey commentedon Dec 5, 2016
Why is this disallowed?
arielb1 commentedon Dec 5, 2016
@KalitaAlexey
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
bluss commentedon Dec 5, 2016
I don't think the user will understand
Thisintype X<This>to be a "local type parameter"; I didn't. If that's just the issue, it could call it something else.KalitaAlexey commentedon Dec 5, 2016
Hi @arielb1,
I meant that I think that
type X = (Self, T)in a function's body can be useful.You said that nested items are independent. What's a reason?
csmoe commentedon Sep 17, 2018
cc #47574
current error message:
estebank commentedon Oct 11, 2019
It seems to me what this is missing is a note explaining the same thing as #37892 (comment).
MatrixDev commentedon Feb 10, 2022
Just had the same issue, just a little different implementation:
It seams that accessing
Selffrom inner functions is not allowed. This doesn't seem logical and error is way too confusing (or probably describing something else completely).Rollup merge of rust-lang#148370 - estebank:outer-param, r=JonathanBr…
estebank commentedon Nov 3, 2025
After changes on the above linked PR and follow up changes I'll publish after that lands, for the original report:
For the case above.
Unrolled build for #148370
Rollup merge of rust-lang#148447 - estebank:outer-param-2, r=jackh726
Rollup merge of rust-lang#148447 - estebank:outer-param-2, r=jackh726
Unrolled build for #148447
Rollup merge of #148370 - estebank:outer-param, r=JonathanBrouwer
Rollup merge of #148370 - estebank:outer-param, r=JonathanBrouwer