View Issue Details

IDProjectCategoryView StatusLast Update
0024548FPCCompilerpublic2020-03-02 22:54
ReporterKverde Assigned To 
PrioritynormalSeverityminorReproducibilityhave not tried
Status newResolutionopen 
Product Version2.6.2 
Summary0024548: Operator overloading is not working properly with the types of byte, word, cardinal
Descriptiontype
  TMyRecord = record
    a: Byte;
    b: Word;
  end;

operator := (A: Byte) : TMyRecord;
operator := (A: Word) : TMyRecord;

implementation

operator:=(A: Byte): TMyRecord;
begin
  Result.A := A;
  Result.B := 0;
end;

operator:=(A: Word): TMyRecord;
begin
  Result.A := 0;
  Result.B := A;
end;

...

  MyRecord1 := 255;
  MyRecord2 := 256;

  Writeln('MyRecord1.a = ', MyRecord1.a); // 255
  Writeln('MyRecord1.b = ', MyRecord1.b); // 0
  Writeln('MyRecord2.a = ', MyRecord2.a); // 0
  Writeln('MyRecord2.b = ', MyRecord2.b); // 0 // there should be 256
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

Kverde

2013-06-04 13:19

reporter   ~0068042

{$mode objfpc}{$H+}

Bart Broersma

2020-03-01 16:23

reporter   ~0121298

Currently it gives a compiler error: Error: Range check error while evaluating constants (256 must be between 0 and 255)
If you typecast 256 to Word(256) it calls the correct operator.

Sven Barth

2020-03-02 22:54

manager   ~0121325

The "problem" here is that Pascal prefers signed types. Thus 256 is internally a Int16 which by itself fits neither into a UInt8 (aka Byte) or UInt16 (aka Word). While in theory the compiler could handle this specific case cause the value is known at compile time, it does not.

Issue History

Date Modified Username Field Change
2013-06-04 05:50 Kverde New Issue
2013-06-04 13:19 Kverde Note Added: 0068042
2020-03-01 16:23 Bart Broersma Note Added: 0121298
2020-03-02 22:54 Sven Barth Note Added: 0121325