Recent

Author Topic: [SOLVED] Is this a compiler bug? What am I missing?  (Read 411 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 395
[SOLVED] Is this a compiler bug? What am I missing?
« on: December 13, 2025, 11:21:02 pm »
Laz 4.4. on Win 11

I'me getting a "Warning: Function result variable does not seem to be initialized" and I want to make certain I don't have a function that's not returning a result ...

Code: Pascal  [Select][+][-]
  1. Unit unitname;
  2. {$mode objfpc}{$H+}
  3.  
  4. Interface
  5.   //... other functions header here
  6.   function CreateStructAttr    : pr_StructAttr   ; overload;
  7.   function CreateStructAttr(const anID: String; Const aName: String; const aStructID  : String): pr_StructAttr; overload;
  8.  
  9. Implementation
  10.  
  11. function C1(const anSType: TStype; const aSize: Natural): pai_Polyrecord;
  12. begin
  13.    Result := NewPtrToStructurePolyRec(aSize , ski_None);
  14.    DoCreateElemSlots(Result,anSType);
  15. end;
  16.  
  17. function CreateStructAttr: pr_StructAttr;
  18. begin
  19.   Result := C1(SStructAttr, cc_struct_attr_size);
  20. end;
  21.  
  22. function CreateStructAttr(const anID: String; const aName: String; const aStructID: String): pr_StructAttr;
  23. begin
  24. //  Result := C1(SStructAttr, cc_struct_attr_size);
  25.   Result := CreateStructAttr; // <== Warning: Function Result variable does not seem to be initialized
  26.   DoPopulateCustomAttrSlots(Result,anID,aName);
  27.   Result^.AsStringChild[cc_cust_attr_type_idx] := aStructID;
  28. end;
  29.  

If I rename function CreateStructAttr to function EmptyStructAttr, the warning goes away ...
Or, if I uncomment the line //  Result := C1(SStructAttr, cc_struct_attr_size); and comment out
  Result := CreateStructAttr; the warning goes away ...


Is this a bug?
« Last Edit: December 14, 2025, 01:44:41 am by EganSolo »

jamie

  • Hero Member
  • *****
  • Posts: 7493
Re: Is this a compiler bug? What am I missing?
« Reply #1 on: December 14, 2025, 12:26:00 am »
Not a bug, what you did was wrong.

You are calling itself to set itself.

That basically would put the function into an incursive loop!
The only true wisdom is knowing you know nothing

EganSolo

  • Sr. Member
  • ****
  • Posts: 395
Re: Is this a compiler bug? What am I missing?
« Reply #2 on: December 14, 2025, 12:32:18 am »
Quote
Not a bug, what you did was wrong.
You are calling itself to set itself.

Really?
How am I doing that, when the call is to the function with no parameters?

Code: Pascal  [Select][+][-]
  1. function A: Integer; begin result := 1; end;
  2. function A(const j : integer): integer; begin result := A + j; // calls the first version, not this one. Am I missing something?
  3.  

jamie

  • Hero Member
  • *****
  • Posts: 7493
Re: Is this a compiler bug? What am I missing?
« Reply #3 on: December 14, 2025, 12:41:00 am »
Sorry about that, I didn't look up enough :(

What does Code tools point to when you hover over it?

Jamie
The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12026
  • Debugger - SynEdit - and more
    • wiki
Re: Is this a compiler bug? What am I missing?
« Reply #4 on: December 14, 2025, 01:10:35 am »
The function name is an alias for "result" or vice versa.

I.e., you can do
Code: Pascal  [Select][+][-]
  1. function foo: byte;
  2. begin
  3.   foo := 1; // set result to 1
  4. end;

If you instead want to call foo, then use "foo()"

So your code
Quote
Code: Pascal  [Select][+][-]
  1. Result := CreateStructAttr;
equals to
Code: Pascal  [Select][+][-]
  1. result := result:

EganSolo

  • Sr. Member
  • ****
  • Posts: 395
Re: Is this a compiler bug? What am I missing?
« Reply #5 on: December 14, 2025, 01:38:07 am »
Ah! Thanks, Martin! Now, Jamie’s answer makes sense! Somehow, I assumed the compiler would first_ look for a function whose signature matches what I'm calling, ergo CreateStructAttr, and if it finds one, it would resolve the call to that function. I had forgotten about result = function name (duh!)

Thanks guys!

jamie

  • Hero Member
  • *****
  • Posts: 7493
Re: [SOLVED] Is this a compiler bug? What am I missing?
« Reply #6 on: December 14, 2025, 01:46:54 am »
It's possible but not sure that if you include the unit name space in that call, it may find the outside one.

Code: Pascal  [Select][+][-]
  1. Result := UnitName.CreateStructAttr
  2.  
  3.  

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018