pawn-lang / compiler

Pawn compiler for SA-MP with bug fixes and new features - runs on Windows, Linux, macOS
Other
305 stars 72 forks source link

Sizeof for enums #44

Closed Misiur closed 9 years ago

Misiur commented 9 years ago

I'm not certain how compiler handles enumerators, but sizeof doesn't bode well with hidden array sizes:

enum E_TEST {
    RandomValue,
    StringValue[64]
};

new Anything[MAX_PLAYERS][E_TEST];

//...
sizeof Anything[0][StringValue]; //1 - it makes sense for compiler, but not for average user
Arkshine commented 9 years ago

You should use sizeof Anything[][StringValue]

Misiur commented 9 years ago

Ok, if Zeex says it's an unreasonable request I'll respect that. But very often I see people caught up with default values, for example strcat:

native strcat(dest[], const source[], maxlength=sizeof dest);

So

strcat(Anything[0][StringValue], "Dickbutt");

Simply fails.

Arkshine commented 9 years ago

There is a known issue with enum field and sizeof in this situation. It has been fixed in the Pawn 3.3.3930.

This should be similar to this : https://github.com/Arkshine/amxmodx/commit/a873066466286217dc2caa83b9259eae21fae406

Zeex commented 9 years ago

I was thinking... could this possibly break something? From the language point of view, it seems totally fine to return 1 for sizeof Anything[0][StringValue] since enums are just array indices IIRC.

@Y-Less what do you think?

Y-Less commented 9 years ago

I've always seen "1" here as the correct return. This enum:

enum X
{
    A,
    B[10],
    C
}

Is entirely equivalent to:

enum X
{
    A,
    B,
    C = B + 10
}

Or:

#define A X_tag:0
#define B X_tag:1
#define C X_tag:11
#define X X_tag:12

People would entirely expect "sizeof (B)" to return "1" there. "B" is just a subset of "X", asking for its size is asking for the size of part of an array, not the whole array, and with this patch not even an element of the array.

Also, I have always said "enums are not arrays" - they are flattened by the compiler. Doing:

new arr[X];
arr[B][3];

Is just fancy syntax for:

new arr[X];
arr[B + X_tag:3];

I didn't comment on this earlier, as if a majority of people think it should be different (as even the later official versions of PAWN seem to think it should) then who am I to argue? But as I say, given everything here, I don't think it makes sense from a language point of view. And since you asked directly...

Arkshine commented 9 years ago

I'm not sure we are talking about the same thing (patch is not about return of sizeof Anything[0][StringValue], also it should not possible to do that, I mean with 0), but how is handled an enum internally is irrelevant to the fact that if you use an enum as a "structure", you would want to expect sizeof B returns 10 and not 1. You're asking sizeof of an array not the index in the internal flat array. Enum in Pawn is an ugly hack for sure.

Zeex commented 9 years ago

Indeed sizeof Anything[0][StringValue] doesn't compile outside of function calls. I was worried that this might have been used in some parts of YSI or perhaps other libraries.

I also looked this up in newer Pawn sources, couldn't find Pawn 3.3.3930 referenced in https://github.com/Arkshine/amxmodx/commit/a873066466286217dc2caa83b9259eae21fae406 but 3.3.4058 has it. I guess they knew what they were doing and had thought about possible backwards compatibility issues in advance.

Y-Less commented 9 years ago

I can't actually think of a place where it would cause issues, at least not for YSI. To be fair, thinking about it more it might actually be better.

Zeex commented 9 years ago

OK, then there are apparently no drawbacks, plus it's in the official Pawn. Let's leave it here.