Skip to content

Commit d38a22a

Browse files
committed
Fix implementation to work for Rust 2015
Prioritizing the added check for the crate's name at the top of `report_path_resolution_error()` seems to account for differences in Rust 2015 and 2018's crate/module path systems, allowing the same suggestion to show up for Rust 2015.
1 parent 55bd201 commit d38a22a

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20032003
Some(ModuleOrUniformRoot::Module(module)) => module.res(),
20042004
_ => None,
20052005
};
2006-
if module_res == self.graph_root.res() {
2006+
if ident.name == self.tcx().crate_name(0_usize.into()) {
2007+
(
2008+
format!("use of unresolved module or unlinked crate `{ident}`"),
2009+
Some((
2010+
vec![(ident.span, String::from("crate"))],
2011+
format!(
2012+
"the current crate name can not be used in a path, use the keyword `crate` instead"
2013+
),
2014+
Applicability::MaybeIncorrect,
2015+
)),
2016+
)
2017+
} else if module_res == self.graph_root.res() {
20072018
let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
20082019
let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
20092020
candidates
@@ -2202,17 +2213,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22022213
});
22032214

22042215
(format!("use of undeclared type `{ident}`"), suggestion)
2205-
} else if ident.name == self.tcx().crate_name(0_usize.into()) {
2206-
(
2207-
format!("use of unresolved module or unlinked crate `{ident}`"),
2208-
Some((
2209-
vec![(ident.span, String::from("crate"))],
2210-
format!(
2211-
"the current crate name can not be used in a path, use the keyword `crate` instead"
2212-
),
2213-
Applicability::MaybeIncorrect,
2214-
)),
2215-
)
22162216
} else {
22172217
let mut suggestion = None;
22182218
if ident.name == sym::alloc {

tests/ui/resolve/current-crate-name-in-path.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
//! Ensure E0433 suggests replacing the crate's name with `crate` when the
22
//! current crate's name is used in a path.
33
4-
//@ edition:2018
5-
// Rust 2018 is needed for the updated crate/module path system and `crate`
6-
// keyword.
7-
84
mod bar {
95
pub fn baz() {}
106
}

tests/ui/resolve/current-crate-name-in-path.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `current_crate_name_in_path`
2-
--> $DIR/current-crate-name-in-path.rs:12:5
2+
--> $DIR/current-crate-name-in-path.rs:8:5
33
|
44
LL | use current_crate_name_in_path::bar::baz;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `current_crate_name_in_path`

0 commit comments

Comments
 (0)