Jump to content
chkaufmann

Missing compiler warning

Recommended Posts

Hi,

 

I just fixed an error in the following function. I had to add the last line where I assign tmp to Result.

Now I wonder, why I didn't get a compiler warning like "return value of function might be undefined.". I get such errors in the same project with other methods, so the options are set correctly.

 

Christian

 

class function TLnxResult.GenForEvent(AData: TLnxObject; AEventId, AAgegroupId: Integer): IBSEnumerable<IBSSwLenexResult>;
var
  data  : TLnxAbstractObject;
  clubs : TLnxCollection;
  tmp   : IBSList<IBSSwLenexResult>;
begin
  tmp := TBSGenerics.GenList<IBSSwLenexResult>;
  data := AData;
  while Assigned(data) and (data.Tag <> lxCOMPETITION)
    do data := data.Parent;
  clubs := TLnxCollection(TLnxObject(data).ChildObject[lxCLUBS]);
  if Assigned(clubs) then for var ixC := 0 to clubs.ChildCount - 1 do begin
    for var e in TLnxEntryResult.GenForClub(lxRESULTS, TLnxCollection(clubs.Objects[ixC].ChildObject[lxPERSONS]), AEventId, AAgegroupId)
      do tmp.Add(e as IBSSwLenexResult);
    for var e in TLnxEntryResult.GenForClub(lxRESULTS, TLnxCollection(clubs.Objects[ixC].ChildObject[lxRELAYS]), AEventId, AAgegroupId)
      do tmp.Add(e as IBSSwLenexResult);
  end;
  tmp.Sort(function(const AResult1, AResult2: IBSSwLenexResult): Integer
           begin
             Result := AResult1.Place(nil) - AResult2.Place(nil);
             if Result.IsZero and (AResult1.Place(nil) = BSSW_MAX_PLACE)
               then Result := AResult1.Status.AsInteger - AResult2.Status.AsInteger;
           end);
  Result := tmp; // <<<< this line was missing.
end;

 

Share this post


Link to post

You don't get this because the return type is a managed type and so is actually passed as an extra var parameter. So the variable is assumed to have been default initialised outside the function. 

 

I'm not trying to defend the compiler design here. The handling of function return variables is a complete mess in my view. They should be passed by value from callee to caller. The whole hidden var param is a weird hack from the ancient past that should never have been done. 

 

 

  • Like 2

Share this post


Link to post

So just that I get it right: Because my return value is an interface type, the compiler adds something like this as first statement of the method:

 

Result := nil;

 

Chrisitan

Share this post


Link to post
9 minutes ago, chkaufmann said:

So just that I get it right: Because my return value is an interface type, the compiler adds something like this as first statement of the method:

 

Result := nil;

Not quite. The default initialisation is outside the function. 

Share this post


Link to post
54 minutes ago, David Heffernan said:

You don't get this because the return type is a managed type and so is actually passed as an extra var parameter.

Indeed. I would have written an "out" parameter in the docs, at least. The function should set or initialize the value.

And the compiler should emit a warning in your case.
FPC is much more paranoid about such warnings, but at least it tries to warn anything dubious, and allow to disable false positives with the {$H-} trick on the faulty line.

Edited by Arnaud Bouchez
  • Like 1

Share this post


Link to post

Reported and complained about numerous times - like https://quality.embarcadero.com/browse/RSP-21023

 

I think Allen Bauer somewhere explained this a bit more detailed but here is the short version I remember:

The compiler internally takes some shortcuts and the part that produces the warning does not know that the var parameter it sees was originally meant to be the function result.

 

My advice: spend a few bucks for FixInsight - it will catch this and other things the compiler does not.

Share this post


Link to post

Yes, FixInsight is great. Although it doesn't get as many updates anymore as it did in the first year. Basically only one per Delphi version. But 2020 seems to be a better year for FixInsight customers 😉

Edited by luebbe

Share this post


Link to post
17 minutes ago, luebbe said:

Although it doesn't get as many updates anymore as it did in the first year.

Probably it is mature now.

Share this post


Link to post
13 minutes ago, David Heffernan said:

Probably it is mature now.

Yes and no. There was almost no activity on FixInsight from the end of 2017 until the end of 2019 and there were several open issues. By the end of 2019 bugs were fixed again and new features were added.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×