-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemC-bugCategory: This is a bug.Category: This is a bug.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.This change is large or controversial enough that it should have an RFC accepted before doing it.
Description
fn foo<T:Default>() {
const bar : T = T::default();
}
gives the following error (twice for some reason):
$ cargo +nightly build
Compiling arrys_sizeoof v0.1.0 (C:\work\trash\repros\arrys_sizeoof)
error[E0401]: can't use type parameters from outer function
--> src\lib.rs:2:17
|
1 | fn foo<T:Default>() {
| --- - type variable from outer function
| |
| try adding a local type parameter in this method instead
2 | const bar : T = T::default();
| ^ use of type variable from outer function
error[E0401]: can't use type parameters from outer function
--> src\lib.rs:2:21
|
1 | fn foo<T:Default>() {
| --- - type variable from outer function
| |
| try adding a local type parameter in this method instead
2 | const bar : T = T::default();
| ^^^^^^^^^^ use of type variable from outer function
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0401`.
error: Could not compile `arrys_sizeoof`.
To learn more, run the command again with --verbose.
Changing let to const
to let
makes it compile just fine.
Another, slightly different example:
fn get_size<T:Sized>() {
const size : usize = std::mem::size_of::<T>();
}
Here, the type of the variable is non-generic now, and it's only a function that is. Results in the same error, but only once now:
$ cargo +nightly build
Compiling arrys_sizeoof v0.1.0 (C:\work\trash\repros\arrys_sizeoof)
error[E0401]: can't use type parameters from outer function
--> src\lib.rs:2:46
|
1 | fn get_size<T:Sized>() {
| -------- - type variable from outer function
| |
| try adding a local type parameter in this method instead
2 | const size : usize = std::mem::size_of::<T>();
| ^ use of type variable from outer function
error: aborting due to previous error
For more information about this error, try `rustc --explain E0401`.
error: Could not compile `arrys_sizeoof`.
To learn more, run the command again with --verbose.
P.s.: if this is the desired behavior, it is confusing, since 1) changing const
to let
should not result in type error, 2) rustc --explain E0401
doesn't mention anything close - all the examples are about definition of new functions/types inside the function body (and it's unclear how to adapt that knowledge to this particular use case).
P.p.s.: the behavior is exactly the same on current stable (1.32.0)
zzau13, toiglak, VladasZ, bewee-i, Ternvein and 3 more
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemC-bugCategory: This is a bug.Category: This is a bug.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.This change is large or controversial enough that it should have an RFC accepted before doing it.