Skip to content

Commit

Permalink
Fix calculation of constrained array index type. Fixes xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Sep 16, 2024
1 parent df40422 commit 8c1d736
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- Limited maximum instantiation depth to prevent crashes due to stack
overflow when elaborating a design that has unbounded recursive entity
instantiation (#969).
- Several other minor bugs were resolved (#961, #962, #971).
- Several other minor bugs were resolved (#961, #962, #971, #975).

## Version 1.13.3 - 2024-08-24
- Type checking was not performed correctly for conversion function
Expand Down
7 changes: 5 additions & 2 deletions src/names.c
Original file line number Diff line number Diff line change
Expand Up @@ -4769,6 +4769,8 @@ static type_t solve_range(nametab_t *tab, tree_t r)
tree_t left = tree_left(r);
tree_t right = tree_right(r);

const bool has_context = tab->top_type_set->members.count > 0;

type_t ltype;
if (is_unambiguous(left))
ltype = _solve_types(tab, left);
Expand All @@ -4792,8 +4794,9 @@ static type_t solve_range(nametab_t *tab, tree_t r)
rtype = _solve_types(tab, right);
}

tree_set_type(r, ltype);
return ltype;
type_t result = has_context ? ltype : type_base_recur(ltype);
tree_set_type(r, result);
return result;
}
default:
fatal_trace("invalid range subkind");
Expand Down
6 changes: 4 additions & 2 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -6374,12 +6374,14 @@ static type_t p_constrained_array_definition(type_t base, tree_t head)

mangle_type(nametab, sub);

type_t index_type = std_type(NULL, STD_INTEGER);
do {
tree_t r = p_discrete_range(head);
solve_types(nametab, r, index_type);
solve_types(nametab, r, NULL);
convert_universal_bounds(r);

tree_add_range(constraint, r);
type_add_index(base, tree_type(r));

head = NULL;
} while (optional(tCOMMA));

Expand Down
11 changes: 11 additions & 0 deletions test/bounds/issue975.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
entity array_issue is
end entity array_issue;

architecture rtl of array_issue is
constant C_LENGTH : positive := 7;
type t_array1 is array (C_LENGTH downto 0) of bit; -- OK
type t_array2 is array (0 to C_LENGTH) of bit; -- OK
type t_array3 is array (positive range 0 to C_LENGTH) of bit; -- Error
type t_array4 is array (positive range C_LENGTH downto 0) of bit; -- Error
begin
end architecture rtl;
20 changes: 20 additions & 0 deletions test/test_bounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,25 @@ START_TEST(test_issue966)
}
END_TEST

START_TEST(test_issue975)
{
input_from_file(TESTDIR "/bounds/issue975.vhd");

const error_t expect[] = {
{ 8, "left index 0 violates constraint POSITIVE" },
{ 9, "right index 0 violates constraint POSITIVE" },
{ -1, NULL }
};
expect_errors(expect);

parse_check_and_simplify(T_ENTITY, T_ARCH);

fail_unless(parse() == NULL);

check_expected_errors();
}
END_TEST

Suite *get_bounds_tests(void)
{
Suite *s = suite_create("bounds");
Expand Down Expand Up @@ -846,6 +865,7 @@ Suite *get_bounds_tests(void)
tcase_add_test(tc_core, test_subtype);
tcase_add_test(tc_core, test_issue951);
tcase_add_test(tc_core, test_issue966);
tcase_add_test(tc_core, test_issue975);
suite_add_tcase(s, tc_core);

return s;
Expand Down
4 changes: 1 addition & 3 deletions test/test_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,7 @@ START_TEST(test_layout)

input_from_file(TESTDIR "/jit/layout.vhd");

tree_t p = parse();
fail_if(p == NULL);
fail_unless(tree_kind(p) == T_PACKAGE);
tree_t p = parse_check_and_simplify(T_PACKAGE);

freeze_global_arena();
fail_unless(parse() == NULL);
Expand Down
18 changes: 13 additions & 5 deletions test/test_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ START_TEST(test_array)
{
tree_t p, d, a, g, s, e, r;
type_t t, i, b;
tree_t x;
tree_t x, left, right;

input_from_file(TESTDIR "/parse/array.vhd");

Expand Down Expand Up @@ -1478,11 +1478,19 @@ START_TEST(test_array)
fail_unless(tree_ranges(type_constraint(t, 0)) == 2);
fail_unless(type_indexes(type_base(t)) == 2);
r = tree_range(type_constraint(t, 0), 0);
fail_unless(tree_ival(tree_left(r)) == 1);
fail_unless(tree_ival(tree_right(r)) == 3);
left = tree_left(r);
fail_unless(tree_kind(left) == T_TYPE_CONV);
fail_unless(tree_ival(tree_value(left)) == 1);
right = tree_right(r);
fail_unless(tree_kind(right) == T_TYPE_CONV);
fail_unless(tree_ival(tree_value(right)) == 3);
r = tree_range(type_constraint(t, 0), 1);
fail_unless(tree_ival(tree_left(r)) == 4);
fail_unless(tree_ival(tree_right(r)) == 6);
left = tree_left(r);
fail_unless(tree_kind(left) == T_TYPE_CONV);
fail_unless(tree_ival(tree_value(left)) == 4);
right = tree_right(r);
fail_unless(tree_kind(right) == T_TYPE_CONV);
fail_unless(tree_ival(tree_value(right)) == 6);

e = parse();
fail_if(e == NULL);
Expand Down

0 comments on commit 8c1d736

Please sign in to comment.