From cfd7b77644bcb456428746d5864597497fa248fa Mon Sep 17 00:00:00 2001 From: IsaacShelton Date: Thu, 26 Dec 2024 02:19:37 -0600 Subject: [PATCH] Minor code improvements --- src/ast/workspace/mod.rs | 4 ++++ src/resolve/function_body.rs | 4 +--- src/resolve/function_head.rs | 4 +--- src/resolve/global_variable.rs | 4 +--- src/resolve/helper_expr.rs | 5 +---- src/resolve/type_definition/prepare.rs | 4 +--- src/resolve/type_definition/resolve.rs | 4 +--- 7 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/ast/workspace/mod.rs b/src/ast/workspace/mod.rs index 23d62a6c..7a85aa37 100644 --- a/src/ast/workspace/mod.rs +++ b/src/ast/workspace/mod.rs @@ -70,6 +70,10 @@ impl<'a> AstWorkspace<'a> { None } + pub fn get_owning_module_or_self(&self, fs_node_id: FsNodeId) -> FsNodeId { + self.get_owning_module(fs_node_id).unwrap_or(fs_node_id) + } + pub fn get_mut(&mut self, id: FsNodeId) -> Option<&mut AstFile> { self.files.get_mut(&id) } diff --git a/src/resolve/function_body.rs b/src/resolve/function_body.rs index 0267eadf..40d74c3f 100644 --- a/src/resolve/function_body.rs +++ b/src/resolve/function_body.rs @@ -16,9 +16,7 @@ pub fn resolve_function_bodies( while let Some(job) = ctx.jobs.pop_front() { match job { FuncJob::Regular(physical_file_id, ast_function_index, resolved_function_ref) => { - let module_file_id = ast_workspace - .get_owning_module(physical_file_id) - .unwrap_or(physical_file_id); + let module_file_id = ast_workspace.get_owning_module_or_self(physical_file_id); let function_haystack = ctx .function_haystacks diff --git a/src/resolve/function_head.rs b/src/resolve/function_head.rs index 84e1ee03..d32d9033 100644 --- a/src/resolve/function_head.rs +++ b/src/resolve/function_head.rs @@ -21,9 +21,7 @@ pub fn create_function_heads<'a>( options: &BuildOptions, ) -> Result<(), ResolveError> { for (physical_file_id, file) in ast_workspace.files.iter() { - let module_file_id = ast_workspace - .get_owning_module(*physical_file_id) - .unwrap_or(*physical_file_id); + let module_file_id = ast_workspace.get_owning_module_or_self(*physical_file_id); for (function_i, function) in file.functions.iter().enumerate() { let name = ResolvedName::new(module_file_id, &Name::plain(&function.head.name)); diff --git a/src/resolve/global_variable.rs b/src/resolve/global_variable.rs index 8045dc71..64258826 100644 --- a/src/resolve/global_variable.rs +++ b/src/resolve/global_variable.rs @@ -13,9 +13,7 @@ pub fn resolve_global_variables( let constraints = CurrentConstraints::new_empty(ctx.implementations); for (physical_file_id, file) in ast_workspace.files.iter() { - let module_file_id = ast_workspace - .get_owning_module(*physical_file_id) - .unwrap_or(*physical_file_id); + let module_file_id = ast_workspace.get_owning_module_or_self(*physical_file_id); for global in file.global_variables.iter() { let type_ctx = ResolveTypeCtx::new( diff --git a/src/resolve/helper_expr.rs b/src/resolve/helper_expr.rs index 42405830..f551c83e 100644 --- a/src/resolve/helper_expr.rs +++ b/src/resolve/helper_expr.rs @@ -16,10 +16,7 @@ pub fn resolve_helper_expressions( ast_workspace: &AstWorkspace, ) -> Result<(), ResolveError> { for (physical_file_id, file) in ast_workspace.files.iter() { - let module_file_id = ast_workspace - .get_owning_module(*physical_file_id) - .unwrap_or(*physical_file_id); - + let module_file_id = ast_workspace.get_owning_module_or_self(*physical_file_id); let settings = &ast_workspace.settings[file.settings.unwrap_or_default().0]; // NOTE: This module should already have a function haystack diff --git a/src/resolve/type_definition/prepare.rs b/src/resolve/type_definition/prepare.rs index d3da15fe..b5e0b2cc 100644 --- a/src/resolve/type_definition/prepare.rs +++ b/src/resolve/type_definition/prepare.rs @@ -25,9 +25,7 @@ pub fn prepare_type_jobs( let mut type_jobs = Vec::with_capacity(ast_workspace.files.len()); for (physical_file_id, file) in ast_workspace.files.iter() { - let module_fs_node_id = ast_workspace - .get_owning_module(*physical_file_id) - .unwrap_or(*physical_file_id); + let module_fs_node_id = ast_workspace.get_owning_module_or_self(*physical_file_id); let mut job = TypeJob { physical_file_id: *physical_file_id, diff --git a/src/resolve/type_definition/resolve.rs b/src/resolve/type_definition/resolve.rs index 3e822c2a..47df97b5 100644 --- a/src/resolve/type_definition/resolve.rs +++ b/src/resolve/type_definition/resolve.rs @@ -29,9 +29,7 @@ pub fn resolve_type_jobs( .get(&job.physical_file_id) .expect("valid ast file"); - let module_file_id = ast_workspace - .get_owning_module(job.physical_file_id) - .unwrap_or(job.physical_file_id); + let module_file_id = ast_workspace.get_owning_module_or_self(job.physical_file_id); for (trait_ref, user_trait) in job.traits.iter().zip(file.traits.iter()) { resolve_trait(