diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-10-09 00:52:27 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-10-09 00:52:27 +0200 |
| commit | 994e6c95929ad9551901d86617e4678920f8f357 (patch) | |
| tree | d12a438aa537ea8fb680161ec945e4a7a14af1de | |
| parent | ea902858825aac806cd282e6d5f02ec683a995e3 (diff) | |
| download | elna-994e6c95929ad9551901d86617e4678920f8f357.tar.gz | |
Strip away constness from literals and rvalues
| -rw-r--r-- | boot/name_analysis.cc | 21 | ||||
| -rw-r--r-- | include/elna/boot/name_analysis.h | 1 | ||||
| -rw-r--r-- | testsuite/fail_compilation/record_constructor_const.elna | 10 | ||||
| -rw-r--r-- | testsuite/runnable/inferred_variable.elna | 6 |
4 files changed, 30 insertions, 8 deletions
diff --git a/boot/name_analysis.cc b/boot/name_analysis.cc index c82f211..c58b586 100644 --- a/boot/name_analysis.cc +++ b/boot/name_analysis.cc @@ -155,6 +155,8 @@ namespace elna::boot return "Duplicate 'const' qualifier is not allowed"; case constant_procedure: return "Procedure types are always constant, 'const' is not allowed"; + case constant_record_constructor: + return "Record constructor cannot have 'const' qualifier"; case module_entry: return "Program entry point should have no or one argument"; case enumeration_values: @@ -918,7 +920,12 @@ namespace elna::boot void resolving_visitor::visit(record_constructor_expression *expression) { - expression->type_decoration = resolve_type(expression->record_type()); + if (expression->record_type().is_constant() != nullptr) + { + add_error<declaration_format_error>(expression->record_type().position(), + declaration_format_error::kind::constant_record_constructor); + } + expression->type_decoration = unqualify(resolve_type(expression->record_type())); for (const field_initializer& initializer : expression->field_initializers) { @@ -936,7 +943,7 @@ namespace elna::boot void resolving_visitor::visit(array_constructor_expression *expression) { visit_all(this, expression->elements); - const type element_type = expression->elements.front()->type_decoration; + const type element_type = unqualify(expression->elements.front()->type_decoration); expression->type_decoration = type(std::make_shared<array_type>(element_type, expression->elements.size())); @@ -1012,11 +1019,11 @@ namespace elna::boot } else { - expression->type_decoration = expression->lhs().type_decoration; + expression->type_decoration = unqualify(expression->lhs().type_decoration); } break; default: - expression->type_decoration = expression->lhs().type_decoration; + expression->type_decoration = unqualify(expression->lhs().type_decoration); break; } this->current_type = type(); @@ -1033,7 +1040,7 @@ namespace elna::boot } else { - expression->type_decoration = expression->operand().type_decoration; + expression->type_decoration = unqualify(expression->operand().type_decoration); } this->current_type = type(); } @@ -1455,7 +1462,7 @@ namespace elna::boot declaration->initializer->accept(this); if (declaration->variable_type == nullptr) { - variable_type = declaration->initializer->type_decoration; + variable_type = unqualify(declaration->initializer->type_decoration); if (declaration->is_const) { variable_type = type(std::make_shared<constant_type>(variable_type)); @@ -1594,7 +1601,7 @@ namespace elna::boot declaration->initializer->accept(this); if (declaration->variable_type == nullptr) { - variable_type = declaration->initializer->type_decoration; + variable_type = unqualify(declaration->initializer->type_decoration); if (declaration->is_const) { variable_type = type(std::make_shared<constant_type>(variable_type)); diff --git a/include/elna/boot/name_analysis.h b/include/elna/boot/name_analysis.h index 5cf5d43..16734cd 100644 --- a/include/elna/boot/name_analysis.h +++ b/include/elna/boot/name_analysis.h @@ -131,6 +131,7 @@ namespace elna::boot array_position, duplicate, constant_procedure, + constant_record_constructor, module_entry, enumeration_values }; diff --git a/testsuite/fail_compilation/record_constructor_const.elna b/testsuite/fail_compilation/record_constructor_const.elna new file mode 100644 index 0000000..946105b --- /dev/null +++ b/testsuite/fail_compilation/record_constructor_const.elna @@ -0,0 +1,10 @@ +module +type + R = record + x: Int + end + +var + r := { const R; x: 1 } (* @Error Record constructor cannot have 'const' qualifier *) + +end. diff --git a/testsuite/runnable/inferred_variable.elna b/testsuite/runnable/inferred_variable.elna index e0822b8..48d7414 100644 --- a/testsuite/runnable/inferred_variable.elna +++ b/testsuite/runnable/inferred_variable.elna @@ -2,8 +2,12 @@ program() var x := 42 c: const := 10 + y := c + arr := [c, 2] begin x := x + 1; - assert(x = 43 & c = 10) + y := y + 5; + arr[1] := 99; + assert(x = 43 & c = 10 & y = 15 & arr[1] = 99 & arr[2] = 2) end. |
