Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Describe explicitly how your example will trigger illegal behavior? Happy to discuss the behavior of the compiled asm if you'd like to be that pendantic?

Through calling @fieldParentPtr with a field_ptr not pointing at a field of the given field name in the result type, when the result type has an ill-defined memory layout. I'm essentially just paraphrasing the documentation, which I've already quoted.

Generated code is irrelevant to this end. Illegal behavior can coincidentally result in code that works as intended in practice as a side effect of the implementation. Similarly you may expect a signed integer to wrap around as it overflows in C because of the implementation of the compiler and the code it generates, but it's still undefined behavior.

> No? Struts have a defined layout, they're not guaranteed between compilations, but they don't change within a compilation unit.

This is not the sense in which the Zig language reference uses the term well-defined memory layout. Bare structs, error unions, slices and optionals don't have a well-defined memory layout in the sense the Zig language reference uses it.

> And the failure mode is the compiler will decide to delete your OS?

The failure mode is irrelevant. Unchecked illegal behavior is unchecked illegal behavior regardless of the failure mode. You are moving goalposts now.

> I thought you were saying that it's guaranteed to be incorrect. In fact, I think that's what you said in your reply to me. Mostly because you used the word 'mearly'.

The example I posted is guaranteed to be incorrect, which demonstrates that the use of @fieldParentPtr can result in unchecked illegal behavior, hence not type safe nor giving you an error when you write invalid code. Regarding the use of "merely", you should adopt the habit of reading complete sentences before you draw conclusions about what they imply.

> The way that I understood what you meant was, the example you provided was guaranteed wrong.

The example is guaranteed to cause unchecked illegal behavior.

> I'm sorry if I was unclear, I was trying to discuss it in good faith. :(

If you want to argue in good faith, you can start with a good faith reading of the language reference.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: