To initialize an array, negative values can not be specified (see error below).

Type casting to positive works,  if you have the exact bit-size-matching unsigned type.
If the typecast is to big, it also gives a range check.

But with $R- values that are to big, are truncated.

Except in bitpacked arrays.
- no range check error (that may be due to the sub range type ??)
- values to big, overwrite the neighbouring element

I think the latter is a bug. *If* the compiler accepts the value, then it should protect neighbouring elements.

So is this a bug?
What should happen?

Doing
gcBitPackTinyNegArray[1] := cardinal(-1);
at runtime seems to work fine. (gives a compile time warning too / $R-)

program test;
{$R-}

type
  TTinyNegRange = -2..3;
  TBitPackTinyNegArray  = bitpacked array [0..3] of TTinyNegRange;
  TTestArray  = bitpacked array [0..3] of byte;

const
  // Warning: range check error while evaluating constants (4294967294 must be between 0 and 255)
  // Works with $R-
//  gcBitPackTinyNegArray   : TTestArray = (2, cardinal(-2), 0, cardinal(-1));

  //Error: range check error while evaluating constants (-2 must be between 0 and 18446744073709551615)//
  //Also range check, even with $R-
//  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, -2, 0, -1);

  // No Error / WRONG Value, prints: 2, -2, -1, -1  // 0 is replaced by -1
  // same with $R-
  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, cardinal(-2), 0, cardinal(-1));

  // works
//  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, 6, 0, cardinal(-1));
//  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, 6, 0, 7);


begin
writeln(gcBitPackTinyNegArray[0]);
writeln(gcBitPackTinyNegArray[1]);
writeln(gcBitPackTinyNegArray[2]);
writeln(gcBitPackTinyNegArray[3]);

readln();
end.

_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to