diff --git a/src/typecheck/program.mbt b/src/typecheck/program.mbt index 398c064..36e4418 100644 --- a/src/typecheck/program.mbt +++ b/src/typecheck/program.mbt @@ -82,6 +82,7 @@ pub fn Context::exit_scope(self : Context) -> Unit { } ///| +#deprecated pub fn Context::set_current_func_ret_ty(self : Context, ty : TypeKind) -> Unit { self.current_func_ret_ty = Some(ty) } @@ -115,7 +116,9 @@ pub fn Context::check_program( } // 收集顶层函数声明 for name, func in program.top_functions { - self.func_types.set(name, self.check_top_function_type_decl(func)) + let func_type = self.check_top_function_type_decl(func) + self.func_types.set(name, func_type) + self.type_env.set(name, { kind: func_type, mutable: false }) } // 补充内建函数类型 self.add_intrinsic_functions() diff --git a/src/typecheck/stmt_local_function.mbt b/src/typecheck/stmt_local_function.mbt index 6906601..cb82496 100644 --- a/src/typecheck/stmt_local_function.mbt +++ b/src/typecheck/stmt_local_function.mbt @@ -26,8 +26,9 @@ pub fn Context::check_local_function( let func_type = Function(param_list.map(p => p.1.kind), ret_type) self.type_env.set(func.id, { kind: func_type, mutable: false }) // - self.enter_scope() + let parent_func_ret_ty = self.current_func_ret_ty self.current_func_ret_ty = Some(ret_type) + self.enter_scope() // for param in param_list { let (param_name, param_type) = param @@ -42,6 +43,7 @@ pub fn Context::check_local_function( } // self.exit_scope() + self.current_func_ret_ty = parent_func_ret_ty // { fname: func.id, diff --git a/src/typecheck/top_function.mbt b/src/typecheck/top_function.mbt index 0a65b27..f2d86b0 100644 --- a/src/typecheck/top_function.mbt +++ b/src/typecheck/top_function.mbt @@ -31,10 +31,6 @@ pub fn Context::check_top_function_body( guard self.func_types.get(func.id) is Some(Function(param_types, ret_ty)) else { raise TypeCheckError("Function type for '\{func.id}' not found.") } - self.type_env.set(func.id, { - kind: Function(param_types, ret_ty), - mutable: false, - }) self.enter_scope() self.current_func_ret_ty = Some(ret_ty) for i in 0..