Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2003,7 +2003,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Some(ModuleOrUniformRoot::Module(module)) => module.res(),
_ => None,
};
if module_res == self.graph_root.res() {
if ident.name == self.tcx().crate_name(0_usize.into()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem: I think this condition is too lax. Even if we constrain this suggestion to only be made if

  1. Unresolved module/crate is the first path segment of a non-1-length path
  2. Not a global path, e.g. ::foo

I think the possibility of false positives is still quite high, even if the applicability is MaybeIncorrect.

(
format!("use of unresolved module or unlinked crate `{ident}`"),
Some((
vec![(ident.span, String::from("crate"))],
format!(
"the current crate name can not be used in a path, use the keyword `crate` instead"
),
Applicability::MaybeIncorrect,
)),
)
Comment on lines +2006 to +2016
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem: I don't think this is right. Counter-example:

// foo.rs
mod bar {
    pub fn baz() {}
}

use crate::foo::baz;

fn main() {}
error[E0432]: unresolved import `crate::foo`
 --> foo.rs:5:12
  |
5 | use crate::foo::baz;
  |            ^^^ use of unresolved module or unlinked crate `foo`
  |
help: the current crate name can not be used in a path, use the keyword `crate` instead
  |
5 - use crate::foo::baz;
5 + use crate::crate::baz;
  |

error: aborting due to 1 previous error

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

} else if module_res == self.graph_root.res() {
let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
candidates
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/resolve/current-crate-name-in-path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Ensure E0433 suggests replacing the crate's name with `crate` when the
//! current crate's name is used in a path.

mod bar {
pub fn baz() {}
}

use current_crate_name_in_path::bar::baz;
//~^ ERROR failed to resolve: use of unresolved module or unlinked crate `current_crate_name_in_path`
//~| HELP the current crate name can not be used in a path, use the keyword `crate` instead
//~| SUGGESTION crate

fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/resolve/current-crate-name-in-path.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `current_crate_name_in_path`
--> $DIR/current-crate-name-in-path.rs:8:5
|
LL | use current_crate_name_in_path::bar::baz;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `current_crate_name_in_path`
|
help: the current crate name can not be used in a path, use the keyword `crate` instead
|
LL - use current_crate_name_in_path::bar::baz;
LL + use crate::bar::baz;
|

error: aborting due to 1 previous error

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