3 ms·
> It is a pretty clear violation of the type rules on any sane language. Is it? What is "sane"? This is the TXR Lisp interactive listener of TXR 141. Use
by kazinator 11y ago
> It is a pretty clear violation of the type rules on any sane language.
Is it? What is "sane"?
This is the TXR Lisp interactive listener of TXR 141.
Use the :quit command or type Ctrl-D on empty line to exit.
1> (defstruct foo nil x)
#<struct-type foo>
2> (defstruct bar nil x)
#<struct-type bar>
3> (defun access-x (obj) obj.x)
access-x
4> (access-x (new foo x 42))
42
5> (access-x (new bar x 42))
42
This is so insane that it will work even if x is at different offsets in foo and bar.
We can whip up a similar thing statically with, say, C++ templates.
Note that C does explicitly allow this:
{
struct foo { int x; char other; };
struct bar { int x; double other; };
union combined { struct foo f; struct bar b; } c;
c.f.x = 42;
return c.b.x;
}
If structs which have some common sequence of leading members (same names and types) are combined via a union, then those common members can be accessed through any member of the that union.
- lmm 11y ago> This is so insane that it will work even if x is at different offsets in foo and bar. You're accessing foo.x or bar.x. You're just doing it in a generic way. That's fine and should work, in any language. > If structs which have some common sequence of leading members (same names and types) are combined via a union, then those common members can be accessed through any member of the that union. Unions have defined semantics in all cases, no? union combined { float f; int i; } c; c.f = 1.0f return c.i; will return some (possibly implementation-defined) value rather than being undefined behaviour, because that's the whole point of a union - access to different fields of a union shouldn't be an aliasing violation.
- kazinator 11y agoUnions have well-defined semantics in that special case, spelled out in ISO C. I have a copy of C99 handy, where it is paragraph 5 in 6.5.2.3 (Structure and union members): "One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the complete type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members." The part about "anywhere that a declaration of the complete type of the union is visible" isn't superfluous verbiage: it means that the aliasing doesn't have to go through the union; but it has to be in a region of code where the union is declared. So for instance, it is no longer well-defined if you do this: // external fun has no idea about the union external_fun(&c.f); // exernal_fun is this: void external_fun(struct foo *pf) { struct bar *pb = (struct bar *) pf; // access through pb } Here, c.f is a member of a union where and has a "common initial part" with c.b of the same union object. But this is not known in external_fun which has no declaration of the union and so the translation of external_fun doesn't have to honor this aliasing if there is no completely declared union type in scope which aliases struct foo and struct bar. (See, C goes out of its way to make it clear that aliasing between different struct types is bad: even when an exception is made, there is weasel wording to carefully contain it!)
- gpderetta 11y agoThe 'visible union' rule is generally understood as meaning that the union type must be part of the access expression. Any other interpretation that only requires the union to be in scope would pretty much render TBAA moot for any non trivial program. And yes, this rule is part of the reason the current TBAA rules are such a mess. There is an outstanding C issue to clarify the wording here but the C commitee is severely under capacity. There are proposed C++ wordings that clarify this rule in addition to generally clarify lifetimes and TBAA (while still being far from settling the issue, mind you). These wordings also introduce std::launder(), a function that bless some of the type punning tricks by explicitly informing the compiler about what's going on.
- kazinator 11y ago> Any other interpretation that only requires the union to be in scope would pretty much render TBAA moot for any non trivial program. An interpretation of the 'visible union' rule as being in scope is the only interpretation which doesn't render that text completely unnecessary. Of course if the access is taking place through the union, the union must be completely declared! When would you be able to access a union member as u.x, or pu->x, when the type of u or *pu is incomplete?
- choosername 11y agois this about strict or static typing, which lisp doesn't use?
- kazinator 11y agoIt is and isn't. There is only one access-x function, and it receives obj, which is de facto a pointer into the heap (a boxed object). So it's a case of aliasing: "access slot x in this thing, regardless of its specific type". It is analogous to what the C code is doing when it does type punning. It is tangentially relevant because C implementations of dynamic languages sometimes bend the rules to skirt with aliasing.