assert semantic change proposal
Matthias Bentrup via Digitalmars-d
digitalmars-d at puremagic.com
Wed Aug 6 08:02:09 PDT 2014
On Wednesday, 6 August 2014 at 13:31:54 UTC, Ola Fosheim Grøstad
wrote:
> On Wednesday, 6 August 2014 at 12:41:16 UTC, Artur Skawina via
> Digitalmars-d wrote:
>> The compiler can /assume/ that the condition never fails.
>> Hence, it does
>> not need to generate any code to check that the assumption is
>> valid.
>
> Exactly, worse example using a coroutine:
>
> «
> label:
> …
> while(running) { // forevah!
> … yield …
> }
> …
> assume(anything local not assigned below label) // reachable,
> but never executed
> »
>
> is equivalent to:
>
> «
> assume(anything local not assigned below label) // optimize
> based on this
> label:
> …
> while(running) {
> … yield …
> }
> »
>
> Woops?
But even if there is no explicit assert()/assume() given by the
developer, I guess the optimizer is free to insert assumes that
are provably correct, e.g.
while(running) {
...don't assign to running, don't break...
}
is equivalent to
while(running) {
...don't assign to running, don't break...
}
assume(!running);
is equivalent to
assume(!running);
while(running) {
...don't assign to running, don't break...
}
is equivalent to
assume(!running);
So I take the compiler is allowed to throw away code without any
asserts already ?
More information about the Digitalmars-d
mailing list