diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-04-23 13:03:47 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-04-23 13:03:47 -0400 |
| commit | 7a175d3b45287b02c5981d09d8120553583f50d5 (patch) | |
| tree | eac3a09626b88439fbed59fb414debadfa948799 | |
| parent | 3c64616ac1f7a0ae6ac66281a2a11e1487f4747e (diff) | |
Catch namespace clobbering of types
| -rw-r--r-- | typecheck.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/typecheck.c b/typecheck.c index 38071c21..e0235308 100644 --- a/typecheck.c +++ b/typecheck.c @@ -120,8 +120,10 @@ void bind_statement(env_t *env, ast_t *statement) } case FunctionDef: { auto def = Match(statement, FunctionDef); - type_t *type = get_function_def_type(env, statement); const char *name = Match(def->name, Var)->name; + if (get_binding(env, name)) + code_err(def->name, "A %T called '%s' has already been defined", get_binding(env, name)->type, name); + type_t *type = get_function_def_type(env, statement); bool is_private = (name[0] == '_'); CORD code = is_private ? CORD_cat("$", name) : CORD_all(env->file_prefix, env->scope_prefix, name); set_binding(env, name, new(binding_t, .type=type, .code=code)); @@ -129,6 +131,8 @@ void bind_statement(env_t *env, ast_t *statement) } case StructDef: { auto def = Match(statement, StructDef); + if (get_binding(env, def->name)) + code_err(statement, "A %T called '%s' has already been defined", get_binding(env, def->name)->type, def->name); env_t *ns_env = namespace_env(env, def->name); @@ -156,6 +160,8 @@ void bind_statement(env_t *env, ast_t *statement) } case EnumDef: { auto def = Match(statement, EnumDef); + if (get_binding(env, def->name)) + code_err(statement, "A %T called '%s' has already been defined", get_binding(env, def->name)->type, def->name); env_t *ns_env = namespace_env(env, def->name); @@ -200,6 +206,8 @@ void bind_statement(env_t *env, ast_t *statement) } case LangDef: { auto def = Match(statement, LangDef); + if (get_binding(env, def->name)) + code_err(statement, "A %T called '%s' has already been defined", get_binding(env, def->name)->type, def->name); env_t *ns_env = namespace_env(env, def->name); type_t *type = Type(TextType, .lang=def->name, .env=ns_env); |
