nickg / nvc

VHDL compiler and simulator
https://www.nickg.me.uk/nvc/
GNU General Public License v3.0
631 stars 77 forks source link

type of string literal cannot be determined from the surrounding context for arrays of length 1 #853

Closed m-kru closed 7 months ago

m-kru commented 7 months ago

Why does the following work:

  type mask_array_t is array (natural range <>) of bit_vector(31 downto 0);
  constant MASKS : mask_array_t := (x"00000000", x"FFFFFFFF");

but this one does not:

  type mask_array_t is array (natural range <>) of bit_vector(31 downto 0);
  constant MASKS : mask_array_t := (x"00000000");

I get

** Error: type of string literal cannot be determined from the surrounding context
    > /home/mkru/workspace/vhdl/vhdl-amba5/apb/tb/crossbar/tb-two-requesters-one-completer.vhd:32
    |
 32 |   constant MASKS : mask_array_t := (x"00000000");
    |                                     ^^^^^^^^^^^
    |
    = Note: context contains type MASK_ARRAY_T which is not a one dimensional array of character type

Is it defined in the LRM that I have to explicitly define type of the initial value in the case of one element arrays?

nickg commented 7 months ago

The expression (x"00000000") isn't a one element array aggregate, it's a parenthesised expression equivalent to x"00000000". You need to use a named association in this case, i.e. (0 => x"00000000"). See section 9.3.3.1 in the 2008 LRM:

Aggregates containing a single element association shall always be specified using named association in order to distinguish them from parenthesized expressions.