From ef427f5e58e916521c4b869ae6726dc14dba9d25 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 22:29:00 +0300 Subject: [PATCH] Fix checking that parameters and return-type is known --- reid/src/mir/implement.rs | 2 +- reid/src/mir/typecheck.rs | 36 ++++++++++++++++++++++++++++++----- reid/src/mir/typeinference.rs | 2 +- reid_src/test.reid | 5 +++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 reid_src/test.reid diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index 5f91f5b..c2501db 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -542,7 +542,7 @@ pub fn pick_return(lhs: (ReturnKind, T), rhs: (ReturnKind, T)) -> (ReturnKind impl TypeKind { /// Assert that a type is already known and not vague. Return said type or /// error. - pub fn assert_known(&self) -> Result { + pub fn assert_unvague(&self) -> Result { self.known().map_err(ErrorKind::TypeIsVague) } diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 655a927..d082280 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -89,7 +89,7 @@ impl<'t> Pass for TypeCheck<'t> { name, kind, meta, - source_module, + source_module: _, } = &typedef; match kind { @@ -162,12 +162,12 @@ fn check_typedefs_for_recursion<'a, 'b>( impl FunctionDefinition { fn typecheck( &mut self, - hints: &TypeRefs, + typerefs: &TypeRefs, state: &mut TypecheckPassState, ) -> Result { for param in &self.parameters { let param_t = state.or_else( - param.1.assert_known(), + param.1.assert_known(typerefs, state), TypeKind::Vague(Vague::Unknown), self.signature(), ); @@ -185,11 +185,11 @@ impl FunctionDefinition { state.ok(res, self.signature()); } - let return_type = self.return_type.clone(); + let return_type = self.return_type.clone().assert_known(typerefs, state)?; let inferred = match &mut self.kind { FunctionDefinitionKind::Local(block, _) => { state.scope.return_type_hint = Some(self.return_type.clone()); - block.typecheck(&mut state.inner(), &hints, Some(&return_type)) + block.typecheck(&mut state.inner(), &typerefs, Some(&return_type)) } FunctionDefinitionKind::Extern(_) => { Ok((ReturnKind::Soft, TypeKind::Vague(Vague::Unknown))) @@ -755,3 +755,29 @@ impl Literal { } } } + +impl TypeKind { + fn assert_known( + &self, + refs: &TypeRefs, + state: &TypecheckPassState, + ) -> Result { + match &self { + TypeKind::Array(type_kind, _) => type_kind.as_ref().assert_known(refs, state), + TypeKind::CustomType(custom_type_key) => state + .scope + .types + .get(custom_type_key) + .map(|_| self.clone()) + .ok_or(ErrorKind::NoSuchType( + custom_type_key.0.clone(), + state.module_id.unwrap(), + )), + TypeKind::Borrow(type_kind, _) => type_kind.assert_known(refs, state), + TypeKind::UserPtr(type_kind) => type_kind.assert_known(refs, state), + TypeKind::CodegenPtr(type_kind) => type_kind.assert_known(refs, state), + TypeKind::Vague(vague_type) => Err(ErrorKind::TypeIsVague(*vague_type)), + _ => Ok(self.clone()), + } + } +} diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index 9054416..049e5c9 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -49,7 +49,7 @@ impl FunctionDefinition { ) -> Result<(), ErrorKind> { let scope_hints = ScopeTypeRefs::from(type_refs); for param in &self.parameters { - let param_t = state.or_else(param.1.assert_known(), Vague(Unknown), self.signature()); + let param_t = state.or_else(param.1.assert_unvague(), Vague(Unknown), self.signature()); let res = scope_hints .new_var(param.0.clone(), false, ¶m_t) .or(Err(ErrorKind::VariableAlreadyDefined(param.0.clone()))); diff --git a/reid_src/test.reid b/reid_src/test.reid new file mode 100644 index 0000000..0cb24aa --- /dev/null +++ b/reid_src/test.reid @@ -0,0 +1,5 @@ +extern fn SDL_malloc(size: u64) -> *SDL_Thing; + +fn main() { + let pixels = SDL_malloc(4 * 320 * 240); +} \ No newline at end of file