Constraint existence of value into an array

In reply to beslo:

Hard to say what might be wrong without a more complete example. If force_max_nesting_depth is being set to 1, I don’t know how it would not work unless you have not proper typed in the constraint.

The following works for me.

class A;
  rand int max_def[] = new[10];
  rand int max_nesting_depth;       // randomized to 19
  rand bit force_max_nesting_depth; // randomized to 1
  constraint c{
    max_nesting_depth == 19;
    force_max_nesting_depth == 1;
    if (force_max_nesting_depth) {
       max_def.sum() with (int'(item == max_nesting_depth)) == 1;
    }
  }
endclass
      
module top;
  A a = new;
  initial begin
    assert(a.randomize());
    foreach(a.max_def[i]) $write(a.max_def[i],);
    $display;
      end
endmodule

prints:

#  1907083669  -719142008          19  -399841475  1096848187  -575642381  1926162295 -1284042096   433992243 -2056237441