# Handling the new "Forming 'UnsafeRawPointer'..." warning

**URL:** <https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523>\
**Category:** Using Swift\
**Created:** [June 12, 2023, 1:31pm UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523 "2023-06-12T13:31:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [June 12, 2023, 1:31pm UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523/1 "2023-06-12T13:31:39Z")

</div>

Swift 5.9 adds [a new warning](https://github.com/apple/swift/issues/64927):

> Forming 'UnsafeRawPointer' to a variable of type 'Type'; this is likely incorrect because 'Type' may contain an object reference.

This largely rears its head when bridging to C or Obj-C APIs. However, it's rather unclear what the intended resolution for the warning should be without fundamentally changing what the code does (and the PR doesn't help). For example, in Obj-C, it's typical to use a string as an associated object key.

```swift
extension ObjCType {
  private struct Keys { static var someKey = "someKey" }

  var isSomeBool: Bool {
    get { objc_getAssociatedObject(self, &Keys.someKey) as? Bool ?? false }
    set {
      objc_setAssociatedObject(self, &Keys.someKey, newValue, .OBJC_ASSOCIATION_COPY)
    }
  }
}

```

In this case it's possible to change the key type to something simple, like `Bool`, but that doesn't help if you're operating within an existing codebase and need compatibility with old code or SDKs. How would I keep compatibility here?

There are also examples where the underlying reference is actually important to the functionality of the code. For example, this `OrderedSet` comparison optimization from the Composable Architecture:

```swift
@inlinable
func areOrderedSetsDuplicates<T>(_ lhs: OrderedSet<T>, _ rhs: OrderedSet<T>) -> Bool {
  var lhs = lhs
  var rhs = rhs
  return memcmp(&lhs, &rhs, MemoryLayout<OrderedSet<T>>.size) == 0 || lhs == rhs
}

```

In this case, comparing the whole value, including references, is entirely intentional. How can we silence the warning while still using `memcmp`?

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [June 12, 2023, 2:33pm UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523/2 "2023-06-12T14:33:21Z")

</div>

For the `memcmp` implementation, the [common workarounds](https://developer.apple.com/wwdc21/10132) section of the proposal suggests `withUnsafePointer(to:)`.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [June 12, 2023, 2:34pm UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523/3 "2023-06-12T14:34:44Z")

</div>

I'm not sure what you meant to link to there, but it links to a concurrency WWDC talk, which doesn't seem right.

As for the work around, I assume you mean that nesting `withUnsafePointer(to:)` would work?

```swift
@inlinable
func areOrderedSetsDuplicates<T>(_ lhs: OrderedSet<T>, _ rhs: OrderedSet<T>) -> Bool {
  withUnsafePointer(to: lhs) { lhs in
    withUnsafePointer(to: rhs) { rhs in
      var lhs = lhs
      var rhs = rhs
      return memcmp(&lhs, &rhs, MemoryLayout<OrderedSet<T>>.size) == 0 || lhs == rhs
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [June 12, 2023, 3:11pm UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523/4 "2023-06-12T15:11:35Z")

</div>

> [@Jon\_Shier](#):
>
> I'm not sure what you meant to link to there, but it links to a concurrency WWDC talk, which doesn't seem right.

Good call! I meant to link [here](https://github.com/atrick/swift-evolution/blob/diagnose-implicit-raw-bitwise/proposals/nnnn-implicit-raw-bitwise-conversion.md#workarounds-for-common-cases).

> [@Jon\_Shier](#):
>
> As for the work around, I assume you mean that nesting `withUnsafePointer(to:)` would work?

Yes, though not exactly like that, as you're now comparing the byte values of the pointers. Drop the second pair of `&` and the inner shadow variables:

```swift
@inlinable
func areOrderedSetsDuplicates<T>(_ lhs: OrderedSet<T>, _ rhs: OrderedSet<T>) -> Bool {
  withUnsafePointer(to: lhs) { lhs in
    withUnsafePointer(to: rhs) { rhs in
      return memcmp(lhs, rhs, MemoryLayout<OrderedSet<T>>.size) == 0 || lhs == rhs
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [June 12, 2023, 3:12pm UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523/5 "2023-06-12T15:12:41Z")

</div>

While I'm here I'll also note that the `|| lhs == rhs` comparison in this context no longer works and has to get hoisted out:

```swift
@inlinable
func areOrderedSetsDuplicates<T>(_ lhs: OrderedSet<T>, _ rhs: OrderedSet<T>) -> Bool {
  withUnsafePointer(to: lhs) { lhs in
    withUnsafePointer(to: rhs) { rhs in
      return memcmp(lhs, rhs, MemoryLayout<OrderedSet<T>>.size) == 0
    }
  } || lhs == rhs
}

```

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [June 12, 2023, 3:16pm UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523/6 "2023-06-12T15:16:24Z")

</div>

Thanks! And I see the proposal has an explicit workaround for the associated object case as well, so both questions are answered.

```swift
class Container {
  static var key = "key"

  func getObject() -> Any? {
    withUnsafePointer(to: Container.key) {
      return objc_getAssociatedObject(self, $0)
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [June 13, 2023, 8:06am UTC](https://forums.swift.org/t/handling-the-new-forming-unsaferawpointer-warning/65523/7 "2023-06-13T08:06:10Z")

</div>

Given the ongoing travails associated with `&`, I always wondered why folks didn’t just switch to `malloc`:

```swift
class Container {
    static let key = malloc(1)!
    
    func getObject() -> Any? {
        return objc_getAssociatedObject(self, Container.key)
    }
}

```

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple
