How to free a type erased field?

I actually did try (for a different thing, not this) compating two different poitners to the same global const declaration and I could not get them to be equal. But I spent like two minutes so I probably missed somehting.

1 Like

That should work, the bug was elsewhere on that one (like you said, two minutes, no big deal).

I’m thinking about esoteric edge cases like merging two functions which end up with the same IR, but, it was more of an abstract question about what the language should allow than anything practical. I’m fairly sure, but not certain, that a function which doesn’t have any comptime specialization will compare equal by pointer as a matter of practice.

A single case doesn’t prove the rule but:

fn sillyFn(i: usize) !?usize {
    if (i >= 5) return null;
    return i + 1;
}

test "fn pointer equality" {
    try expectEqual(&sillyFn, &sillyFn);
    try expect(&sillyFn == &sillyFn);
}

In C this is something one can rely on, and plenty of code in the wild does. Zig is not (yet) specified so there are a lot of question marks about esoteric stuff like this.

By the way, what you can’t rely on, is that pointers to two different ā€˜global’ const values will compare as different. Zig interns strings, so if they have different identifiers / namespaces, but the same value, their pointers will be equal. Another thing where it is not clear that the language will guarantee this, but it does happen to be the case.