Automatic invariant generation

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 7 07:17:34 PDT 2017


On Friday, July 7, 2017 1:38:13 PM MDT Stefan Koch via Digitalmars-d 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.
> >
> > -Steve
>
> It was added because someone VIP demanded it I guess.
> you can see the assert being added using -vcg-ast ;)

What does it even do? I don't see how it makes any sense for _anything_ to
have an invariant if it's not explicitly declared. And honestly, I'm of the
opinion that invariants with structs are borderline useless, because they're
run even before opAssign, meaning that if you ever need to use = void; or
use emplace, then you're screwed if you have an invariant, because it's
bound to fail due to the object not having been initialized previously.
Unfortunately, I couldn't get Walter to agree that it made sense to not call
the invariant prior to opAssign being called - which is why SysTime no
longer has an invariant (it was blowing up in people's code due to emplace
IIRC). As such, it seems that much more stupid for structs to get any kind
fo invariant automatically.

- Jonathan M Davis



More information about the Digitalmars-d mailing list