Skip to content

[ER] Better than the "unresolved import super::K" error message #140449

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

Open
leonardo-m opened this issue Apr 29, 2025 · 3 comments
Open

[ER] Better than the "unresolved import super::K" error message #140449

leonardo-m opened this issue Apr 29, 2025 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

Code

For wrong code like this:


fn foo() {
    const K: usize = 1;

    mod inner {
        use super::K;

        fn bar() {
            dbg!(K);
        }
    }
}
fn main() {}

Current output

rustc 1.88.0-nightly (25cdf1f67 2025-04-28) currently gives:


error[E0432]: unresolved import `super::K`
 --> ...\test.rs:5:13
  |
5 |         use super::K;
  |             ^^^^^^^^ no `K` in the root

error: aborting due to 1 previous error

Desired output

But I'd like an error message that tries to suggest how to correctly import the name X.

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0-nightly (25cdf1f67 2025-04-28)
binary: rustc
commit-hash: 25cdf1f67463c9365d8d83778c933ec7480e940b
commit-date: 2025-04-28
host: x86_64-pc-windows-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

No response

@leonardo-m leonardo-m added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 29, 2025
@Urgau
Copy link
Member

Urgau commented Apr 29, 2025

But I'd like an error message that tries to suggest how to correctly import the name X.

I can think of multiple "suggestion", but they are all invasive, which would you like rustc to suggest?

  • Move const K: usize = 1; out-side the function
  • Remove the intermediate mod inner
  • Pass K as a parameter

In any case we should note that items defined inside a function are local to that function and are not accessible from nested modules.

@leonardo-m
Copy link
Author

In any case we should note that items defined inside a function are local to that function and are not accessible from nested modules.<

I agree, that's the basic improvement, it could be enough.

But can't we offer a way to access the items defined inside functions from modules defined inside them? :-)

which would you like rustc to suggest?<

Perhaps none of them.

@xizheyin
Copy link
Contributor

note that items defined inside a function are local to that function and are not accessible from nested modules.

I don't know if the compiler can detect that K is defined inside the function foo, if it can it can give note in the diagnostics.
If can not, where would be a better place to do this prompt, perhaps in the documentation for E0432?

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 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

3 participants