Skip to content

Can't assign results of generic functions to const variables #57775

Open
@moxian

Description

@moxian
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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.T-langRelevant to the language team, which will review and decide on the PR/issue.T-typesRelevant 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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions