A trap of SystemVerilog inline constraint

In Constraint Random Verification(CRV), we may write the inline constraint with below format:


In most cases,  this works. After be randomized, the value of item.var1 is _something_. But have you ever thought about the result of below codes:


You will find that the  both item.var1 is not set to 10, but a random value. Why? This is because the ‘scope’. For inline constraint, it first looks up the var1 in item scope, thus it finds var1. The inline constraint equals to:


That means assign the value to itself. So the result is a random value.

To achieve our goal, there are two ways.

First is rename the local variable name from var1 to _var1.

The second is add “local::” contributor before the local variable var1. Shown as below.



Then the item.var1 will be set to 10 after randomization.

A summary: The priority of the scope in inline constraint is