Skip to content

Commit 8b12ac6

Browse files
Sdoba16LesterEvSe
authored andcommitted
fix: internal error inside resolution.rs and add error tests
1 parent da1240a commit 8b12ac6

2 files changed

Lines changed: 115 additions & 6 deletions

File tree

src/lib.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,99 @@ fn main() {
11021102
}
11031103
}
11041104

1105+
#[cfg(test)]
1106+
mod error_tests {
1107+
use std::path::Path;
1108+
1109+
use super::*;
1110+
1111+
use crate::resolution::tests::canon;
1112+
use crate::resolution::CanonPath;
1113+
use crate::test_utils::TempWorkspace;
1114+
1115+
fn dependency_map(root_dir: &Path, drp: &str, lib_dir: &Path) -> DependencyMap {
1116+
let mut dependency_map = DependencyMap::new();
1117+
1118+
let context = CanonPath::canonicalize(root_dir).unwrap();
1119+
let target = CanonPath::canonicalize(lib_dir).unwrap();
1120+
1121+
dependency_map.insert(context, drp.into(), target).unwrap();
1122+
1123+
dependency_map
1124+
}
1125+
1126+
fn source_file(path: &Path) -> SourceFile {
1127+
let content = std::fs::read_to_string(path).expect("Failed to read test file");
1128+
SourceFile::new(path, Arc::from(content))
1129+
}
1130+
1131+
#[test]
1132+
#[ignore = "TODO: Bug in Error Handler. Expected to be fixed in a future update to correctly point to dependency source files."]
1133+
fn dependency_ast_errors_use_dependency_source_file() {
1134+
let ws = TempWorkspace::new("dependency_ast_error_source");
1135+
let root_dir = ws.create_dir("workspace");
1136+
let lib_dir = ws.create_dir("workspace/lib");
1137+
let main_path = ws.create_file(
1138+
"workspace/main.simf",
1139+
"use lib::bad::f;\nfn main() { f(); }\n",
1140+
);
1141+
let bad_path = ws.create_file(
1142+
"workspace/lib/bad.simf",
1143+
"pub fn f() { let x: u32 = true; }\n",
1144+
);
1145+
1146+
let dependencies = dependency_map(&root_dir, "lib", &lib_dir);
1147+
1148+
let err = TemplateProgram::new_with_dep(source_file(&main_path), &dependencies)
1149+
.expect_err("dependency body has a type error");
1150+
let dependency_source = canon(&bad_path).as_path().display().to_string();
1151+
1152+
assert!(
1153+
err.contains(&dependency_source),
1154+
"expected diagnostic to point at dependency source {dependency_source}, got:\n{err}"
1155+
);
1156+
}
1157+
1158+
#[test]
1159+
fn omitted_context_dependency_applies_inside_dependency_files() {
1160+
let ws = TempWorkspace::new("omitted_context_dependency");
1161+
let lib_dir = ws.create_dir("workspace/lib");
1162+
let main_path = ws.create_file(
1163+
"workspace/main.simf",
1164+
"use lib::nested::two;\nfn main() { assert!(jet::eq_32(two(), 2)); }\n",
1165+
);
1166+
ws.create_file(
1167+
"workspace/lib/nested.simf",
1168+
"use lib::base::one;\npub fn two() -> u32 {\n let (_, out): (bool, u32) = jet::add_32(one(), 1);\n out\n}\n",
1169+
);
1170+
ws.create_file("workspace/lib/base.simf", "pub fn one() -> u32 { 1 }\n");
1171+
1172+
let dependencies = dependency_map(&main_path, "lib", &lib_dir);
1173+
let _err = TemplateProgram::new_with_dep(source_file(&main_path), &dependencies)
1174+
.expect_err("omitted-context dependencies");
1175+
}
1176+
1177+
#[test]
1178+
fn missing_mapped_module_is_reported_as_file_not_found() {
1179+
let ws = TempWorkspace::new("missing_mapped_module");
1180+
let root_dir = ws.create_dir("workspace");
1181+
let lib_dir = ws.create_dir("workspace/lib");
1182+
let main_path = ws.create_file(
1183+
"workspace/main.simf",
1184+
"use lib::missing::Thing;\nfn main() {}\n",
1185+
);
1186+
let dependencies = dependency_map(&root_dir, "lib", &lib_dir);
1187+
1188+
let err = TemplateProgram::new_with_dep(source_file(&main_path), &dependencies)
1189+
.expect_err("missing imported module should fail");
1190+
1191+
assert!(
1192+
err.contains("missing.simf"),
1193+
"diagnostic should mention the missing module path, got:\n{err}"
1194+
);
1195+
}
1196+
}
1197+
11051198
#[cfg(test)]
11061199
mod functional_tests {
11071200
use crate::tests::{run_dependency_test, run_multidep_test};

src/resolution.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,33 @@ impl DependencyMap {
192192

193193
// Check if the alias matches what the user typed
194194
if remapping.drp_name == drp_name {
195-
return remapping.target.join(&parts[1..]).map_err(|err| {
196-
RichError::new(
197-
Error::Internal(format!("Dependency resolution failed: {}", err)),
198-
*use_decl.span(),
199-
)
200-
});
195+
return Self::build_and_verify_path(&remapping.target, &parts[1..]).map_err(
196+
|failed_path| {
197+
RichError::new(Error::FileNotFound(failed_path), *use_decl.span())
198+
},
199+
);
201200
}
202201
}
203202

204203
Err(Error::UnknownLibrary(drp_name.to_string())).with_span(*use_decl.span())
205204
}
205+
206+
/// Replace `.join` method to better error handling
207+
fn build_and_verify_path(
208+
base_target: &CanonPath,
209+
module_parts: &[impl ToString],
210+
) -> Result<CanonPath, std::path::PathBuf> {
211+
let mut theoretical_path = base_target.as_path().to_path_buf();
212+
for part in module_parts {
213+
theoretical_path.push(part.to_string());
214+
}
215+
theoretical_path.set_extension("simf");
216+
217+
match CanonPath::canonicalize(&theoretical_path) {
218+
Ok(valid_canon_path) => Ok(valid_canon_path),
219+
Err(_) => Err(theoretical_path),
220+
}
221+
}
206222
}
207223

208224
#[cfg(test)]

0 commit comments

Comments
 (0)