Thread overview
[Issue 17445] [REG2.073.0] Disagreement between 'static assert' and 'static if'
[Issue 17445] Disagreement between 'static assert' and 'static if'
May 27, 2017
Dario Schiavon
May 27, 2017
Stanislav Blinov
May 27, 2017
Stanislav Blinov
May 28, 2017
Dario Schiavon
May 28, 2017
Dario Schiavon
Jul 02, 2017
Vladimir Panteleev
Jul 02, 2017
Vladimir Panteleev
Nov 29, 2018
RazvanN
May 27, 2017
https://issues.dlang.org/show_bug.cgi?id=17445

--- Comment #1 from Dario Schiavon <dario.schiavon@gmail.com> ---
See also https://issues.dlang.org/show_bug.cgi?id=17446 for a similar issue, which however is also present on LDC2 (so it can't actually be the same issue).

--
May 27, 2017
https://issues.dlang.org/show_bug.cgi?id=17445

Stanislav Blinov <stanislav.blinov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |stanislav.blinov@gmail.com

--- Comment #2 from Stanislav Blinov <stanislav.blinov@gmail.com> ---
There are isCopyable and isAssignable traits in std.traits. Or you can roll your own:

enum bool isCopyable(T) = is(typeof(() { T* a, b; *a = *b; }));

using pointers in case e.g. the default ctor is disabled.

--
May 27, 2017
https://issues.dlang.org/show_bug.cgi?id=17445

--- Comment #3 from Stanislav Blinov <stanislav.blinov@gmail.com> ---
Sorry, that was isAssignable.
isCopyable would be:

enum bool isCopyable(T) = is(typeof(() { T* a; T b = *a; }));

--
May 28, 2017
https://issues.dlang.org/show_bug.cgi?id=17445

--- Comment #4 from Dario Schiavon <dario.schiavon@gmail.com> ---
Stanislav, thanks for the tip. I knew about isAssignable but I had somehow missed isCopyable, which is exactly what I needed.

However, I still cannot make my code work because of the bug shown here. isAssignable only gives the right answer as long as I don't try to use it as a static-if condition.

For example:

enum bool isCopyable(T) = is(typeof(() { T* a; T b = *a; }));

struct MyStruct
{
    enum result = isCopyable!MyStruct;
    pragma(msg, result);
    //static if (result) {}
}

This code (with the static-if commented out) correctly prints true. But if I
leave the static-if in the struct's body, it prints false (DMD 2.074.0).

If the static-if is moved outside of the struct's body, or if the condition doesn't involve the struct's name, then everything works as expected - but that's not what I need.

--
May 28, 2017
https://issues.dlang.org/show_bug.cgi?id=17445

--- Comment #5 from Dario Schiavon <dario.schiavon@gmail.com> ---
I guess I can speculate about the reason for this behavior. What I'm doing, is making a copiability test on a type that hasn't been completely defined yet. As long as there's no static-if depending on the answer, the answer can be given. Otherwise, the code inserted by the static-if might cause the answer to change, leading to a paradox.

Of course I had something else in mind when I wrote that code. I expected the answer to be based only on the code written so far, and I didn't intend the static-if to produce a paradox. But I guess it would be difficult for the compiler to prove that no paradox happens.

If all of this is correct, than it would certainly be better that the compiler emit an error message and stop the compilation, rather than give an answer that is surely 100% wrong.

--
July 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17445

Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dlang-bugzilla@thecybershad
                   |                            |ow.net
           Hardware|x86                         |All
            Summary|Disagreement between        |[REG2.073.0] Disagreement
                   |'static assert' and 'static |between 'static assert' and
                   |if'                         |'static if'
                 OS|Windows                     |All
           Severity|normal                      |regression

--- Comment #6 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
This appears to be a regression.

Introduced in https://github.com/dlang/dmd/pull/6388

--
July 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17445

--- Comment #7 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
(In reply to Vladimir Panteleev from comment #6)
> Introduced in https://github.com/dlang/dmd/pull/6388

Specifically:

https://github.com/dlang/dmd/pull/5972 (PR to scope branch) Commit 62be2fbda0a09e1a5dd1fa6024f81c165057d386

--
November 29, 2018
https://issues.dlang.org/show_bug.cgi?id=17445

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |razvan.nitu1305@gmail.com
         Resolution|---                         |FIXED

--- Comment #8 from RazvanN <razvan.nitu1305@gmail.com> ---
Fixed by: https://github.com/dlang/dmd/pull/8993

--