FPC fully evaluates case branches that it knows are unreachable, making some things (namely WRT generics) impossible to write
Original Reporter info from Mantis: Akira1364
-
Reporter name:
Original Reporter info from Mantis: Akira1364
- Reporter name:
Description:
Consider the following example:
program Example;
{$mode ObjFPC}
generic function Test<T>(const Value: T): T;
begin
case True of
True: Result := Value div Value;
False: Result := Value / Value;
end;
end;
begin
WriteLn(specialize Test<LongInt>(4));
end.
As written, FPC gives the following errors for it:
Example.pas(9,21) Hint: Use DIV instead to get an integer result
Example.pas(9,30) Error: Incompatible types: got "Double" expected "LongInt"
Example.pas(15,4) Fatal: There were 1 errors compiling module, stopping
However, if you change both branches to "Value div Value", it of course compiles fine, and gives the following hint:
Example.pas(9,14) Warning: Unreachable code
It would be nice if that known-unreachability was taken into account at all times, thus allowing more truly "generic" code. This kind of thing is especially relevant with case blocks using, say, "GetTypeKind(T)", because while they're (awesomely!) always optimized down to a single branch, they still cannot contain branches that are truly "invalid" for any particular specialization of "T".
Steps to reproduce:
See the provided example program.
Mantis conversion info:
- Mantis ID: 35766
- OS: Windows
- OS Build: 10
- Build: Trunk
- Platform: x86_64
- Version: 3.3.1