Skip to content
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

unhelpful error message when declaring constant with type parameter's associated constant #53241

Closed
tspiteri opened this issue Aug 10, 2018 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tspiteri
Copy link
Contributor

For this code:

trait Foo {
    const BAR: i32;
}
impl Foo for i32 {
    const BAR: i32 = 42;
}
fn baz<F: Foo>(a: F) {
    const QUX: i32 = F::BAR;
    println!("{}", QUX);
}
fn main() {
    baz(1i32);
}

I get the error message:

| fn baz<F: Foo>(a: F) {
|    --- - type variable from outer function
|    |
|    try adding a local type parameter in this method instead
|     const QUX: i32 = F::BAR;
|                      ^^^^^^ use of type variable from outer function

QUX is not a method and cannot have local type parameters; I don't think constants can be generic. So the error message is confusing.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 10, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Aug 13, 2018

Additionally, the try adding a local type parameter in this method instead is pointing to the outer function instead of the item that is supposed to become generic

@estebank
Copy link
Contributor

Current output:

error[E0401]: can't use generic parameters from outer function
 --> src/main.rs:8:22
  |
7 | fn baz<F: Foo>(a: F) {
  |        - type parameter from outer function
8 |     const QUX: i32 = F::BAR;
  |                      ^^^^^^ use of generic parameter from outer function

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 14, 2020
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@sgued
Copy link
Contributor

sgued commented Aug 3, 2022

I still found the current error message confusing. In my case it wasn't with a variable but within the type parameter:

fn testing<T>() {
    const TESTING: [T; 0] = [];
}

gives the following error:

error[[E0401]](https://doc.rust-lang.org/nightly/error-index.html#E0401): can't use generic parameters from outer function
 --> src/main.rs:3:21
  |
2 | fn testing<T>() {
  |            - type parameter from outer function
3 |     const TESTING: [T; 0] = [];
  |                     ^ use of generic parameter from outer function

For more information about this error, try `rustc --explain E0401`.

rustc --explain E0401 doesn't mention consts or statics, it seems to me that it should.

@estebank estebank added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Aug 4, 2022
@Dylan-DPC
Copy link
Member

Dylan-DPC commented Aug 25, 2024

Current output:

error[E0401]: can't use generic parameters from outer item
 --> src/main.rs:8:22
  |
7 | fn baz<F: Foo>(a: F) {
  |        - type parameter from outer item
8 |     const QUX: i32 = F::BAR;
  |                      ^^^^^^ use of generic parameter from outer item
  |
  = note: a `const` is a separate item from the item that contains it

warning: unused variable: `a`
 --> src/main.rs:7:16
  |
7 | fn baz<F: Foo>(a: F) {
  |                ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants