Single return from function with multiple returns [4.4.2]

Is there something that doesn't work right in the latest GZDoom? Post about it here.

Moderator: GZDoom Developers

Forum rules
Please construct and post a simple demo whenever possible for all bug reports. Please provide links to everything.

If you can include a wad demonstrating the problem, please do so. Bug reports that include fully-constructed demos have a much better chance of being investigated in a timely manner than those that don't.

Please make a new topic for every bug. Don't combine multiple bugs into a single topic. Thanks!

Single return from function with multiple returns [4.4.2]

Postby Apeirogon » Mon Oct 05, 2020 12:54 pm

Function with multiple returns, like
Code: Select allExpand view
    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: Select allExpand view
    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.
User avatar
Apeirogon
I have a strange sense of humour
 
Joined: 12 Jun 2017

Re: Single return from function with multiple returns [4.4.2

Postby Player701 » 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.
User avatar
Player701
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support


Return to Bugs

Who is online

Users browsing this forum: No registered users and 0 guests