alliedmodders / sourcepawn

A small, statically typed scripting language.
Other
369 stars 63 forks source link

while(true) loop failes to compile #752

Closed c0rp3n closed 2 years ago

c0rp3n commented 2 years ago

When writing some stocks to replace some sourcepawn natives (testing reasons) I had a while true loop which now failes to compile with the latest canges on master.

[0.0390] SourcePawn Compiler 1.11
[0.0390] Copyright (c) 1997-2006 ITB CompuPhase
[0.0390] Copyright (c) 2004-2021 AlliedModders LLC
[0.0390]
[0.0390] E:\programming\spshell-includes\include\string.inc(22) : error 206: redundant test: constant expression is non-zero
[0.0390]
[0.0390] 1 Error.

This is the code in string.inc and line 22 is while(true).

stock int strlen(const char[] str)
{
    int i = 0;
    while(true)
    {
        if (str[i] == '\x0')
        {
            return i;
        }

        ++i;
    }
}

Whereas using a for loop with ;; works for this case.

stock int strlen(const char[] str)
{
    int i = 0;
    for(;;)
    {
        if (str[i] == '\x0')
        {
            return i;
        }

        ++i;
    }
}
asherkin commented 2 years ago

while(true) has always been a warning in SourcePawn.

stock int my_strlen(const char[] str)
{
    int i = 0;
    while(true)
    {
        if (str[i] == '\x0')
        {
            return i;
        }

        ++i;
    }
}

public void OnPluginStart()
{
    my_strlen("");
}
SourcePawn Compiler 1.10.0.6453
Copyright (c) 1997-2006 ITB CompuPhase
Copyright (c) 2004-2018 AlliedModders LLC

/groups/sourcemod/upload_tmp/text7V4gn8.sp(4) : warning 206: redundant test: constant expression is non-zero
/groups/sourcemod/upload_tmp/text7V4gn8.sp(13) : warning 209: function "my_strlen" should return a value

Note that the stock has to be used for the errors to show up pre-1.11, as previously error reporting was silenced for unused stocks (this is mentioned in the 1.11 migration doc).

c0rp3n commented 2 years ago

Ahh okay, just seems awkward when compiling with warning error, but that is fine will just use the workaround.