Unexpected (and confusing) compiler diagnostic

I thought the following should work but it doesn't. I can't really see why not, what am I missing?

Also, where did the UInt8 come from?

Cheers,

xcrun swift repl
Welcome to Apple Swift version 6.3.3 (swiftlang-6.3.3.1.3 clang-2100.1.1.101).
Type :help for assistance.
  1> import Foundation
  2> func f<C: ContiguousBytes>(data: C) { data.withUnsafeBytes { print($0.baseAddress) } }
  3> var a = [Int]()
a: [Int] = 0 values
  4> f(data: a)
     ˄˜˜˜˜˜˜˜˜˜
     ╰─ error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
  4> f(data: a[...])
     ˄
     ╰─ error: global function 'f(data:)' requires the types 'Int' and 'UInt8' be equivalent
note: Foundation.ArraySlice:2:11: requirement from conditional conformance of 'Array<Int>.SubSequence' (aka 'ArraySlice<Int>') to 'ContiguousBytes'
extension ArraySlice : ContiguousBytes where Element == UInt8 {
          ^

note: Foundation.ArraySlice:2:11: requirement from conditional conformance of 'Array<Int>.SubSequence' (aka 'ArraySlice<Int>') to 'ContiguousBytes'
extension ArraySlice : ContiguousBytes where Element == UInt8 {
          ^

It also occurred to me to try this which works:

  6> var d = [UInt8]()
d: [UInt8] = 0 values
  7> f(data: d)
Optional(0x00000001f22c5c98)

But why? The documentation doesn't seem to indicate the conformance to ContiguousBytes is conditional?

I found out that this "works" but need to experiment to see if the difference (the closure gets an UnsafeBufferPointer vs UnsafeRawBufferPointer is significant.

If its worth mentioning I want to use this to move data from arrays to metal buffers for strictly bitwise copyable stuff where Array<Element>.withUnsafeBytes works.

  1> import Accelerate
  2> import Foundation
  3> func f<B: AccelerateBuffer>(data: B) {data.withUnsafeBufferPointer { print($0.baseAddress) }}
  4> var a = [Int64]()
a: [Int64] = 0 values
  5> f(data: a)
Optional(0x00000001f22c5c98)
  6> f(data: a[...])
Optional(0x00000001f22c5c98)

failed to produce diagnostic for expression; please submit a bug report

You should file a bug report about this (especially since you’ve narrowed it down to a really simple reproducer!)

That may be a bug in DocC. The conformance is conditional on Element == UInt8:

1 Like

I wonder if it's a DocC bug when the conformance spans across modules. Looking at the "Conforms To" section of Array | Apple Developer Documentation , it only lists the conditions of the conformance for protocols also defined in the stdlib. Protocols outside of the stdlib don't list any conditions, but ContiguousBytes, DataProtocol, and the CodableWithConfiguration protocols from Foundation all have conditions as well as the Attachable conformance from swift-testing at a minimum (just spot checking a few)

Thanks for the suggestions, filed feedback FB24417927

And FB24417976 for the documentation bug on Array's conformance to ContiguousBytes.

Cheers,

2 Likes