Overloading NOT unary operator is allowed by compiler, but ignored at evaluation.
Original Reporter info from Mantis: Thaddy
-
Reporter name: Thaddy de Koning
Original Reporter info from Mantis: Thaddy
- Reporter name: Thaddy de Koning
Description:
The unary NOT operator can be overloaded, but the compiler chooses the wrong path and uses a simple binary NOT.
Either way:
- It can be overloaded
- Or it should be marked as non-overloadable.
Steps to reproduce:
{$mode objfpc}{$H+}
type
ternary = (F, U, T);
operator and (const a,b:ternary):ternary;inline;
const lookupAnd:array[ternary,ternary] of ternary =
((F,F,F),(F,U,U),(F,U,T));
begin
Result:= LookupAnd[a,b];
end;
operator or (const a,b:ternary):ternary;inline;
const lookupOr:array[ternary,ternary] of ternary =
((F,U,T),(U,U,T),(T,T,T));
begin
Result := LookUpOr[a,b];
end;
operator not (const a:ternary):ternary;inline;
const LookupNot:array[ternary] of ternary =(T,U,F);
begin
Result:= LookUpNot[a];
end;
begin
// works as expected
writeln('AND');write(F and F);write(F and U);
writeln(F and T);write(U and F);write(U and U);
writeln(U and T);write(T and F);write(T and U);
writeln(T and T);
writeln;
//works as expected
writeln('OR');write(F or F);write(F or U);
writeln(F or T);write(U or F);write(U or U);
writeln(U or T);write(T or F);write(T or U);
writeln(T or T);
writeln;
// this fails, but compiles and runs w/o error indication.
// and renders the wrong results
writeln('NOT');
writeln(not F);// prints -1 unstead of T, it does not pick the overload!
writeln(not U);// prints -2 instead of U, it does not pick the overload!
writeln(not T);// prints -3 instead of F, it does not pick the overload!
// which makes this next construct impossible while the compiler suggests it's legal.
{
writeln(T and not F);
}
end.
Additional information:
Found during testing and playing with:
http://forum.lazarus.freepascal.org/index.php/topic,41144.0.html
The output of the program is:
AND table
FFF
FUU
FUT
OR table
FUT
UUT
TTT
NOT table
-1
-2
-3
Mantis conversion info:
- Mantis ID: 33696
- OS: Raspbian
- OS Build: Stretch
- Build: 38904
- Platform: armhf
- Version: 3.1.1
- Fixed in version: 3.1.1
- Fixed in revision: 39406 (#96e92527)
- Monitored by: » @KaiBurghardt (Kai Burghardt)