Skip to content

Confusing diagnostic when improperly using constant as type. #81508

Closed
@rylev

Description

@rylev

The following code...

#[allow(non_upper_case_globals)]
pub const Foo: &str = "";

mod submod {
    use super::Foo;
    fn function() {
        println!("{}", Foo::Bar);
    }
}

...yields the following error and warning:

error[E0433]: failed to resolve: use of undeclared type `Foo`
 --> src/lib.rs:7:24
  |
7 |         println!("{}", Foo::Bar);
  |                        ^^^ use of undeclared type `Foo`

warning: unused import: `super::Foo`
 --> src/lib.rs:5:9
  |
5 |     use super::Foo;
  |         ^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

The warning says that Foo is not used but it clearly is, just not properly. This error is normally easier to spot when using the proper casing for constants, but that might not always be the case. Perhaps in this instance, we should look for other items with the exact same name and point out that the user is trying to use a constant as a type.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions