aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2024-04-27 10:34:17 +0100
committergingerBill <bill@gingerbill.org>2024-04-27 10:34:17 +0100
commit5e1b376e22854ee4dd1d455d9db372d24f445ae4 (patch)
tree367132e16c43d07b6d63dfd86767ac758899a383 /src
parent393e4a9db6d649387dd5c831134e75d98b780156 (diff)
Disallow `for x in bitset_or_map` if `x` is a variable that matches the "key"
Diffstat (limited to 'src')
-rw-r--r--src/check_stmt.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp
index c31056b33..cccbab4f6 100644
--- a/src/check_stmt.cpp
+++ b/src/check_stmt.cpp
@@ -1629,6 +1629,17 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags)
if (build_context.no_rtti && is_type_enum(t->BitSet.elem)) {
error(node, "Iteration over a bit_set of an enum is not allowed runtime type information (RTTI) has been disallowed");
}
+ if (rs->vals.count == 1 && rs->vals[0] && rs->vals[0]->kind == Ast_Ident) {
+ String name = rs->vals[0]->Ident.token.string;
+ Entity *found = scope_lookup(ctx->scope, name);
+ if (found && are_types_identical(found->type, t->BitSet.elem)) {
+ ERROR_BLOCK();
+ gbString s = expr_to_string(expr);
+ error(rs->vals[0], "'%.*s' shadows a previous declaration which might be ambiguous with 'for (%.*s in %s)'", LIT(name), LIT(name), s);
+ error_line("\tSuggestion: Use a different identifier if iteration is wanted, or surround in parentheses if a normal for loop is wanted\n");
+ gb_string_free(s);
+ }
+ }
break;
case Type_EnumeratedArray:
@@ -1664,6 +1675,17 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags)
if (is_reverse) {
error(node, "#reverse for is not supported for map types, as maps are unordered");
}
+ if (rs->vals.count == 1 && rs->vals[0] && rs->vals[0]->kind == Ast_Ident) {
+ String name = rs->vals[0]->Ident.token.string;
+ Entity *found = scope_lookup(ctx->scope, name);
+ if (found && are_types_identical(found->type, t->Map.key)) {
+ ERROR_BLOCK();
+ gbString s = expr_to_string(expr);
+ error(rs->vals[0], "'%.*s' shadows a previous declaration which might be ambiguous with 'for (%.*s in %s)'", LIT(name), LIT(name), s);
+ error_line("\tSuggestion: Use a different identifier if iteration is wanted, or surround in parentheses if a normal for loop is wanted\n");
+ gb_string_free(s);
+ }
+ }
break;
case Type_Tuple: