Skip to content

Commit 3e922de

Browse files
authored
Merge pull request #525 from harehare/fix/module-validation-and-error-handling
🐛 fix(hir): improve module validation and error handling
2 parents c3d62fd + 214df39 commit 3e922de

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

crates/mq-hir/src/error.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ pub enum HirError {
1616
symbol: Symbol,
1717
similar_name: Option<Vec<CompactString>>,
1818
},
19+
#[error("Included module not found: {module_name}")]
20+
ModuleNotFound {
21+
symbol: Symbol,
22+
module_name: CompactString,
23+
},
1924
}
2025

2126
impl Hir {
@@ -24,14 +29,28 @@ impl Hir {
2429
.iter()
2530
.filter_map(|(symbol_id, symbol)| match symbol.kind {
2631
SymbolKind::Call | SymbolKind::Ref => {
27-
if !self.references.contains_key(&symbol_id) {
32+
if self.references.contains_key(&symbol_id) {
33+
None
34+
} else {
2835
Some(HirError::UnresolvedSymbol {
2936
symbol: symbol.clone(),
3037
similar_name: self
3138
.find_similar_names(&symbol.clone().value.unwrap_or_default()),
3239
})
33-
} else {
34-
None
40+
}
41+
}
42+
SymbolKind::Include(_) => {
43+
let module_name = symbol
44+
.clone()
45+
.value
46+
.unwrap_or(CompactString::new("unknown"))
47+
.clone();
48+
match self.module_loader.read_file(&module_name) {
49+
Ok(_) => None,
50+
Err(_) => Some(HirError::ModuleNotFound {
51+
symbol: symbol.clone(),
52+
module_name,
53+
}),
3554
}
3655
}
3756
_ => None,
@@ -49,6 +68,9 @@ impl Hir {
4968
HirError::UnresolvedSymbol { symbol, .. } => {
5069
symbol.source.text_range.clone().unwrap_or_default()
5170
}
71+
HirError::ModuleNotFound { symbol, .. } => {
72+
symbol.source.text_range.clone().unwrap_or_default()
73+
}
5274
},
5375
)
5476
})
@@ -118,6 +140,9 @@ mod tests {
118140
assert_eq!(symbol.value.as_deref(), Some("unknown_var"));
119141
assert!(similar_name.is_none());
120142
}
143+
_ => {
144+
panic!("Expected UnresolvedSymbol error");
145+
}
121146
}
122147
}
123148

0 commit comments

Comments
 (0)