"Range must be bounded by constant expression" - Error from a constraint block

In reply to naveensv:

You can’t specify a dynamic range when slicing a dynamic array (regardless of whether it’s in a constraint or anywhere else). This is an LRM limitation. You can work around this by using an intermediary variable:


class some_class;
  rand int current[], next[];
  rand int unsigned total_count[];

  constraint legal_total_counts {
    total_count.size() == current.size();
    foreach (total_count[ i]) {
      total_count[ i] > 0;
      total_count[ i] < total_count.size();
    }
  }

  local rand int legal_nexts[][];
  
  constraint next_vals {
    next.size() == current.size();
    
    legal_nexts.size() == next.size();
    foreach (legal_nexts[ i])
      legal_nexts[ i].size() == total_count[ i];
    foreach (legal_nexts[ i, j])
      legal_nexts[ i][j] == current[j];

    foreach (next[ i])
      next[ i] inside { legal_nexts[ i] };
  }
endclass

The ‘legal_nexts’ 2D array will hold all valid values for each value of the next array. You need to construct it “yourself” using constraints. After you’re finished with it, it might be a good idea to destroy it to free up memory:


function void post_randomize();
  legal_nexts = '{};
endfunction