What should happen when the assert message expression throws?

kdevel kdevel at vogtner.de
Fri Nov 25 16:48:39 UTC 2022


On Friday, 25 November 2022 at 15:48:49 UTC, Ali Çehreli wrote:
> On 11/25/22 07:33, kdevel wrote:
> > On Friday, 25 November 2022 at 14:38:57 UTC, Ali Çehreli
> wrote:
> >> On 11/25/22 05:32, kdevel wrote:
> >
> >> If that assertion fails, the program is in an invalid state.
> >
> > [reordering ...]
> >
> >> > int main ()
> >> > {
> >> >     int a = 1;
> >> >     try
> >> >        assert (a == 0);
> >> >     catch (Throwable t)
> >> >        {}
> >> >     return 0;
> >> > }
> >> >
> >> > Which line makes the program enter the "invalid state"?
>
> I am not an expert here; just philosophizing.

If I tell you "A program is running on a machine. There is an int 
variable a in memory at adress 0x47110816. The variable has the 
value -4." you probably can explain to me how and where the 
negative sign is represented in the machine.

I would like to know if there is a representation of that ominous 
"invalid state" in terms of bits and bytes. Well, it doesn't have 
to. But if we are talking about "invalid state" we should be able 
to explain what we mean when we use that group of words. I don't 
use the term because I do not intuitively know what it means and 
the D docs do not define the term either.

> [reordering...] :)
>
> >> Although that looks like a bug in the programmer's mind, I 
> >> can still
> >> imagine the programmer expected some code perhaps running in 
> >> a thread
> >> started e.g. in a 'static this' block was expected to alter 
> >> the stack.
> >
> > The fabricated nine lines of code above is all I have. No 
> > static this
> > block. It is meant as it is.
> >
>
> Your example proves that we all assume that the assertion 
> condition is correct.

If you allow I would like to discuss the interpretation of the 
code a bit more thoroughly.

> I don't think the example above has a correct condition; so 
> let's focus on correct ones.

The condition is false. That is what I want to discuss. My 
question is: What does that mean? Does it mean more than writing 
a comment

    // I solemnly swear a is zero.

?

> > Is it in the "invalid state" right after the condition has 
> > been
> > evaluated or not before the AssertError has been thrown? What 
> > if the
> > program is compiled with -release? Is it still in "invalid 
> > state"?
>
> I think assert is just a tool that exposes an invalid state.

In your last post you wrote "If that assertion fails, the program 
is in an invalid state." The assertion in my program fails. 
According to the common inference rules the program must be in an 
invalid state and according to your last statement this invalid 
state is only exposed, i.e. the program was already in "invalid 
state" before the assert statment.

Thus my question: What constitutes the invalid state in my 
example program? The program is syntactically valid, no UB, 
nothing else.

> The invalid state may have been reached long ago.

In my fabricated example program: Where and by what event went 
the program into "invalid state"?




More information about the Digitalmars-d mailing list