Jump to page: 1 2
Thread overview
A code example that shows why I don't like warnings
Oct 18, 2019
Adam D. Ruppe
Oct 18, 2019
FeepingCreature
Oct 18, 2019
Adam D. Ruppe
Oct 18, 2019
Alexandru Ermicioi
Oct 18, 2019
Adam D. Ruppe
Oct 19, 2019
Alexandru Ermicioi
Oct 20, 2019
Daniel N
Oct 20, 2019
Ethan
Oct 20, 2019
Johan
Oct 21, 2019
Walter Bright
Oct 22, 2019
tsbockman
Oct 22, 2019
tsbockman
October 18, 2019
Consider this code:

-----

string defaultFormat(alias method)() {
        static foreach(attr; __traits(getAttributes, method))
                static if(is(typeof(attr) == string))
                        return attr;
        return "default";
}

void test() {}
@("test") void test2() {}

pragma(msg, defaultFormat!test);
pragma(msg, defaultFormat!test2);

void main() {}

-----


If you compile that with warnings enabled, you will get

Warning: statement is not reachable

on test, but on test2, that very *same code* it complains about is executed. If you remove it, it errors on the other test2 condition!



I know it is possible to refactor the attribute thing into a function (like phobos' getAttribute), but nevertheless, this kind of early return in a loop is a common enough pattern that I don't think the compiler should warn on.

I almost put this in the "Learn" forum asking for techniques, but really, I know how to refactor it already. I am more complaining about the compiler here :)

basically not a lot to say, just wanna make sure code like this is on the dev's radar if they ever revisit those warnings.
October 18, 2019
On Friday, 18 October 2019 at 16:38:38 UTC, Adam D. Ruppe wrote:
> Consider this code:
>
> -----
>
> string defaultFormat(alias method)() {
>         static foreach(attr; __traits(getAttributes, method))
>                 static if(is(typeof(attr) == string))
>                         return attr;
>         return "default";
> }
>
> void test() {}
> @("test") void test2() {}
>
> pragma(msg, defaultFormat!test);
> pragma(msg, defaultFormat!test2);
>
> void main() {}
>
> -----
>
>
> If you compile that with warnings enabled, you will get
>
> Warning: statement is not reachable

Behold, the shitty workaround:

string defaultFormat(alias method)() {
        alias attributes = __traits(getAttributes, method);
        static if (attributes.length == 0)
            	return "default";
        else static foreach(i, attr; attributes)
                static if(is(typeof(attr) == string))
                        return attr;
           		else static if (i == __traits(getAttributes, method).length - 1)
            		return "default";
}

This does **not** spark joy. But it works.
October 18, 2019
On Friday, 18 October 2019 at 16:43:54 UTC, FeepingCreature wrote:
> This does **not** spark joy. But it works.

Yes, I just put in a runtime condition to trick the compiler:

bool isScriptable(attributes...)() {
        bool nonConstConditionForWorkingAroundASpuriousDmdWarning = true;
        foreach(attribute; attributes) {
                static if(is(typeof(attribute) == string)) {
                        static if(attribute == scriptable) {
                                if(nonConstConditionForWorkingAroundASpuriousDmdWarning)
                                return true;
                        }
                }
        }
        return false;
}


the optimizer can prolly remove that condition anyway but it is good enough to silence the warning. still just all the work arounds are kinda gross.
October 18, 2019
On Friday, 18 October 2019 at 16:46:36 UTC, Adam D. Ruppe wrote:
> On Friday, 18 October 2019 at 16:43:54 UTC, FeepingCreature wrote:
>> This does **not** spark joy. But it works.
>
> Yes, I just put in a runtime condition to trick the compiler:
>
> bool isScriptable(attributes...)() {
>         bool nonConstConditionForWorkingAroundASpuriousDmdWarning = true;
>         foreach(attribute; attributes) {
>                 static if(is(typeof(attribute) == string)) {
>                         static if(attribute == scriptable) {
>                                 if(nonConstConditionForWorkingAroundASpuriousDmdWarning)
>                                 return true;
>                         }
>                 }
>         }
>         return false;
> }
>
>
> the optimizer can prolly remove that condition anyway but it is good enough to silence the warning. still just all the work arounds are kinda gross.

You can define a bool enum in the body of static if (inside foreach)  and then wrap default return statement with static if that checks if that enum was defined. If it wasn't then return "default".
October 18, 2019
On Friday, 18 October 2019 at 18:19:55 UTC, Alexandru Ermicioi wrote:
> You can define a bool enum in the body of static if (inside foreach)  and then wrap default return statement with static if that checks if that enum was defined. If it wasn't then return "default".

oh that's not horrible and it works too. But still I don't love it :)

BTW as of today my whole lib will compile with warnings including when exercising the more complicated templates!
October 19, 2019
On Friday, 18 October 2019 at 18:22:33 UTC, Adam D. Ruppe wrote:
> On Friday, 18 October 2019 at 18:19:55 UTC, Alexandru Ermicioi wrote:
>> You can define a bool enum in the body of static if (inside foreach)  and then wrap default return statement with static if that checks if that enum was defined. If it wasn't then return "default".
>
> oh that's not horrible and it works too. But still I don't love it :)
>
> BTW as of today my whole lib will compile with warnings including when exercising the more complicated templates!

Yeah it is not ideal. This trick can also shield you in case if multiple elements from static foreach matches inner static if, just by adding a check to it if this enum was defined earlier. Also beware of code blocks for foreach or inner static if, since it will limit enum declaration to that block and wont be available to static if defined outside of foreach (the one for default return).

October 20, 2019
On Saturday, 19 October 2019 at 16:14:20 UTC, Alexandru Ermicioi wrote:
> On Friday, 18 October 2019 at 18:22:33 UTC, Adam D. Ruppe wrote:
>> On Friday, 18 October 2019 at 18:19:55 UTC, Alexandru Ermicioi wrote:
>>> You can define a bool enum in the body of static if (inside foreach)  and then wrap default return statement with static if that checks if that enum was defined. If it wasn't then return "default".
>>
>> oh that's not horrible and it works too. But still I don't love it :)
>>
>> BTW as of today my whole lib will compile with warnings including when exercising the more complicated templates!
>
> Yeah it is not ideal. This trick can also shield you in case if multiple elements from static foreach matches inner static if, just by adding a check to it if this enum was defined earlier. Also beware of code blocks for foreach or inner static if, since it will limit enum declaration to that block and wont be available to static if defined outside of foreach (the one for default return).

template defaultFormat(alias method)
{
        static foreach(attr; __traits(getAttributes, method))
                static if(is(typeof(attr) == string))
                        enum defaultFormat = attr;

        static if(is(typeof(defaultFormat) == void))
	        enum defaultFormat = "default";
}

October 20, 2019
On Friday, 18 October 2019 at 16:38:38 UTC, Adam D. Ruppe wrote:
> Warning: statement is not reachable

This is also another valid argument for error/warning codes.

This exact thing happens *all the time* in my code, and it results in me having to write my static ifs and CTFE functions in a roundabout way just to not get the warning.

Whereas if I had the warning code, I could tell the compiler to suppress that warning. Exactly like I do in C++.
October 20, 2019
On Sunday, 20 October 2019 at 12:53:22 UTC, Ethan wrote:
> On Friday, 18 October 2019 at 16:38:38 UTC, Adam D. Ruppe wrote:
>> Warning: statement is not reachable
>
> This is also another valid argument for error/warning codes.
>
> This exact thing happens *all the time* in my code, and it results in me having to write my static ifs and CTFE functions in a roundabout way just to not get the warning.
>
> Whereas if I had the warning code, I could tell the compiler to suppress that warning. Exactly like I do in C++.

I once submitted a PR that enabled that functionality (being able to silence specific warnings, and the warning itself would mention its category), but it was refused. I haven't pursued it any further, but am still considering adding it to the better compiler for professional use (LDC ofc ;).

-Johan

[1] https://github.com/dlang/dmd/pull/5592

October 21, 2019
On 10/18/2019 9:38 AM, Adam D. Ruppe wrote:
> basically not a lot to say, just wanna make sure code like this is on the dev's radar if they ever revisit those warnings.

Nothing gets on the radar if you don't put it in bugzilla !
« First   ‹ Prev
1 2