# Unsupported Index type for an associative array in an interactive constraint

**URL:** <https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177>\
**Category:** SystemVerilog\
**Tags:** constraint-randomization, associative-array\
**Created:** [December 13, 2024, 2:38pm UTC](https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177 "2024-12-13T14:38:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yilou](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/yilou/32/2645_2.png) [@Yilou](https://verificationacademy.com/forums/u/Yilou)\
**Post date:** [December 13, 2024, 2:38pm UTC](https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177/1 "2024-12-13T14:38:16Z")

</div>

Hi, Guys

When using `vsim`, I encountered an error with an associative array where a `class` is used as the index during constraint randomization. Could you clarify whether this issue is caused by the tool, my code, or my understanding?

Part of the code:

```auto
class KeyClass;
  rand bit [31:0] id;
endclass

class ConstrainedAssocArrayClass;
  // Associative array with class keys
  rand bit [31:0] assoc_array[KeyClass];

  // Constraint to limit values
  constraint value_limit {
    foreach (assoc_array[key]) {
      assoc_array[key] < 32'd100; // Values must be less than 100
    }
  }

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 13, 2024, 5:13pm UTC](https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177/2 "2024-12-13T17:13:44Z")

</div>

The LRM says

> - A constraint may be any expression containing operands of integral or real types only
> - Subexpressions of a constraint may include operands that are neither integral nor real if the subexpression  
> contains no random variables and the result of the subexpression has an integral or real type (e.g., an equality operator with both sides having string operands).

---

<div class="post-metadata">

**Author:** ![Yilou](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/yilou/32/2645_2.png) [@Yilou](https://verificationacademy.com/forums/u/Yilou)\
**Post date:** [December 16, 2024, 8:50am UTC](https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177/3 "2024-12-16T08:50:28Z")

</div>

Hi, @dave_59

Thank you for your response!

Is my understanding correct? The reason class types indices are not supported is that they do not have a fixed value range and cannot be enumerated, which makes it impossible for the solver to parse and solve such dynamic types. On the other hand, other types like user-defined data types or strings can be enumerated, and that’s why they are allowed in constraint expressions.

If that’s the case, does it mean that using a wildcard as an index is also not allowed in constraint expressions?

Best regards,  
Yilou

---

<div class="post-metadata">

**Author:** ![MICRO\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@MICRO\_91](https://verificationacademy.com/forums/u/MICRO_91)\
**Post date:** [December 16, 2024, 10:58am UTC](https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177/4 "2024-12-16T10:58:23Z")

</div>

> [@Yilou](#):
>
> Is my understanding correct? The reason class types indices are not supported is that they do not have a fixed value range and cannot be enumerated, which makes it impossible for the solver to parse and solve such dynamic types. On the other hand, other types like user-defined data types or strings can be enumerated, and that’s why they are allowed in constraint expressions.

The constraint expression is required to be an integral expression.  
When you write ::

```auto
constraint value_limit {
    foreach (assoc_array[key]) {
      assoc_array[key] < 32'd100; // Not an integral expression !!
    }
  }

```

**Each element of assoc\_array[key] is a class type whereas assoc\_array[key].id is an integral type.** Your intention is to constraint the property ‘id’ for each object

```auto
  constraint value_limit {
    foreach (assoc_array[key]) {
      assoc_array[key].id < 32'd100; // Now it's an integral expression
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Yilou](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/yilou/32/2645_2.png) [@Yilou](https://verificationacademy.com/forums/u/Yilou)\
**Post date:** [December 16, 2024, 3:05pm UTC](https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177/5 "2024-12-16T15:05:44Z")

</div>

I don’t understand.  
The class is the index of the associative array, not the data type of the associative array.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 16, 2024, 7:50pm UTC](https://verificationacademy.com/forums/t/unsupported-index-type-for-an-associative-array-in-an-interactive-constraint/49177/6 "2024-12-16T19:50:43Z")

</div>

These are two separate issues. The way the LRM is written, you can’t use an associative array in a constraint with a class as in index type because the expression used as an index is not integral or real (reals are not allowed as associative array index types regardless of being in a constraint). There is no reason other than an oversight preventing this as long as the index is a non-random state variable.  
Note that constraints cannot construct or destroy associative array elements.

The issue with not being allowed to iterate with a wildcard index is also regardless of being in a constraint.
