Single return from function with multiple returns [4.4.2]

Mon Oct 05, 2020 12:54 pm

Function with multiple returns, like
Code:
    double, int, bool get_something()
    {
        return 1.1, 15, true;
    }

can be compiled and executed when amount of returned arguments dont match with amount of declared arguments, i.e. this is a valid function for Gzdoom
Code:
    double, int, bool get_something()
    {
        return 1.1;
    }


Example
single_multiple_return_bug.zip


If its intentional design, can it be addressed/make it more clear with warning message on startup like "Warning, <path to file>, <line>: Expected X arguments, but returns only Y"?!
You do not have the required permissions to view the files attached to this post.

Re: Single return from function with multiple returns [4.4.2

Mon Oct 05, 2020 2:48 pm

Sometimes the semantics of a multiple-return function dictate that subsequent return values only make sense when the first return value satisfies a certain condition (non-null, greater than zero etc). In this case, when the other values are meaningless, you can return only the first one, knowing that the subsequent values will not be accessed.

However, with +vm_jit 0, such code triggers an assertion failure in GZDoom. It doesn't make GZDoom crash and only appears in debug builds. I've recently discovered it because my own mod had a similar portion of code where I returned only one value from a method that normally returns two values. It never caused any issues because I didn't use the second value when I knew it wasn't present. Just in case though, I still decided to fix it before I released the next version of the mod.

I guess if you try to use a return value when it isn't there, it is considered "undefined behavior" (oh how I hate that term...) similar to reading from an uninitialized variable. From my experience, if you don't attempt to use it, everything will work fine. Ideally though, I think it'd be better if returning a different number of values wasn't legal at all (could be done with a ZScript version bump) or at least produced a warning at compile time.