Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tagof(<tag name>:) in a switch statement bug #20

Closed
Daniel-Cortez opened this issue Jul 25, 2016 · 3 comments
Closed

tagof(<tag name>:) in a switch statement bug #20

Daniel-Cortez opened this issue Jul 25, 2016 · 3 comments
Assignees
Labels

Comments

@Daniel-Cortez
Copy link
Contributor

Daniel-Cortez commented Jul 25, 2016

This bug was originally discovered by @ziggi.

#include <console>
#include <float>

stock PrintInt(i)
    printf("int: %d\n", i);

stock PrintFloat(Float:f)
    printf("float: %f\n", f);

stock PrintBool(bool:b)
    printf("bool: %s\n", (b)?("true"):("false"));

stock Print({_, Float, bool}:arg, arg_tag=tagof(arg))
{
    switch(arg_tag)
    {
        case tagof(Float:):             // Line 17
            PrintFloat(Float:arg);
        case tagof(bool:):
            PrintBool(bool:arg);
        default:
            PrintInt(_:arg);
    }
}

main()
{
    print("--Function overloading emulation test--\n");
    Print(1);
    Print(1.0);
    Print(true);
}

When I try to compile this the compiler shows a bunch of error messages:

test.p(17) : error 081: expression with tag override must appear between parentheses
test.p(17) : error 029: invalid expression, assumed zero
test.p(19) : warning 217: loose indentation
test.p(19) : error 014: invalid statement; not in switch
test.p(19) : warning 215: expression has no effect
test.p(19) : error 001: expected token: ";", but found ":"
test.p(19) : error 029: invalid expression, assumed zero
test.p(19) : fatal error 107: too many error messages on one line
6 Errors.
Compilation aborted.

But if I put the tagof expressions into parentheses...

        case (tagof(Float:)):               // Line 17
            PrintFloat(Float:arg);
        case (tagof(bool:)):
            PrintBool(bool:arg);

... the code compiles and works normally.

@Daniel-Cortez
Copy link
Contributor Author

Daniel-Cortez commented Jul 26, 2016

Another bug related to the tagof operator:

#include <console>
#include <float>

stock PrintInt(i)
    printf("int: %d\n", i);

stock PrintFloat(Float:f)
    printf("float: %f\n", f);

stock PrintBool(bool:b)
    printf("bool: %s\n", (b)?("true"):("false"));

stock Print({_, Float, bool}:arg, arg_tag=tagof(arg))
{
    switch(arg_tag)
    {
        case (tagof(_:)):
            PrintInt(_:arg);
        case (tagof(Float:)):
            PrintFloat(Float:arg);
        case (tagof(bool:)):
            PrintBool(bool:arg);
    }
}

main()
{
    print("--Function overloading emulation test--\n");
    Print(1);
    Print(1.0);
    Print(true);
}

Output:

--Function overloading emulation test--
float: 1.00000
bool: true

Note the lack of the "int: 1" line.
I added some debug output to Print and main and commented out the remaining code in Print:

stock Print({_, Float, bool}:arg, arg_tag=tagof(arg))
{
    printf("arg_tag = %d\n", arg_tag);
    /*
    switch(arg_tag)
    {
        case (tagof(_:)):
            PrintInt(_:arg);
        case (tagof(Float:)):
            PrintFloat(Float:arg);
        case (tagof(bool:)):
            PrintBool(bool:arg);
    }
    */
}

main()
{
    print("--Function overloading emulation test--\n");
    printf("tagof(_:) = %d\n", tagof(_:));
    printf("tagof(Float:) = %d\n", tagof(Float:));
    printf("tagof(bool:) = %d\n", tagof(bool:));
    Print(1);
    Print(1.0);
    Print(true);
}

And here's what I got:

--Function overloading emulation test--
tagof(_:) = -(
tagof(Float:) = -1073741822
tagof(bool:) = -2147483647
arg_tag = 0
arg_tag = -1073741822
arg_tag = -2147483647

-( is supposed to be -2147483648 (which is another bug that is well known in the SA-MP community; not sure if it was reported here though).
So that means that tagof(_:) is -2147483648, but if I pass an integer to the Print function, arg_tag becomes 0 for some reason. For the other tags everything works normally.

@compuphase compuphase added the bug label Aug 1, 2016
@compuphase compuphase self-assigned this Aug 1, 2016
@compuphase
Copy link
Owner

The bug is that tagof(_:) should be 0, not -2147483648.
The other issue is that -2147483648 cannot be printed; this is a bug in printf().

@compuphase
Copy link
Owner

Fixed in commit a25041a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants