Skip to content

Confusing diagnostic when improperly using constant as type. #81508

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

Closed
rylev opened this issue Jan 29, 2021 · 9 comments · Fixed by #83669
Closed

Confusing diagnostic when improperly using constant as type. #81508

rylev opened this issue Jan 29, 2021 · 9 comments · Fixed by #83669
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rylev
Copy link
Member

rylev commented Jan 29, 2021

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.

@rylev rylev added A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jan 29, 2021
@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 29, 2021
@estebank
Copy link
Contributor

Every single one of these errors should point at similarly named things of the wrong namespace.

@estebank estebank added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jan 29, 2021
@henryboisdequin
Copy link
Contributor

Do you know where in the code this is coming from?

@estebank
Copy link
Contributor

@henryboisdequin running the following will tell you where the error is being emitted:

% RUST_BACKTRACE=1 rustc +dev f5.rs -Ztreat-err-as-bug
error[E0433]: failed to resolve: use of undeclared type `Foo`
 --> f5.rs:7:24
  |
7 |         println!("{}", Foo::Bar);
  |                        ^^^ use of undeclared type `Foo`

thread 'rustc' panicked at 'aborting due to `-Z treat-err-as-bug=1`', compiler/rustc_errors/src/lib.rs:990:27
stack backtrace:
   0: std::panicking::begin_panic
   1: rustc_errors::HandlerInner::emit_diagnostic
   2: rustc_errors::Handler::emit_diagnostic
   3: rustc_errors::diagnostic_builder::DiagnosticBuilder::emit
   4: rustc_resolve::diagnostics::<impl rustc_resolve::Resolver>::report_error
   5: rustc_resolve::late::LateResolutionVisitor::report_error
   6: rustc_resolve::late::LateResolutionVisitor::smart_resolve_path_fragment
   7: rustc_resolve::late::LateResolutionVisitor::resolve_expr
   8: rustc_resolve::late::LateResolutionVisitor::resolve_expr
   9: rustc_ast::visit::walk_expr
  10: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  11: rustc_ast::visit::walk_expr
  12: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  13: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  14: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  15: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  16: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_block
  17: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  18: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_block
  19: rustc_resolve::late::LateResolutionVisitor::with_rib
  20: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_fn
  21: rustc_ast::visit::walk_item
  22: rustc_resolve::late::LateResolutionVisitor::with_generic_param_rib
  23: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_item
  24: rustc_ast::visit::walk_item
  25: rustc_resolve::late::LateResolutionVisitor::with_scope
  26: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_item
  27: rustc_ast::visit::walk_crate
  28: rustc_resolve::late::<impl rustc_resolve::Resolver>::late_resolve_crate
  29: rustc_session::utils::<impl rustc_session::session::Session>::time
  30: rustc_resolve::Resolver::resolve_crate
  31: rustc_interface::passes::configure_and_expand_inner
  32: rustc_interface::passes::configure_and_expand::{{closure}}
  33: alloc::boxed::<impl core::ops::generator::Generator<R> for core::pin::Pin<alloc::boxed::Box<G,A>>>::resume
  34: rustc_data_structures::box_region::PinnedGenerator<I,A,R>::new
  35: rustc_interface::passes::BoxedResolver::new
  36: rustc_interface::passes::configure_and_expand
  37: rustc_interface::queries::Query<T>::compute
  38: rustc_interface::queries::Queries::expansion
  39: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  40: rustc_span::with_source_map
  41: rustc_interface::interface::create_compiler_and_run
  42: scoped_tls::ScopedKey<T>::set
  43: rustc_span::with_session_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.51.0-dev running on x86_64-apple-darwin

note: compiler flags: -Z treat-err-as-bug

query stack during panic:
end of query stack

From there the place you might want to start looking at is smart_resolve_path_fragment, which is a bit hard to follow at the moment (I'm doing a slight clean up of that method at the moment), but should be reasonably straight forward to add another check for this case to that pile :)

@henryboisdequin
Copy link
Contributor

@rustbot claim

@Krout0n
Copy link
Contributor

Krout0n commented Mar 4, 2021

Can I take this issue?

@henryboisdequin
Copy link
Contributor

Can I take this issue?

Yes feel free to take it, I removed my assignment.

@Krout0n
Copy link
Contributor

Krout0n commented Mar 13, 2021

I’m going to be tied up until May, feel free to take this issue:pray:

@kwj2104
Copy link
Contributor

kwj2104 commented Mar 14, 2021

Hey I'm looking to get to know the Rust codebase better and contribute to the project - would love to take a crack at this issue!

@kwj2104
Copy link
Contributor

kwj2104 commented Mar 14, 2021

@rustbot claim

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 D-papercut Diagnostics: An error or lint that needs small tweaks. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants