Raised This Month: $51 Target: $400
 12% 

Compiler not smart enough


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
luxor
Member
Join Date: Jan 2014
Old 04-23-2016 , 05:31   Compiler not smart enough
Reply With Quote #1

Hi allied, i tried to compile one of my plugins and compiler throwed me this weird warning :

for this function :
PHP Code:
getNum(Time)
{
    switch (
Time)
    {
        case 
16..20:
        {
            return 
0;
        }
        case 
8..15:
        {
            return 
1;
        }
        case 
1..7:
        {
            return 
2;
        }
        default : 
// just in case
        
{
            return -
1
        }
    }

Hope it will be fixed
luxor is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 04-23-2016 , 07:10   Re: Compiler not smart enough
Reply With Quote #2

Code:
getNum(Time) {     switch (Time)     {         case 16..20:         {             return 0;         }         case 8..15:         {             return 1;         }         case 1..7:         {             return 2;         }     }     return -1; }
__________________

Last edited by Black Rose; 04-23-2016 at 07:10.
Black Rose is offline
luxor
Member
Join Date: Jan 2014
Old 04-23-2016 , 08:40   Re: Compiler not smart enough
Reply With Quote #3

i don't want to avoid the bug, i went here to report it and maybe wait for a fix
luxor is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 04-23-2016 , 09:36   Re: Compiler not smart enough
Reply With Quote #4

It's not a bug. This is how programming works in some cases.
If the compiler would be able to find out if there was a possibility to reach the end it would be too complicated.
This way it will warn you, but you make the ultimate choice if you care or not.
If it doesn't reach the end it doesn't even matter.

Here's another example using Android Studio. This will not warn you, it will throw an error making it unable to build.
Code:
    public int Whatever() {         int test = 0;         if ( test == 1 )             return 0;     }

Code:
15:33:24 Executing tasks: [:app:assembleDebug]
15:33:25 Gradle build finished with 2 error(s) in 693ms
15:33:25 Build APK: Errors while building APK. You can find the errors in the 'Messages' view.
Code:
C:\Nerladdat\AndroidStudioProjects\*snip*.java
Error:(31, 5) error: missing return statement
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 0.638 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console
__________________

Last edited by Black Rose; 04-23-2016 at 09:42.
Black Rose is offline
luxor
Member
Join Date: Jan 2014
Old 04-23-2016 , 09:45   Re: Compiler not smart enough
Reply With Quote #5

but returning in each case including default will result return and any code below that switch should be ignored because there is not any chance to get out of that switch
luxor is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 04-23-2016 , 09:54   Re: Compiler not smart enough
Reply With Quote #6

But... but... but...
So what?

In this case it's easy but the switch could be nested inside an if statement or another switch. And that is too complicated to build into a compiler. It will probably cause more errors. The golden rule in coding is keeping it simple.
__________________
Black Rose is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 04-23-2016 , 13:44   Re: Compiler not smart enough
Reply With Quote #7

I have to agree, the compiler returning the warning despite a default statement in the switch existing has to be a bug as the default statement SHOULD count as the "if all fails, return this value" for the function effectively neutralizing the warning.

Edit: Ya, definitely a bug, just tried it on https://spider.limetech.org/ with the following:

PHP Code:
Test(num)
{
    switch (
num)
    {
        case 
1:
            return 
5;
        default:
            return 
4;
    }

Got a warning with above test method when in reality, it's the same as:

PHP Code:
Test(num)
{
    return (
num == 4);


Last edited by WildCard65; 04-23-2016 at 13:51.
WildCard65 is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 04-23-2016 , 16:33   Re: Compiler not smart enough
Reply With Quote #8

Quote:
Originally Posted by Black Rose View Post
Code:
getNum(Time) {     switch (Time)     {         case 16..20:         {             return 0;         }         case 8..15:         {             return 1;         }         case 1..7:         {             return 2;         }     }     return -1; }
Man , this code is correct.
If number is in interval 16..20 then it will execute "return 0" and it will return 0 , return -1 won't be executed.

"return -1" will be executed only when number will be out of all cases.

P.S. I am speaking to the thread's author , just used your code as quote.
siriusmd99 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-23-2016 , 17:03   Re: Compiler not smart enough
Reply With Quote #9

Quote:
Originally Posted by siriusmd99 View Post
Man , this code is correct.
If number is in interval 16..20 then it will execute "return 0" and it will return 0 , return -1 won't be executed.

"return -1" will be executed only when number will be out of all cases.

P.S. I am speaking to the thread's author , just used your code as quote.
He knows the code is correct. What you don't know is that "return -1" won't be reached, since he has a "default" case. I remember Arkshine fixing something similar for if statements:
PHP Code:
if()
{
    return 
1
}
else
{
    return 
0
}

return -
// this should not be needed. 
__________________

Last edited by HamletEagle; 04-23-2016 at 17:04.
HamletEagle is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 04-24-2016 , 04:33   Re: Compiler not smart enough
Reply With Quote #10

solution is to remove default case and add return -1 at the end or just ignore that warning.
siriusmd99 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:12.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode