Skip to content

Commit 5e0c758

Browse files
Improved type checker error messages for clarity
1 parent 2bd1685 commit 5e0c758

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

src/interpreter/expression_eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ pub fn eval_function_call(
450450
//This will never happen, but I need to cover all cases, otherwise it won't compile
451451
_ => {
452452
return Err(format!(
453-
"[Runtime Error] Function {} expected another function as argument, but received a non functional argument",
454-
func_name
453+
"[Runtime Error] Function {:?} expected another function as argument, but received a non functional argument",
454+
func_signature
455455
));
456456
}
457457
}

src/type_checker/expression_type_checker.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ pub fn check_func_call(
9090
let func = env.lookup_function(&func_signature);
9191
if func.is_none() {
9292
return Err(format!(
93-
"Function {} was called but never declared",
94-
func_name
93+
"Function {:?} was called but never declared",
94+
func_signature
9595
));
9696
}
9797
let func = func.unwrap();
@@ -105,10 +105,10 @@ pub fn check_func_call(
105105
for (formal_type, actual_type) in formal_arg_types.iter().zip(actual_arg_types.iter()) {
106106
if formal_type != actual_type {
107107
return Err(format!(
108-
"Mismatched types in function {} call \n
108+
"Mismatched types in function {:?} call \n
109109
Expected:{:?}\n
110110
Received: {:?}",
111-
func_name, formal_arg_types, actual_arg_types
111+
func_signature, formal_arg_types, actual_arg_types
112112
));
113113
}
114114
}

src/type_checker/statement_type_checker.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,21 @@ fn check_func_def_stmt(
210210
env: &Environment<Type>,
211211
) -> Result<Environment<Type>, ErrorMessage> {
212212
let mut new_env = Environment::new();
213+
let func_signature = FuncSignature::from_func(&function);
213214
//new_env.push(); -> Push and pop will happen in check_block_statement
214-
new_env.set_current_func(&FuncSignature::from_func(&function));
215+
new_env.set_current_func(&func_signature);
215216
// Previous environment functions and the formal parameters are regarded as global
216217
new_env.set_global_functions(env.get_all_functions());
217218

218219
// Ensure that each function is defined only once in current scope
219220
let current_scope = env.get_current_scope();
220221
if current_scope
221222
.functions
222-
.contains_key(&FuncSignature::from_func(&function))
223+
.contains_key(&func_signature)
223224
{
224225
return Err(format!(
225-
"Function {} is defined multiple times",
226-
function.name
226+
"Function {:?} is defined multiple times",
227+
func_signature
227228
));
228229
}
229230

@@ -232,8 +233,8 @@ fn check_func_def_stmt(
232233
for arg in &function.params {
233234
if !seen_names.insert(arg.argument_name.clone()) {
234235
return Err(format!(
235-
"Duplicate parameter name '{}' found in function '{}'",
236-
arg.argument_name, function.name
236+
"Duplicate parameter name '{}' found in function '{:?}'",
237+
arg.argument_name, func_signature
237238
));
238239
}
239240
}

0 commit comments

Comments
 (0)