Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#485 Improve the error message when redefining a builtin function #567

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/fun/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}'.")
}
}
}
4 changes: 2 additions & 2 deletions src/imp/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions tests/golden_tests/parse_file/redefinition_builtin.bend
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def Map/get(m):
return m

def main:
return *(*(*(*(*(*)))))
11 changes: 11 additions & 0 deletions tests/snapshots/parse_file__redefinition_builtin.bend.snap
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 1 addition & 1 deletion tests/snapshots/parse_file__redefinition_imp_fun.bend.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading