Skip to content

Commit 224a255

Browse files
bors[bot]Veykril
andauthored
Merge #11680
11680: fix: Show what file paths were expected for unresolved modules r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 69e5bd5 + a9dd606 commit 224a255

File tree

12 files changed

+133
-59
lines changed

12 files changed

+133
-59
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ diagnostics![
5454
#[derive(Debug)]
5555
pub struct UnresolvedModule {
5656
pub decl: InFile<AstPtr<ast::Module>>,
57-
pub candidate: String,
57+
pub candidates: Box<[String]>,
5858
}
5959

6060
#[derive(Debug)]

crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,12 @@ impl Module {
593593

594594
fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag: &DefDiagnostic) {
595595
match &diag.kind {
596-
DefDiagnosticKind::UnresolvedModule { ast: declaration, candidate } => {
596+
DefDiagnosticKind::UnresolvedModule { ast: declaration, candidates } => {
597597
let decl = declaration.to_node(db.upcast());
598598
acc.push(
599599
UnresolvedModule {
600600
decl: InFile::new(declaration.file_id, AstPtr::new(&decl)),
601-
candidate: candidate.clone(),
601+
candidates: candidates.clone(),
602602
}
603603
.into(),
604604
)

crates/hir_def/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fst = { version = "0.4", default-features = false }
2424
itertools = "0.10.0"
2525
indexmap = "1.7.0"
2626
smallvec = "1.4.0"
27+
arrayvec = "0.7.2"
2728
la-arena = { version = "0.3.0", path = "../../lib/arena" }
2829

2930
stdx = { path = "../stdx", version = "0.0.0" }

crates/hir_def/src/item_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ pub enum ModKind {
709709
Inline { items: Box<[ModItem]> },
710710

711711
/// `mod m;`
712-
Outline {},
712+
Outline,
713713
}
714714

715715
#[derive(Debug, Clone, Eq, PartialEq)]

crates/hir_def/src/item_tree/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'a> Ctx<'a> {
401401
let name = module.name()?.as_name();
402402
let visibility = self.lower_visibility(module);
403403
let kind = if module.semicolon_token().is_some() {
404-
ModKind::Outline {}
404+
ModKind::Outline
405405
} else {
406406
ModKind::Inline {
407407
items: module

crates/hir_def/src/item_tree/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'a> Printer<'a> {
423423
});
424424
wln!(self, "}}");
425425
}
426-
ModKind::Outline {} => {
426+
ModKind::Outline => {
427427
wln!(self, ";");
428428
}
429429
}

crates/hir_def/src/nameres/collector.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ impl ModCollector<'_, '_> {
17131713
}
17141714
}
17151715
// out of line module, resolve, parse and recurse
1716-
ModKind::Outline {} => {
1716+
ModKind::Outline => {
17171717
let ast_id = AstId::new(self.tree_id.file_id(), module.ast_id);
17181718
let db = self.def_collector.db;
17191719
match self.mod_dir.resolve_declaration(db, self.file_id(), &module.name, path_attr)
@@ -1751,9 +1751,9 @@ impl ModCollector<'_, '_> {
17511751
}
17521752
}
17531753
}
1754-
Err(candidate) => {
1754+
Err(candidates) => {
17551755
self.def_collector.def_map.diagnostics.push(
1756-
DefDiagnostic::unresolved_module(self.module_id, ast_id, candidate),
1756+
DefDiagnostic::unresolved_module(self.module_id, ast_id, candidates),
17571757
);
17581758
}
17591759
};

crates/hir_def/src/nameres/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515

1616
#[derive(Debug, PartialEq, Eq)]
1717
pub enum DefDiagnosticKind {
18-
UnresolvedModule { ast: AstId<ast::Module>, candidate: String },
18+
UnresolvedModule { ast: AstId<ast::Module>, candidates: Box<[String]> },
1919

2020
UnresolvedExternCrate { ast: AstId<ast::ExternCrate> },
2121

@@ -46,11 +46,11 @@ impl DefDiagnostic {
4646
pub(super) fn unresolved_module(
4747
container: LocalModuleId,
4848
declaration: AstId<ast::Module>,
49-
candidate: String,
49+
candidates: Box<[String]>,
5050
) -> Self {
5151
Self {
5252
in_module: container,
53-
kind: DefDiagnosticKind::UnresolvedModule { ast: declaration, candidate },
53+
kind: DefDiagnosticKind::UnresolvedModule { ast: declaration, candidates },
5454
}
5555
}
5656

crates/hir_def/src/nameres/mod_resolution.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! This module resolves `mod foo;` declaration to file.
2+
use arrayvec::ArrayVec;
23
use base_db::{AnchoredPath, FileId};
34
use hir_expand::name::Name;
45
use limit::Limit;
@@ -63,22 +64,21 @@ impl ModDir {
6364
file_id: HirFileId,
6465
name: &Name,
6566
attr_path: Option<&SmolStr>,
66-
) -> Result<(FileId, bool, ModDir), String> {
67+
) -> Result<(FileId, bool, ModDir), Box<[String]>> {
6768
let orig_file_id = file_id.original_file(db.upcast());
6869

69-
let mut candidate_files = Vec::new();
70+
let mut candidate_files = ArrayVec::<_, 2>::new();
7071
match attr_path {
7172
Some(attr_path) => {
7273
candidate_files.push(self.dir_path.join_attr(attr_path, self.root_non_dir_owner))
7374
}
75+
None if file_id.is_include_macro(db.upcast()) => {
76+
candidate_files.push(format!("{}.rs", name));
77+
candidate_files.push(format!("{}/mod.rs", name));
78+
}
7479
None => {
75-
if file_id.is_include_macro(db.upcast()) {
76-
candidate_files.push(format!("{}.rs", name));
77-
candidate_files.push(format!("{}/mod.rs", name));
78-
} else {
79-
candidate_files.push(format!("{}{}.rs", self.dir_path.0, name));
80-
candidate_files.push(format!("{}{}/mod.rs", self.dir_path.0, name));
81-
}
80+
candidate_files.push(format!("{}{}.rs", self.dir_path.0, name));
81+
candidate_files.push(format!("{}{}/mod.rs", self.dir_path.0, name));
8282
}
8383
};
8484

@@ -97,7 +97,7 @@ impl ModDir {
9797
}
9898
}
9999
}
100-
Err(candidate_files.remove(0))
100+
Err(candidate_files.into_iter().collect())
101101
}
102102
}
103103

0 commit comments

Comments
 (0)