Automatic invariant generation

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 7 07:26:57 PDT 2017


On 7/7/17 9:38 AM, Stefan Koch wrote:
> On Friday, 7 July 2017 at 13:34:20 UTC, Steven Schveighoffer wrote:
>> On 7/7/17 4:21 AM, Nicholas Wilson wrote:
>>> The compiler seems to inset an `assert(this !is null, "null this");` 
>>> into my struct.
>>> which is for all intents and purposes.
>>> struct Foo {
>>>      Bar b;
>>> }
>>>
>>> struct Bar {
>>>      void* ptr;
>>> }
>>
>> What? When is this invariant called? I've never heard of a hidden 
>> invariant being added to structs, structs are supposed to be free of 
>> such things.
>>
>> I would call such a thing a bug.
>>
> 
> It was added because someone VIP demanded it I guess.

Nope, it's really REALLY old (version 0.167):

https://github.com/dlang/dmd/commit/43a336d81c38817ae545becf02b7459836025c60

> you can see the assert being added using -vcg-ast ;)
> 

Hm... it doesn't look like an invariant, it just looks like an inserted 
assert inside every function.

An incorrect assert, IMO:

struct Foo
{
     int x;
     void foo() {}
}

void main()
{
    Foo *foo;
    foo.foo(); // shouldn't assert, wouldn't crash anyway.
}

And since when did we care about null pointers causing segfaults?

Can anyone vouch for this feature?

-Steve


More information about the Digitalmars-d mailing list