commit 3d54dc112121fd2e9fd09b28e373ddf853567168
parent 49d0f3427752f6d6f97118cdbe113bf429cadce4
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 1 Jan 2021 12:49:38 -0500
Add assertions and constraints around assignment
Diffstat:
3 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/src/check.c b/src/check.c
@@ -156,11 +156,10 @@ check_expr_assign(struct context *ctx,
"Value type is not assignable to pointer type");
} else {
assert(object->type == EXPR_ACCESS); // Invariant
- const struct scope_object *obj = object->access.object;
+ expect(&aexpr->loc, !(object->result->flags & TYPE_CONST),
+ "Cannot assign to const object");
expect(&aexpr->loc,
- !(obj->type->flags & TYPE_CONST), "Cannot assign to const object");
- expect(&aexpr->loc,
- type_is_assignable(&ctx->store, obj->type, value->result),
+ type_is_assignable(&ctx->store, object->result, value->result),
"rvalue type is not assignable to lvalue");
}
}
diff --git a/src/gen.c b/src/gen.c
@@ -395,6 +395,7 @@ gen_expr_assign(struct gen_context *ctx,
const struct expression *object = expr->assign.object;
assert(object->type == EXPR_ACCESS || expr->assign.indirect); // Invariant
+ assert(object->access.type == ACCESS_IDENTIFIER);
const struct scope_object *obj = object->access.object;
const struct expression *value = expr->assign.value;
const struct type *objtype = expr->assign.indirect
diff --git a/tests/01-arrays.ha b/tests/01-arrays.ha
@@ -26,15 +26,6 @@ fn alignment() void = {
assert(&y: uintptr: size % 8z == 0z);
};
-fn nested() void = {
- let x = [[1, 2], [3, 4]];
- assert(x[0][0] == 1);
- assert(x[0][1] == 2);
- assert(x[1][0] == 3);
- assert(x[1][1] == 4);
- assert(len(x[0]) == 2z);
-};
-
fn assignment() void = {
let x = [1, 2, 3];
let y = x;
@@ -55,12 +46,24 @@ fn param(x: [3]int) void = {
assert(x[2] == 3);
};
+fn nested() void = {
+ let x = [[1, 2], [3, 4]];
+ assert(x[0][0] == 1);
+ assert(x[0][1] == 2);
+ assert(x[1][0] == 3);
+ assert(x[1][1] == 4);
+ assert(len(x[0]) == 2z);
+ // TODO: Assignment to nested array
+ // x[1] = [5, 6];
+};
+
export fn main() void = {
+ // TODO: Assign to index, take address of index
indexing();
measurements();
storage();
alignment();
- nested();
assignment();
param([1, 2, 3]);
+ nested();
};