assert semantic change proposal

Artur Skawina via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 6 09:59:17 PDT 2014


On 08/06/14 18:00, via Digitalmars-d wrote:
> I am quite confident that assume(false) anywhere in your 
> program is basically stating that the program is unsound 
> (true==false) and should not be compiled and run at all.

No, an assume(false) in a program only means that every _path_
_leading_to_that_statement is 'unsound'. For practical purposes
it's better to treat 'unsound' as impossible and unreachable.

IOW

     import std.stdio, std.array;
     int main(string[] argv) {
        if (argv.length<2)
           assume(0);
        if (argv.length==1)
           writeln("help text");
        return argv.empty;
     }

=>

0000000000403890 <_Dmain>:
    403890:       31 c0                   xor    %eax,%eax
    403892:       c3                      retq


The alternatives would be to make it either: a) always a compile
error, or b) always a runtime error. The former would add little
value (`static assert` already exists); the latter is already
available as `assert(0)`.
The above example after "s/assume(0)/assert(0)/" becomes:

0000000000403890 <_Dmain>:
    403890:       48 83 ff 01             cmp    $0x1,%rdi
    403894:       76 03                   jbe    403899 
<_Dmain+0x9>
    403896:       31 c0                   xor    %eax,%eax
    403898:       c3                      retq
    403899:       50                      push   %rax
    40389a:       e8 71 e7 ff ff          callq  402010 <abort at plt>

IOW the compiler can still optimize based on the (un)reachability,
but the behavior in no longer undefined.

artur


More information about the Digitalmars-d mailing list