diff --git a/src/fun/parser.rs b/src/fun/parser.rs index 8624d643..0ba98453 100644 --- a/src/fun/parser.rs +++ b/src/fun/parser.rs @@ -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}'.") + } + } } diff --git a/src/imp/parser.rs b/src/imp/parser.rs index 45ee1a7a..7fb9e3a3 100644 --- a/src/imp/parser.rs +++ b/src/imp/parser.rs @@ -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) { diff --git a/tests/golden_tests/parse_file/redefinition_builtin.bend b/tests/golden_tests/parse_file/redefinition_builtin.bend new file mode 100644 index 00000000..5dae112f --- /dev/null +++ b/tests/golden_tests/parse_file/redefinition_builtin.bend @@ -0,0 +1,5 @@ +def Map/get(m): + return m + +def main: + return *(*(*(*(*(*))))) diff --git a/tests/snapshots/parse_file__redefinition_builtin.bend.snap b/tests/snapshots/parse_file__redefinition_builtin.bend.snap new file mode 100644 index 00000000..2ea6f62f --- /dev/null +++ b/tests/snapshots/parse_file__redefinition_builtin.bend.snap @@ -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: diff --git a/tests/snapshots/parse_file__redefinition_imp_fun.bend.snap b/tests/snapshots/parse_file__redefinition_imp_fun.bend.snap index e14ae79d..39d574ed 100644 --- a/tests/snapshots/parse_file__redefinition_imp_fun.bend.snap +++ b/tests/snapshots/parse_file__redefinition_imp_fun.bend.snap @@ -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 diff --git a/tests/snapshots/parse_file__redefinition_with_def_between.bend.snap b/tests/snapshots/parse_file__redefinition_with_def_between.bend.snap index aca21a52..25c12461 100644 --- a/tests/snapshots/parse_file__redefinition_with_def_between.bend.snap +++ b/tests/snapshots/parse_file__redefinition_with_def_between.bend.snap @@ -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 diff --git a/tests/snapshots/parse_file__redefinition_with_object_between.bend.snap b/tests/snapshots/parse_file__redefinition_with_object_between.bend.snap index a95e8146..707c7f13 100644 --- a/tests/snapshots/parse_file__redefinition_with_object_between.bend.snap +++ b/tests/snapshots/parse_file__redefinition_with_object_between.bend.snap @@ -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 diff --git a/tests/snapshots/parse_file__redefinition_with_type_between.bend.snap b/tests/snapshots/parse_file__redefinition_with_type_between.bend.snap index 264ad129..ccdc4328 100644 --- a/tests/snapshots/parse_file__redefinition_with_type_between.bend.snap +++ b/tests/snapshots/parse_file__redefinition_with_type_between.bend.snap @@ -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