Disallow mutation of read-only views
This commit is contained in:
parent
052316261a
commit
63a5032ca0
17
compile.c
17
compile.c
@ -278,9 +278,20 @@ CORD compile_type(type_t *t)
|
||||
static CORD compile_lvalue(env_t *env, ast_t *ast)
|
||||
{
|
||||
if (!can_be_mutated(env, ast)) {
|
||||
if (ast->tag == Index || ast->tag == FieldAccess) {
|
||||
ast_t *subject = ast->tag == Index ? Match(ast, Index)->indexed : Match(ast, FieldAccess)->fielded;
|
||||
code_err(subject, "This is an immutable value, you can't assign to it");
|
||||
if (ast->tag == Index) {
|
||||
ast_t *subject = Match(ast, Index)->indexed;
|
||||
type_t *t = get_type(env, subject);
|
||||
if (t->tag == PointerType && Match(t, PointerType)->is_view)
|
||||
code_err(ast, "This is a read-only view and you can't mutate its contents");
|
||||
else
|
||||
code_err(subject, "This is an immutable value, you can't mutate its contents");
|
||||
} else if (ast->tag == FieldAccess) {
|
||||
ast_t *subject = Match(ast, FieldAccess)->fielded;
|
||||
type_t *t = get_type(env, subject);
|
||||
if (t->tag == PointerType && Match(t, PointerType)->is_view)
|
||||
code_err(subject, "This is a read-only view and you can't mutate its fields");
|
||||
else
|
||||
code_err(subject, "This is an immutable value, you can't assign to its fields");
|
||||
} else {
|
||||
code_err(ast, "This is a value of type %T and can't be used as an assignment target", get_type(env, ast));
|
||||
}
|
||||
|
@ -1293,7 +1293,7 @@ PUREFUNC bool can_be_mutated(env_t *env, ast_t *ast)
|
||||
auto index = Match(ast, Index);
|
||||
type_t *indexed_type = get_type(env, index->indexed);
|
||||
if (indexed_type->tag == PointerType)
|
||||
return true;
|
||||
return !Match(indexed_type, PointerType)->is_view;
|
||||
return can_be_mutated(env, index->indexed);
|
||||
}
|
||||
default: return false;
|
||||
|
Loading…
Reference in New Issue
Block a user