Let a file without main be checked as a module

Editor diagnostics and 'rex check' no longer report a missing main:
the file may be a module other files import. 'rex build' and 'rex run'
still need it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Your NameandClaude Opus 5.5 committed 2026-11-08 18:11:51 +01:00
1 parent 36eacc0011
commit 9b496a7b03
4 files changed
+55 -5

No files matched your search

+29 -4
View File
@@ -47,8 +47,18 @@ pub fn compile_to_c(src: &str) -> PResult<String> {
pub fn compile_project(
main_path: &str,
main_src: &str,
mut read: impl FnMut(&str) -> Option<(String, String)>,
read: impl FnMut(&str) -> Option<(String, String)>,
) -> Result<String, String> {
Ok(codegen_c::generate(&check_project(main_path, main_src, read, false)?))
}
/// Load and check a program; `module_ok`: the main file may lack `main` (it is a module).
fn check_project(
main_path: &str,
main_src: &str,
mut read: impl FnMut(&str) -> Option<(String, String)>,
module_ok: bool,
) -> Result<crate::hir::HProgram, String> {
let mut seen: HashMap<String, (String, String)> = HashMap::new();
let loaded = loader::load(main_src, &mut |key| {
let found = read(key);
@@ -69,8 +79,11 @@ pub fn compile_project(
loaded.modules.into_iter().map(|m| (m.key, m.program)).collect();
let dir = Path::new(main_path).parent().map(Path::to_path_buf).unwrap_or_default();
let libs = load_c_headers(&loaded.main, &modules, &dir).map_err(&render)?;
let hir = check::check_project_c(&loaded.main, &modules, libs).map_err(&render)?;
Ok(codegen_c::generate(&hir))
if module_ok {
check::check_module_c(&loaded.main, &modules, libs).map_err(&render)
} else {
check::check_project_c(&loaded.main, &modules, libs).map_err(&render)
}
}
/// Compile the program whose main file is `path`; imports are found next to it.
@@ -83,6 +96,18 @@ pub fn compile_file(path: &str) -> Result<String, String> {
})
}
/// Check the file at `path` (and what it imports) without building it. Unlike
/// `compile_file`, a file without `main` is fine: it may be a module.
pub fn check_file(path: &str) -> Result<(), String> {
let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?;
let dir = Path::new(path).parent().map(Path::to_path_buf).unwrap_or_default();
let read = |key: &str| {
let file = dir.join(format!("{}.rex", key.replace('.', "/")));
std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s))
};
check_project(path, &src, read, true).map(|_| ())
}
fn unique_name(tag: &str, ext: &str) -> PathBuf {
// the counter makes names differ even when two threads ask in the same nanosecond
static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
@@ -192,6 +217,6 @@ pub fn diagnose(
let modules: Vec<(String, crate::ast::Program)> =
loaded.modules.into_iter().map(|m| (m.key, m.program)).collect();
let libs = load_c_headers(&loaded.main, &modules, dir)?;
check::check_project_c(&loaded.main, &modules, libs)?;
check::check_module_c(&loaded.main, &modules, libs)?;
Ok(())
}