harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit fe176abcb22ea8d2342ea7e96e344aacd7443a83
parent 8aa60216ee43b9e3ca5d55700a70832aa19694d6
Author: Sebastian <sebastian@sebsite.pw>
Date:   Thu, 24 Mar 2022 21:58:54 -0400

check: evaluate initializer before storing shadowed type

This fixes a bug in which shadowing a variable with a different type
resulted in a compile error if the initializer referenced the shadowed
variable.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Msrc/check.c | 11+++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -970,9 +970,12 @@ check_expr_binding(struct context *ctx, bool context = abinding->type && abinding->type->storage == STORAGE_ARRAY && abinding->type->array.contextual; - if (type && !context) { - // If the type is defined in advance, we can insert the - // object into the scope early, which is required for + const struct scope_object *shadowed = + scope_lookup(ctx->scope, &ident); + if (type && !context && shadowed == NULL) { + // If the type is defined in advance, and a variable + // isn't being shadowed, we can insert the object into + // the scope early, which is required for // self-referencing objects. if (!abinding->is_static) { binding->object = scope_insert(ctx->scope, @@ -1000,7 +1003,7 @@ check_expr_binding(struct context *ctx, type = initializer->result; } - if (context || !type) { + if (context || !type || shadowed != NULL) { if (!type) { type = type_store_lookup_with_flags(ctx->store, initializer->result, abinding->flags);