Improve function redefinition error message

This commit is contained in:
imaqtkatt 2024-06-07 17:25:23 -03:00
parent 8b31fad7a3
commit 0d2d2bb01e
8 changed files with 32 additions and 8 deletions

View File

@ -133,12 +133,12 @@ impl<'a> TermParser<'a> {
def.rules.push(rule);
} else {
// Trying to add a new rule to a previous definition, coming from a different rule.
let msg = format!("Redefinition of function '{name}'");
let msg = self.redefinition_of_function_msg(builtin, &name);
return self.with_ctx(Err(msg), ini_idx, end_idx);
}
} else {
// Trying to add a new rule to a previous definition, coming from another kind of top-level.
let msg = format!("Redefinition of function '{name}'");
let msg = self.redefinition_of_function_msg(builtin, &name);
return self.with_ctx(Err(msg), ini_idx, end_idx);
}
} else {
@ -1234,4 +1234,12 @@ pub trait ParserCommons<'a>: Parser<'a> {
self.consume_exactly("`")?;
Ok(result)
}
fn redefinition_of_function_msg(&self, builtin: bool, function_name: &str) -> String {
if builtin {
format!("Redefinition of builtin (function) '{function_name}'.")
} else {
format!("Redefinition of function '{function_name}'.")
}
}
}

View File

@ -1069,8 +1069,8 @@ impl<'a> PyParser<'a> {
end_idx: usize,
builtin: bool,
) -> ParseResult<()> {
if book.defs.contains_key(&def.name) {
let msg = format!("Redefinition of function '{}'.", def.name);
if let Some(def) = book.defs.get(&def.name) {
let msg = self.redefinition_of_function_msg(def.builtin, &def.name);
return self.with_ctx(Err(msg), ini_idx, end_idx);
}
if book.ctrs.contains_key(&def.name) {

View File

@ -0,0 +1,5 @@
def Map/get(m):
return m
def main:
return *(*(*(*(*(*)))))

View File

@ -0,0 +1,11 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/parse_file/redefinition_builtin.bend
---
Errors:
In tests/golden_tests/parse_file/redefinition_builtin.bend :
Redefinition of builtin (function) 'Map/get'.
 1 | def Map/get(m):
 2 |  return m
 3 | 
 4 | def main:

View File

@ -4,5 +4,5 @@ input_file: tests/golden_tests/parse_file/redefinition_imp_fun.bend
---
Errors:
In tests/golden_tests/parse_file/redefinition_imp_fun.bend :
Redefinition of function 'A'
Redefinition of function 'A'.
 5 | (A) = 1

View File

@ -4,5 +4,5 @@ input_file: tests/golden_tests/parse_file/redefinition_with_def_between.bend
---
Errors:
In tests/golden_tests/parse_file/redefinition_with_def_between.bend :
Redefinition of function 'A'
Redefinition of function 'A'.
 4 | (A) = @x x

View File

@ -4,5 +4,5 @@ input_file: tests/golden_tests/parse_file/redefinition_with_object_between.bend
---
Errors:
In tests/golden_tests/parse_file/redefinition_with_object_between.bend :
Redefinition of function 'A'
Redefinition of function 'A'.
 3 | A = 1

View File

@ -4,5 +4,5 @@ input_file: tests/golden_tests/parse_file/redefinition_with_type_between.bend
---
Errors:
In tests/golden_tests/parse_file/redefinition_with_type_between.bend :
Redefinition of function 'A'
Redefinition of function 'A'.
 3 | A = 1