From 71b3bbb7698b13b42c99a4ca66c27db4a6189ff8 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sat, 1 Aug 2026 16:05:51 +0200 Subject: Enforce integral values in slice ranges --- boot/type_check.cc | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'boot/type_check.cc') diff --git a/boot/type_check.cc b/boot/type_check.cc index 07deb2d..529f66a 100644 --- a/boot/type_check.cc +++ b/boot/type_check.cc @@ -165,6 +165,11 @@ namespace elna::boot return "Type '" + this->actual.to_string() + "' cannot be converted to '" + payload.target.to_string() + "'"; } + else if constexpr (std::is_same_v) + { + return "The number " + payload.overflow.to_string() + + " does not fit into the type '" + actual.to_string() + "'"; + } else if constexpr (std::is_same_v) { switch (payload) @@ -762,6 +767,17 @@ namespace elna::boot void type_analysis_visitor::visit(slicing_expression *expression) { walking_visitor::visit(expression); + + if (!is_integral_type(resolve_underlying_type(expression->start().type_decoration))) + { + add_error(expression->start().position(), + expression->start().type_decoration, type_mismatch_error::kind::array_index); + } + if (!is_integral_type(resolve_underlying_type(expression->end().type_decoration))) + { + add_error(expression->end().position(), + expression->end().type_decoration, type_mismatch_error::kind::array_index); + } } void type_analysis_visitor::visit(array_access_expression *expression) @@ -1008,4 +1024,22 @@ namespace elna::boot } } } + + void type_analysis_visitor::visit(literal *expression) + { + bool narrowed{ false }; + if (expression->value.is_signed()) + { + narrowed = expression->value.fit_into(target.int_properties.size); + } + else + { + narrowed = expression->value.fit_into(target.word_properties.size); + } + if (!narrowed) + { + add_error(expression->position(), expression->type_decoration, + type_mismatch_error::integer_literal_overflow{ expression->value }); + } + } } -- cgit v1.2.3