diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-05-24 00:09:39 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-05-24 00:09:39 -0400 |
| commit | 670c90cef45f96cfbea4406c74a4eed0074f539e (patch) | |
| tree | 48810aa999425126f2ebd899697856671536be51 | |
| parent | 308946e794f05da9f7010797f5911bcf4e131c3e (diff) | |
Bugfix for doctest assignments not doing promotion checks
| -rw-r--r-- | compile.c | 13 |
1 files changed, 9 insertions, 4 deletions
@@ -285,14 +285,19 @@ CORD compile_statement(env_t *env, ast_t *ast) auto assign = Match(test->expr, Assign); if (!assign->targets->next && assign->targets->ast->tag == Var) { // Common case: assigning to one variable: - type_t *t = get_type(env, assign->targets->ast); - if (t->tag == PointerType && Match(t, PointerType)->is_stack) + type_t *lhs_t = get_type(env, assign->targets->ast); + if (lhs_t->tag == PointerType && Match(lhs_t, PointerType)->is_stack) code_err(test->expr, "Stack references cannot be assigned to local variables because the variable may outlive the stack memory."); + env_t *val_scope = with_enum_scope(env, lhs_t); + type_t *rhs_t = get_type(val_scope, assign->values->ast); + CORD value = compile(val_scope, assign->values->ast); + if (!promote(env, &value, rhs_t, lhs_t)) + code_err(assign->values->ast, "You cannot assign a %T value to a %T operand", rhs_t, lhs_t); return CORD_asprintf( "test(({ %r; &%r; }), %r, %r, %r, %ld, %ld);", - compile_assignment(env, assign->targets->ast, compile(with_enum_scope(env, t), assign->values->ast)), + compile_assignment(env, assign->targets->ast, value), compile(env, assign->targets->ast), - compile_type_info(env, t), + compile_type_info(env, lhs_t), compile(env, WrapAST(test->expr, TextLiteral, .cord=test->output)), compile(env, WrapAST(test->expr, TextLiteral, .cord=test->expr->file->filename)), (int64_t)(test->expr->start - test->expr->file->text), |
