harec

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

commit a1633fb654183f6446c7812f1ea3ac1a566ca693
parent b8fb462654ba877de628e6fcf34321aa1e09135d
Author: Armin Weigl <tb46305@gmail.com>
Date:   Mon,  5 Apr 2021 19:20:35 +0000

*_expr_assign: dealias indirect assignment target

Diffstat:
Msrc/check.c | 11++++++-----
Msrc/gen.c | 2+-
Mtests/03-pointers.ha | 4+++-
3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -486,22 +486,23 @@ check_expr_assign(struct context *ctx, expr->assign.op = aexpr->assign.op; if (aexpr->assign.indirect) { - if (object->result->storage != STORAGE_POINTER) { + const struct type *otype = type_dealias(object->result); + if (otype->storage != STORAGE_POINTER) { return error(aexpr->loc, expr, errors, "Cannot dereference non-pointer type for assignment"); } - if (object->result->pointer.flags & PTR_NULLABLE) { + if (otype->pointer.flags & PTR_NULLABLE) { return error(aexpr->loc, expr, errors, "Cannot dereference nullable pointer type"); } errors = check_expression(ctx, aexpr->assign.value, value, - object->result->pointer.referent, errors); - if (!type_is_assignable(object->result->pointer.referent, + otype->pointer.referent, errors); + if (!type_is_assignable(otype->pointer.referent, value->result)) { return error(aexpr->loc, expr, errors, "Value type is not assignable to pointer type"); } - value = lower_implicit_cast(object->result->pointer.referent, value); + value = lower_implicit_cast(otype->pointer.referent, value); } else { errors = check_expression(ctx, aexpr->assign.value, value, object->result, errors); diff --git a/src/gen.c b/src/gen.c @@ -837,7 +837,7 @@ gen_expr_assign(struct gen_context *ctx, const struct expression *value = expr->assign.value; const struct type *objtype = expr->assign.indirect - ? object->result->pointer.referent : object->result; + ? type_dealias(object->result)->pointer.referent : object->result; const struct qbe_type *vtype = qtype_for_type(ctx, value->result, false); const struct qbe_type *otype = qtype_for_type(ctx, objtype, false); diff --git a/tests/03-pointers.ha b/tests/03-pointers.ha @@ -1,8 +1,10 @@ use rt; +type intp = *int; + fn basics() void = { let x = 42; - let y: *int = &x; + let y: intp = &x; assert(*y == 42); *y = 1337; assert(x == 1337);