View Issue Details

IDProjectCategoryView StatusLast Update
0037796FPCCompilerpublic2020-10-02 20:19
ReporterVojtech Cihak Assigned ToFlorian  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Platformamd64OSLinux 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0037796: Function with untyped parameter accepts retyped constant
DescriptionFunction with untyped parameter (i.e procedure Any(const Anything);) accepts retyped constant (i.e. Cardinal([fsItalic, fsBold])).
Without retyping (you can uncomment the line) compilation fails with Error: Variable identifier expected
Steps To Reproduceprogram tformal;
{$mode objfpc}
 
type
  TFontStyle = (
    fsItalic,
    fsBold,
    fsUnderlined,
    fsStrikeOut
  );
  TFontStyles = set of TFontStyle;
 
var aFS: TFontStyles;
 
procedure Any(const Anything);
begin
  aFS:=aFS+TFontStyles(Anything);
end;
 
procedure DoIt;
begin
  //Any([fsItalic, fsBold]); //unit1.pas(31,25) Error: Variable identifier expected
  Any(Cardinal([fsItalic, fsBold])); //ok
end;
 
begin
  writeln(Cardinal(aFS));
  DoIt;
  writeln(Cardinal(aFS));
end.

[v1@nb-msi testset]$ fpc test.pas
Free Pascal Compiler version 3.3.1 [2020/09/04] for x86_64
Copyright (c) 1993-2020 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test.pas
Linking test
29 lines compiled, 0.2 sec
[v1@nb-msi testset]$ ./test
0
3
[v1@nb-msi testset]$
Additional InformationYou may look at https://forum.lazarus.freepascal.org/index.php?topic=51516.msg378344#msg378344
where PascalDragon confirmed with 3.0.4, 3.2.0 and 3.3.1.
TagsNo tags attached.
Fixed in Revision46973
FPCOldBugId
FPCTarget-
Attached Files

Activities

Florian

2020-09-23 22:07

administrator   ~0125795

What rules should the compiler apply for passing a set constant or an array of TFontStyles in this case?

Vojtech Cihak

2020-09-25 16:15

reporter   ~0125861

Last edited: 2020-09-25 16:15

View 2 revisions

For curiosity, I tested with Delphi 7.

  TDummy = 0..31;
  TDummySet = set of TDummy;

procedure Switch(var Par1; const Par2);
begin
  TDummySet(Par1):=TDummySet(Par1)+TDummySet(Par2);
end;

procedure TForm1.Button1Click(Sender: TObject);
var aFSs: TFontStyles;
begin
  aFSs:=[];
  Switch(aFSs, [fsBold]); //this works
  if fsBold in aFSs then Form1.Caption:='Bold is in set';
// Switch(aFSs, fsBold); //variable required
// Switch(aFSs, Cardinal(fsBold)); //variable required
// Switch(aFSs, Cardinal([fsBold])); //invalid typecast
end;

Old Delphi accepts const set but not const enum.

Florian

2020-09-26 16:22

administrator   ~0125876

And the caption of the form changes?

Vojtech Cihak

2020-09-26 20:42

reporter   ~0125882

Yes.

Sven Barth

2020-09-27 11:52

manager   ~0125897

For the sake of completeness, the following prints "3" in Delphi 10.2 which already supports dynamic array constructors:

program tformal;

{$apptype console}

uses
  SysUtils;

type
  TFontStyle = (
    tfBold,
    tfItalic,
    tfUnderline,
    tfStrikeOut
  );
  TFontStyles = set of TFontStyle;

procedure Any(const aArg);
begin
  Writeln(IntToHex(PLongInt(@aArg)^, 8));
end;

begin
  Any([tfBold, tfItalic]);
  //Any(LongInt([tfBold, tfItalic])); // does not compile
  Readln;
end.

Vojtech Cihak

2020-10-02 20:19

reporter   ~0126036

Thanks.

Issue History

Date Modified Username Field Change
2020-09-22 14:05 Vojtech Cihak New Issue
2020-09-23 22:07 Florian Note Added: 0125795
2020-09-25 16:15 Vojtech Cihak Note Added: 0125861
2020-09-25 16:15 Vojtech Cihak Note Edited: 0125861 View Revisions
2020-09-26 16:22 Florian Note Added: 0125876
2020-09-26 20:42 Vojtech Cihak Note Added: 0125882
2020-09-27 11:52 Sven Barth Note Added: 0125897
2020-09-27 18:38 Florian Assigned To => Florian
2020-09-27 18:38 Florian Status new => resolved
2020-09-27 18:38 Florian Resolution open => fixed
2020-09-27 18:38 Florian Fixed in Version => 3.3.1
2020-09-27 18:38 Florian Fixed in Revision => 46973
2020-09-27 18:38 Florian FPCTarget => -
2020-10-02 20:19 Vojtech Cihak Status resolved => closed
2020-10-02 20:19 Vojtech Cihak Note Added: 0126036