[fpc-pascal] 2 bugs ???

2011-09-07 Thread Yann Bat
Hi,

I am trying to learn freepascal generics and I think that I have found 2 bugs.

Tested with FPC 2.4.4 Linux x86

==
= BUG 1 - with SizeOf =
==
program SizeOfBug;
{$mode objfpc}{$H+}

type
  generic TGen_T = class(TObject)
private
  FField: _T;
public
  constructor Create(Val: _T);

  function Bug: Integer;
  end;

{--- TGen.Create ---}
constructor TGen.Create(Val: _T);
begin
  inherited Create;
  FField := Val;
end;

{--- TGen.Bug ---}
function TGen.Bug : Integer;
begin
  Result := 10 div SizeOf(_T);  // *** DIVISION BY ZERO ***

  // THE FOLLOWING CODE IS OK !
  //
  // var
  //   S: Integer;
  // begin
  //   S := SizeOf(_T);
  //   Result := 10 div S;
end;

type
  TGenInt = specialize TGenInteger;

var
  V: TGenInt;
begin
  V.Create(589);
  WriteLn('V.Bug = ', V.Bug);
  V.Free;
end.

==
= BUG 2 - with WriteLn =
==

program WriteLnBug;
{$mode objfpc}{$H+}

type
  generic TGen_T = class(TObject)
private
  FField: _T;
public
  constructor Create(Val: _T);

  procedure Bug;
  end;

{--- TGen.Create ---}
constructor TGen.Create(Val: _T);
begin
  inherited Create;
  FField := Val;
end;

{--- TGen.Bug ---}
procedure TGen.Bug;
begin
  WriteLn('FField = ', FField);  // *** CAN'T READ OR WRITE VARIABLES
OF THIS TYPE ***
end;

type
  TGenInt = specialize TGenInteger;

var
  V: TGenInt;
begin
  V.Create(589);
  V.Bug;
  V.Free;
end.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] 2 bugs ???Q

2011-09-07 Thread Marco van de Voort
In our previous episode, Yann Bat said:
 I am trying to learn freepascal generics and I think that I have found 2 bugs.

 var
   V: TGenInt;
 begin
   V.Create(589);

This is no pascal way of creating a class. Use v:=tgenint.create;

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] 2 bugs ???Q

2011-09-07 Thread Yann Bat
Thanks. You've just found bug number 3 ! :-)

But #1 and #2 are alway here.



2011/9/7 Marco van de Voort mar...@stack.nl:
 In our previous episode, Yann Bat said:
 I am trying to learn freepascal generics and I think that I have found 2 
 bugs.

 var
   V: TGenInt;
 begin
   V.Create(589);

 This is no pascal way of creating a class. Use v:=tgenint.create;

 ___
 fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
 http://lists.freepascal.org/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] 2 bugs ???

2011-09-07 Thread Leonardo M . Ramé
From: Yann Bat batyann...@gmail.com
To: fpc-pascal@lists.freepascal.org fpc-pascal@lists.freepascal.org
Sent: Wednesday, September 7, 2011 11:36 AM
Subject: [fpc-pascal] 2 bugs ???

Hi,

I am trying to learn freepascal generics and I think that I have found 2 bugs.

Tested with FPC 2.4.4 Linux x86

==
= BUG 1 - with SizeOf =
==
{--- TGen.Bug ---}
function TGen.Bug : Integer;
begin
  Result := 10 div SizeOf(_T);  // *** DIVISION BY ZERO ***

  // THE FOLLOWING CODE IS OK !
  //
  // var
  //   S: Integer;
  // begin
  //   S := SizeOf(_T);
  //   Result := 10 div S;
end;


The error message is correct, because inside the scope of method  Bug, _T 
points to nil, and the size of nil is 0, so 1 div 0 raises the error.

What about using Result := 100 div SizeOf(FField); ?

Leonardo.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] 2 bugs ???

2011-09-07 Thread Yann Bat
2011/9/7 Leonardo M. Ramé martinr...@yahoo.com:
 From: Yann Bat batyann...@gmail.com


 The error message is correct, because inside the scope of method  Bug, _T 
 points to nil, and the size of nil is 0, so 1 div 0 raises the error.


I think that SizeOf(_T) should be unknown rather than 0. The compiler
should not make any assumption about _T before specialization.

 What about using Result := 100 div SizeOf(FField); ?

Same error with SizeOf(FField).




 Leonardo.
 ___
 fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
 http://lists.freepascal.org/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal