Skip to content

Conversation

@SeanTUT
Copy link
Contributor

@SeanTUT SeanTUT commented Oct 24, 2025

Fix several errors in hasUniqueRepresentation, both false negatives and false positives. Additionally, improve documentation surrounding the function, and split its ever-growing unit test into multiple seperate tests.

  • False positives resolved
    • Error sets when the backing int of the error set does not have a unique representation
    • Enums tagged by non-unique types
    • Function types
    • Pointers to comptime-only types
  • False negatives resolved
    • Packed unions
    • Vectors on targets where the scalar types are bitpacked

While fixing this, I ran into several notable edge cases, so I would recommend any reviewers read the comments I left in hasUniqueRepresentation

One of the false positives in question was pointers to comptime-only types. Their representation is not defined, but to solve this, it was necessary to introduce a function to determine whether a type is comptime-only. Since this is a widely-applicable function, I made it public in meta.isComptimeOnly.
^ This has been rolled back, as determining whether a type is comptime-known takes up a massive of eval branch quota such that building any sizable project, including the compiler or even the build system, is impossible when checking this in hasUniqueRepresentation

Some rules of thumb for how hasUniqueRepresentation works now:

  • False negatives can be returned, but never false positives
  • Comptime-only types always return false
  • Types with a well-defined layout never return a false negative or a
    false positive
  • Types with no well-defined layout may return true if the type
    provably has a unique representation, but may also return a false
    negative

PS: While working on this, I ran into some bugs in std.hash.autoHash which ended up incorrectly hashing slices, optional slices, and other types which should not be able to be hashed without specifying a strategy. To resolve this, I ended up refactoring std/hash/auto_hash.zig and fixing some uses of the API which should have been compile errors.

PPS: Also while working on this, I ran into a stale reference bug in std.zig.resolveTargetQuery that was causing the CI and behavior tests to fail. To fix this, an additional allocator parameter had to be added to the function. Then, I had to correct every reference of the function within the codebase, hence the all-over-the-place changes.

Fix several errors in hasUniqueRepresentation, both false negatives and
false positives. Additionally, improve documentation surrounding the
function, and split its ever-growing unit test into multiple seperate tests.

One of the false positives in question was pointers to comptime-only
types. Their representation is not defined, but to solve this, it was
necessary to introduce a function to determine whether a type is
comptime-only. Since this is a widely-applicable function, I made it
public in meta.isComptimeOnly.

Some rules of thumb for how hasUniqueRepresentation works now:
  - False negatives can be returned, but never false positives
  - Comptime-only types always return false
  - Types with a well-defined layout never return a false negative or a
    false positive
  - Types with no well-defined layout may return true if the type
    provably has a unique representation, but may also return a false
    negative
@SeanTUT
Copy link
Contributor Author

SeanTUT commented Oct 24, 2025

Ah, I stepped away from my computer to do something, but in a few minutes I will fix those checks and add tests for what failed

SeanTUT and others added 2 commits October 25, 2025 00:28
Remove some debug code left in the hasUniqueRepresentation
implementation and fix auto_hash's incorrect usage of
hasUniqueRepresentation. (While we're at it, we also gave autoHash a new
coat of paint and cleaned it up a little)
@SeanTUT
Copy link
Contributor Author

SeanTUT commented Oct 25, 2025

Unfortunately, it's looking like determining if a type is comptime-only, and by extension determining whether a pointer hasUniqueRepresentation, take unseemly amount of eval branch quota. It looks like for now, we might just have to live with a false positive for pointers to comptime-only types. Language support here seems like it would be ideal.

isComptimeOnly took up too much eval branch quota to be practical, and
some of my changes to auto_hash.zig were admittedly overzealous and out
of scope for this branch.
While technically correct, it seems like this is causing issues with
hash maps, so it has to be rolled back, at least for now. Goodnight
sweet prince :(
@SeanTUT
Copy link
Contributor Author

SeanTUT commented Oct 26, 2025

I've determined that the build failures come from an incorrect usage of autoHash in src/Builtin.zig, which hashes SemanticVersions (including their build/pre) by value. This exposes some problems in std/hash/auto_hash.zig which necessitate some work on that, and in addition, some functions to hash components of std.Target

@SeanTUT
Copy link
Contributor Author

SeanTUT commented Oct 26, 2025

Looks like the above bug actually stems from an alarming stale pointer reference where std.zig.resolveTargetQuery returns a slice of a stack buffer used for the pre and build fields of a SemanticVersion. Adding to the fix!

Squashed some bugs and cleaned things up in auto_hash and
hasUniqueRepresentation. Additionally, fixed a stale reference bug in
std.zig.resolveTargetQuery. This fix required adding an additional
allocator parameter to resolveTargetQuery.
@alexrp
Copy link
Member

alexrp commented Oct 27, 2025

Looks like the above bug actually stems from an alarming stale pointer reference where std.zig.resolveTargetQuery returns a slice of a stack buffer used for the pre and build fields of a SemanticVersion. Adding to the fix!

Would you mind splitting this into a separate PR? Sounds like a fix we should land ASAP.

@SeanTUT
Copy link
Contributor Author

SeanTUT commented Oct 27, 2025

For the time being, I will be closing this PR, as the tests have proven more difficult than expected, and I will likely be busy for the foreseeable future

@SeanTUT SeanTUT closed this Oct 27, 2025
@SeanTUT
Copy link
Contributor Author

SeanTUT commented Oct 27, 2025

Additionally, there is deeper surgery to be done on related components such as auto_hash and resolveTargetQuery which are out of scope for a single PR anyways

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants