[core] process variables before function instances

This commit is contained in:
abdul124 2025-01-09 17:25:15 +08:00
parent 5076eccb6c
commit 16a3ac72e6

View File

@ -434,7 +434,7 @@ impl TopLevelComposer {
location: Location, location: Location,
) -> Result<(StrRef, DefinitionId, Option<Type>), String> { ) -> Result<(StrRef, DefinitionId, Option<Type>), String> {
if self.keyword_list.contains(&name) { if self.keyword_list.contains(&name) {
return Err(format!("cannot use keyword `{name}` as a class name (at {location})")); return Err(format!("cannot use keyword `{name}` as a variable name (at {location})"));
} }
let global_var_name = let global_var_name =
@ -469,10 +469,10 @@ impl TopLevelComposer {
self.analyze_top_level_class_definition()?; self.analyze_top_level_class_definition()?;
self.analyze_top_level_class_fields_methods()?; self.analyze_top_level_class_fields_methods()?;
self.analyze_top_level_function()?; self.analyze_top_level_function()?;
self.analyze_top_level_variables()?;
if inference { if inference {
self.analyze_function_instance()?; self.analyze_function_instance()?;
} }
self.analyze_top_level_variables()?;
Ok(()) Ok(())
} }
@ -1410,7 +1410,7 @@ impl TopLevelComposer {
Ok(()) Ok(())
} }
/// step 4, analyze and call type inferencer to fill the `instance_to_stmt` of /// step 5, analyze and call type inferencer to fill the `instance_to_stmt` of
/// [`TopLevelDef::Function`] /// [`TopLevelDef::Function`]
fn analyze_function_instance(&mut self) -> Result<(), HashSet<String>> { fn analyze_function_instance(&mut self) -> Result<(), HashSet<String>> {
// first get the class constructor type correct for the following type check in function body // first get the class constructor type correct for the following type check in function body
@ -1941,7 +1941,7 @@ impl TopLevelComposer {
Ok(()) Ok(())
} }
/// Step 5. Analyze and populate the types of global variables. /// Step 4. Analyze and populate the types of global variables.
fn analyze_top_level_variables(&mut self) -> Result<(), HashSet<String>> { fn analyze_top_level_variables(&mut self) -> Result<(), HashSet<String>> {
let def_list = &self.definition_ast_list; let def_list = &self.definition_ast_list;
let temp_def_list = self.extract_def_list(); let temp_def_list = self.extract_def_list();