Browse Source

* fix 41526: use the pretty type name for the RTTI name
+ added test

Sven/Sarah Barth 1 week ago
parent
commit
4611060e99
2 changed files with 117 additions and 1 deletions
  1. 1 1
      compiler/ncgrtti.pas
  2. 116 0
      tests/webtbs/tw41526.pp

+ 1 - 1
compiler/ncgrtti.pas

@@ -652,7 +652,7 @@ implementation
            def:=tarraydef(def).elementdef;
          { name }
          if assigned(def.typesym) then
-           tcb.emit_shortstring_const(ttypesym(def.typesym).realname)
+           tcb.emit_shortstring_const(ttypesym(def.typesym).prettyname)
          else
            tcb.emit_shortstring_const('');
       end;

+ 116 - 0
tests/webtbs/tw41526.pp

@@ -0,0 +1,116 @@
+program tw41526;
+
+{$IFDEF FPC}
+  {$MODE DELPHI}
+{$ENDIF}
+
+{$APPTYPE CONSOLE}
+
+uses
+  SysUtils,
+  Rtti,
+  TypInfo,
+  StrUtils;
+
+type
+  // Simple class
+  TMyClass = class
+  end;
+
+  // Simple record
+  TMyRecord = record
+    A, B: Integer;
+  end;
+
+  // Simple interface
+  IMyInterface = interface
+    ['{6BB98F7E-4B9E-4C88-8F76-90C2E47FB3D9}']
+    procedure DoSomething;
+  end;
+
+procedure CheckName(const aActual, aExpected: String);
+var
+  tmpactual, tmpexpected: String;
+begin
+  tmpactual := StringReplace(aActual, ' ', '', [rfReplaceAll]);
+  tmpexpected := StringReplace(aExpected, ' ', '', [rfReplaceAll]);
+  if not SameText(tmpactual, tmpexpected) then begin
+    Writeln('Name mismatch');
+    Writeln('   Expected: ', tmpexpected);
+    Writeln('   Actual: ', tmpactual);
+    ExitCode := 3;
+  end;
+end;
+
+var
+  Ctx: TRttiContext;
+  RType: TRttiType;
+  iname, sname: String;
+begin
+  Ctx := TRttiContext.Create;
+  try
+    RType := Ctx.GetType(TypeInfo(Integer));
+    if RType <> nil then
+      iname := RType.Name
+    else begin
+      Writeln('Integer RTTI not found');
+      Halt(1);
+    end;
+
+    RType := Ctx.GetType(TypeInfo(string));
+    if RType <> nil then
+      sname := RType.Name
+    else begin
+      Writeln('String RTTI not found');
+      Halt(2);
+    end;
+
+    // Dynamic array type (array of Integer)
+    RType := Ctx.GetType(TypeInfo(TArray<Integer>));
+    if RType <> nil then begin
+      Writeln('TArray<Integer> Name:    ', RType.Name);
+      CheckName(RType.Name, 'TArray<System.' + iname + '>');
+    end else
+      Writeln('TArray<Integer> RTTI not found');
+
+    // Dynamic array type (array of string)
+    RType := Ctx.GetType(TypeInfo(TArray<string>));
+    if RType <> nil then begin
+      Writeln('TArray<string> Name:    ', RType.Name);
+      CheckName(RType.Name, 'TArray<System.' + sname + '>');
+    end else
+      Writeln('TArray<string> RTTI not found');
+
+    // Dynamic array type (array of TMyClass)
+    RType := Ctx.GetType(TypeInfo(TArray<TMyClass>));
+    if RType <> nil then begin
+      Writeln('TArray<TMyClass> Name:    ', RType.Name);
+      CheckName(RType.Name, 'TArray<tw41526.TMyClass>');
+    end else
+      Writeln('TArray<TMyClass> RTTI not found');
+
+    // Dynamic array type (array of TMyRecord)
+    RType := Ctx.GetType(TypeInfo(TArray<TMyRecord>));
+    if RType <> nil then begin
+      Writeln('TArray<TMyRecord> Name:    ', RType.Name);
+      CheckName(RType.Name, 'TArray<tw41526.TMyRecord>');
+    end else
+      Writeln('TArray<TMyRecord> RTTI not found');
+
+    // Dynamic array type (array of IMyInterface)
+    RType := Ctx.GetType(TypeInfo(TArray<IMyInterface>));
+    if RType <> nil then begin
+      Writeln('TArray<IMyInterface> Name:    ', RType.Name);
+      CheckName(RType.Name, 'TArray<tw41526.IMyInterface>');
+    end else
+      Writeln('TArray<IMyInterface> RTTI not found');
+
+  finally
+    Ctx.Free;
+  end;
+
+  //Writeln;
+  //Writeln('Press ENTER to exit...');
+  //Readln;
+end.
+