Disallow duplicate module functions
Closes
@@ -827,6 +827,11 @@ pub enum Error {
RecursiveType {
meta: Meta,
},
+
+ DuplicateFunction {
+ meta: Meta,
+ name: String,
+ },
}
/// Crawl the AST, annotating each node with the inferred type or
@@ -850,6 +855,11 @@ pub fn infer_module(module: UntypedModule) -> Result<TypedModule, Error> {
} => {
let level = 1;
+ // Ensure function has not already been defined in this module
+ if let Some((Scope::Module { .. }, _)) = env.get_variable(&name) {
+ return Err(Error::DuplicateFunction { meta, name });
+ };
+
// Register a var for the function so that it can call itself recursively
let rec = env.new_unbound_var(level + 1);
env.insert_variable(
@@ -3115,6 +3125,22 @@ fn infer_module_error_test() {
given: 2,
},
},
+ Case {
+ src: "fn dupe() { 1 }
+ fn dupe() { 2 }",
+ error: Error::DuplicateFunction {
+ meta: Meta { start: 34, end: 49 },
+ name: "dupe".to_string(),
+ },
+ },
+ Case {
+ src: "fn dupe() { 1 }
+ fn dupe(x) { x }",
+ error: Error::DuplicateFunction {
+ meta: Meta { start: 34, end: 50 },
+ name: "dupe".to_string(),
+ },
+ },
];
for Case { src, error } in cases.into_iter() {
