-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-7497] [stdlib] Fix KeyPaths hashing #17467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Removes shadowed `hasher` argument in component hashing.
Include type metadata pointer in hashing.
The metatype object for something that can't be subclassed (or otherwise store interesting information about the type) is zero-sized. The (meta)type class Super {}
class Sub1: Super {}
class Sub2: Super {} The type As I guess you might already know, (Hello and welcome!) |
ah, I see. I misunderstood it. I thought |
stdlib/public/core/KeyPath.swift
Outdated
@@ -59,6 +59,7 @@ public class AnyKeyPath: Hashable, _AppendKeyPath { | |||
/// of this instance. | |||
@_effects(releasenone) | |||
final public func hash(into hasher: inout Hasher) { | |||
hasher.combine(unsafeBitCast(type(of: self) as Any.Type, to: Int.self)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be written ObjectIdentifier(type(of: self)).hash(into: &hasher)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know, thanks 👌
Nice catch! |
Uses ObjectIdentifier to extract metatype pointer, instead of unsafe pointer casting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting this!
@swift-ci please smoke test and merge |
1 similar comment
@swift-ci please smoke test and merge |
@lorentey my pleasure 😁 I guess this could be merged into 4.2 too, right? |
@dmcyk Yes, we should get it into 4.2! I'll cherry pick it after it lands on master. |
hasher
argument in component hashing, so the component type/offset wasn't taken into accountSo I thought it'd make sense to include the type metadata pointer while hashing. As far as I understand it, the metadata pointer doesn't change during program execution.
Regarding code:
I'm not sure if
type(of: self) as Any.Type
cast is necessary.Quickly running REPL trying to do
unsafeBitCast(type(of: Int(0)), to: Int.self)
crashes, casting thetype(of:)
result toAny.Type
helps though.Is this caused by value vs ref type difference? Doing so with a class object works.
I was looking at
_getTypeName
and other core functions, but I couldn't really find a place whereAny.Type
is guaranteed to beconst Metadata*
and howAny.Type
differs from e.g.Int.Type
.Is it somehow implemented at SIL level? Would be great if someone could maybe give me a tip where to look for information/code regarding this.
Resolves SR-7497.
P.S. That's my first attempt at contribution to Swift. Hello everyone 👋