Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-10

Enum range check error

7 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

Is this a bug in generics with default()?

=====================

type
  TEnum = (Unused = '.');
  generic TSomething<T> = class
    function Get: T;
  end;
  TEnumeratedSomething = specialize TSomething<TEnum>;
function TSomething.Get: T;
begin
  // Range check error while evaluating constants (0 must be between 46 and 46)
  exit(Default(T));
end;

Regards,
Ryan Joseph

Reply by email

Jonas Maebe
to Ryan Joseph
Ryan Joseph wrote 16 lines
Is this a bug in generics with default()? ===================== type TEnum = (Unused = '.'); generic TSomething<T> = class function Get: T; end; TEnumeratedSomething = specialize TSomething<TEnum>; function TSomething.Get: T; begin // Range check error while evaluating constants (0 must be between 46 and 46) exit(Default(T)); end;

See https://bugs.freepascal.org/view.php?id=34972 for some comments about Default. So this is a bug in the usage of Default, since Default cannot be guaranteed to return a valid value for a type.

It is even technically impossible to make Default return a valid value for every type due to variant records (you could have overlapping "field1: 1..3;" and "field2: 4..6;").

Jonas

Reply by email

Ryan Joseph
to Jonas Maebe
Show quoted text 5 lines
On Oct 21, 2020, at 2:08 PM, Jonas Maebe via fpc-pascal wrote: See https://bugs.freepascal.org/view.php?id=34972 for some comments about Default. So this is a bug in the usage of Default, since Default cannot be guaranteed to return a valid value for a type.

I thought default would just return the first value in the enum but I guess it's just zeros. Maybe we need a better default for objfpc mode.

Regards,
Ryan Joseph

Reply by email

Jonas Maebe
to Ryan Joseph
Ryan Joseph wrote 9 lines
> On Oct 21, 2020, at 2:08 PM, Jonas Maebe via fpc-pascal wrote: > > See https://bugs.freepascal.org/view.php?id=34972 for some comments > about Default. So this is a bug in the usage of Default, since Default > cannot be guaranteed to return a valid value for a type. I thought default would just return the first value in the enum but I guess it's just zeros. Maybe we need a better default for objfpc mode.

I tried to explain in the rest of my message why that cannot be done. It might be possible in some cases, but that would just create a false sense of security and unpredictable results (because no compile-time range checking is possible for variant records).

Jonas

Reply by email

Ryan Joseph
to Jonas Maebe: I tried to explain in the rest of my message why that cannot be done. It might be…
Show quoted text 6 lines
On Oct 21, 2020, at 2:43 PM, Jonas Maebe via fpc-pascal wrote: I tried to explain in the rest of my message why that cannot be done. It might be possible in some cases, but that would just create a false sense of security and unpredictable results (because no compile-time range checking is possible for variant records).

Not even for enums? I get that records should just return zero'd memory but enums seem to be a special case.

Regards,
Ryan Joseph

Reply by email

Jonas Maebe
to Ryan Joseph
Ryan Joseph wrote 7 lines
> On Oct 21, 2020, at 2:43 PM, Jonas Maebe via fpc-pascal wrote: > > I tried to explain in the rest of my message why that cannot be done. It > might be possible in some cases, but that would just create a false > sense of security and unpredictable results (because no compile-time > range checking is possible for variant records). Not even for enums? I get that records should just return zero'd memory but enums seem to be a special case.

A record field whose type is an enum should be handled exactly the same as an individual enum.

Jonas

Reply by email