Hi @kubamracek ... another little update. I reworked Array to use _ContiguousArrayBuffer as normal but I'm still getting the same compiler error. I used swift-ide-test to decompile my Swift module, just to make sure, and the old AVRArrayBuffer has gone, replaced by the normal array buffer.
So this is the definition of Array in the most recent clone of swift I have...
@frozen
@_eagerMove
public struct Array<Element>: _DestructorSafeContainer {
#if _runtime(_ObjC)
@usableFromInline
internal typealias _Buffer = _ArrayBuffer<Element>
#else
@usableFromInline
internal typealias _Buffer = _ContiguousArrayBuffer<Element>
#endif
@usableFromInline
internal var _buffer: _Buffer
/// Initialization from an existing buffer does not have "array.init"
/// semantics because the caller may retain an alias to buffer.
@inlinable
internal init(_buffer: _Buffer) {
self._buffer = _buffer
}
}
...and the definition I'm working with is...
@frozen
public struct Array<Element>: _DestructorSafeContainer {
#if FORCE_MAIN_SWIFT_ARRAYS
@usableFromInline
internal typealias _Buffer = _ContiguousArrayBuffer<Element>
#else
@usableFromInline
internal typealias _Buffer = _AVRArrayBuffer<Element>
#endif
@usableFromInline
internal var _buffer: _Buffer
@inlinable
internal init(_buffer: _Buffer) {
self._buffer = _buffer
}
}
(With FORCE_MAIN_SWIFT_ARRAYS defined in the builds I'm using.)
This simple program still fails to compile:
let arr = [3]
Given this is leading away from my standard library experiments a bit, I thought it was time to dive into the SIL and debugging the compiler.
The raw SIL...
sil_stage raw
import Builtin
import Swift
import SwiftShims
@_hasStorage @_hasInitialValue let arr: [UInt8] { get }
// arr
sil_global hidden [let] @$s4main3arrSays5UInt8VGvp : $Array<UInt8>
// main
sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @$s4main3arrSays5UInt8VGvp // id: %2
%3 = global_addr @$s4main3arrSays5UInt8VGvp : $*Array<UInt8> // user: %16
%4 = integer_literal $Builtin.Word, 1 // user: %6
// function_ref _allocateUninitializedArray<A>(_:)
%5 = function_ref @$ss27_allocateUninitializedArrayySayxG_BptBwlF : $@convention(thin) <Ļ_0_0> (Builtin.Word) -> (@owned Array<Ļ_0_0>, Builtin.RawPointer) // user: %6
%6 = apply %5<UInt8>(%4) : $@convention(thin) <Ļ_0_0> (Builtin.Word) -> (@owned Array<Ļ_0_0>, Builtin.RawPointer) // user: %7
(%7, %8) = destructure_tuple %6 : $(Array<UInt8>, Builtin.RawPointer) // users: %16, %9, %9
%9 = mark_dependence %8 : $Builtin.RawPointer on %7 : $Array<UInt8> // user: %10
%10 = pointer_to_address %9 : $Builtin.RawPointer to [strict] $*UInt8 // user: %15
%11 = integer_literal $Builtin.IntLiteral, 3 // user: %14
%12 = metatype $@thin UInt8.Type // user: %14
// function_ref UInt8.init(_builtinIntegerLiteral:)
%13 = function_ref @$ss5UInt8V22_builtinIntegerLiteralABBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin UInt8.Type) -> UInt8 // user: %14
%14 = apply %13(%11, %12) : $@convention(method) (Builtin.IntLiteral, @thin UInt8.Type) -> UInt8 // user: %15
store %14 to [trivial] %10 : $*UInt8 // id: %15
store %7 to [init] %3 : $*Array<UInt8> // id: %16
%17 = integer_literal $Builtin.Int32, 0 // user: %18
%18 = struct $Int32 (%17 : $Builtin.Int32) // user: %19
return %18 : $Int32 // id: %19
} // end sil function 'main'
// _allocateUninitializedArray<A>(_:)
sil [serialized] [always_inline] [_semantics "array.uninitialized_intrinsic"] @$ss27_allocateUninitializedArrayySayxG_BptBwlF : $@convention(thin) <Ļ_0_0> (Builtin.Word) -> (@owned Array<Ļ_0_0>, Builtin.RawPointer)
// UInt8.init(_builtinIntegerLiteral:)
sil [transparent] [serialized] @$ss5UInt8V22_builtinIntegerLiteralABBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin UInt8.Type) -> UInt8
// Mappings from '#fileID' to '#filePath':
// 'main/main.swift' => 'main.swift'
Emitted build/main.sil
Canonical SIL cannot be made, the error occurs before then...
main.swift:1:11: error: cannot use metatype of type 'UInt8' in embedded Swift
let arr = [3]
^
in function specialized static Array._allocateUninitialized(_:)
make: *** [build/main.csil] Error 1
Breaking on the diagnostic being emitted in the compiler and looking at the SIL function in question...
e function->dump()
// specialized static Array._allocateUninitialized(_:)
sil shared [always_inline] [_semantics "array.uninitialized"] [ossa] @$sSa22_allocateUninitializedySayxG_SpyxGtSizFZs5UInt8V_Tgm5 : $@convention(thin) (@inout Int) -> (@owned Array<UInt8>, UnsafeMutablePointer<UInt8>) {
// %0 // users: %32, %13, %10
bb0(%0 : $*Int):
%1 = metatype $@thin Array<UInt8>.Type // user: %8
%2 = metatype $@thick UInt8.Type // user: %4
%3 = metatype $@thick UInt8.Type // user: %5
%4 = unchecked_trivial_bit_cast %2 : $@thick UInt8.Type to $Builtin.Word // user: %6
%5 = unchecked_trivial_bit_cast %3 : $@thick UInt8.Type to $Builtin.Word // user: %6
%6 = builtin "cmp_eq_Word"(%4 : $Builtin.Word, %5 : $Builtin.Word) : $Builtin.Int1 // user: %7
cond_br %6, bb1, bb2 // id: %7
bb1: // Preds: bb0
%8 = unchecked_trivial_bit_cast %1 : $@thin Array<UInt8>.Type to $@thin Array<UInt8>.Type // user: %10
// function_ref specialized static Array._allocateUninitialized(_:)
%9 = function_ref @$sSa22_allocateUninitializedySayxG_SpyxGtSizFZs5UInt8V_Tgq5 : $@convention(method) (@inout Int, @thin Array<UInt8>.Type) -> (@owned Array<UInt8>, UnsafeMutablePointer<UInt8>) // user: %10
%10 = apply %9(%0, %8) : $@convention(method) (@inout Int, @thin Array<UInt8>.Type) -> (@owned Array<UInt8>, UnsafeMutablePointer<UInt8>) // user: %11
%11 = unchecked_value_cast %10 : $(Array<UInt8>, UnsafeMutablePointer<UInt8>) to $(Array<UInt8>, UnsafeMutablePointer<UInt8>) // user: %12
br bb3(%11 : $(Array<UInt8>, UnsafeMutablePointer<UInt8>)) // id: %12
bb2: // Preds: bb0
%13 = load [trivial] %0 : $*Int // users: %63, %58, %45, %16
%14 = integer_literal $Builtin.Int8, 0 // user: %50
%15 = integer_literal $Builtin.Int16, 0 // users: %52, %56, %22, %28, %17
%16 = struct_extract %13 : $Int, #Int._value // users: %52, %48, %22, %17
%17 = builtin "cmp_slt_Int16"(%16 : $Builtin.Int16, %15 : $Builtin.Int16) : $Builtin.Int1 // user: %18
cond_br %17, bb4, bb5 // id: %18
// %19 // user: %20
bb3(%19 : @owned $(Array<UInt8>, UnsafeMutablePointer<UInt8>)): // Preds: bb7 bb1
return %19 : $(Array<UInt8>, UnsafeMutablePointer<UInt8>) // id: %20
bb4: // Preds: bb2
br bb6 // id: %21
bb5: // Preds: bb2
%22 = builtin "cmp_eq_Int16"(%16 : $Builtin.Int16, %15 : $Builtin.Int16) : $Builtin.Int1 // user: %23
cond_br %22, bb8, bb9 // id: %23
bb6: // Preds: bb14 bb8 bb4
// function_ref specialized _ContiguousArrayBuffer.init()
%24 = function_ref @$ss22_ContiguousArrayBufferVAByxGycfCs5UInt8V_Tgm5 : $@convention(thin) () -> @owned _ContiguousArrayBuffer<UInt8> // user: %25
%25 = apply %24() : $@convention(thin) () -> @owned _ContiguousArrayBuffer<UInt8> // users: %30, %26, %31
%26 = copy_value %25 : $_ContiguousArrayBuffer<UInt8> // user: %27
%27 = move_value [lexical] %26 : $_ContiguousArrayBuffer<UInt8> // user: %33
%28 = struct $Int (%15 : $Builtin.Int16) // users: %30, %32
// function_ref specialized _ContiguousArrayBuffer.count.setter
%29 = function_ref @$ss22_ContiguousArrayBufferV5countSivss5UInt8V_Tg5 : $@convention(method) (Int, @guaranteed _ContiguousArrayBuffer<UInt8>) -> () // user: %30
%30 = apply %29(%28, %25) : $@convention(method) (Int, @guaranteed _ContiguousArrayBuffer<UInt8>) -> ()
destroy_value %25 : $_ContiguousArrayBuffer<UInt8> // id: %31
store %28 to [trivial] %0 : $*Int // id: %32
br bb7(%27 : $_ContiguousArrayBuffer<UInt8>) // id: %33
// %34 // user: %35
bb7(%34 : @owned $_ContiguousArrayBuffer<UInt8>): // Preds: bb13 bb6
%35 = struct $Array<UInt8> (%34 : $_ContiguousArrayBuffer<UInt8>) // user: %36
%36 = move_value [lexical] [var_decl] %35 : $Array<UInt8> // users: %42, %37
%37 = copy_value %36 : $Array<UInt8> // user: %38
%38 = destructure_struct %37 : $Array<UInt8> // users: %40, %41
// function_ref specialized _ContiguousArrayBuffer.firstElementAddress.getter
%39 = function_ref @$ss22_ContiguousArrayBufferV19firstElementAddressSpyxGvgs5UInt8V_Tg5 : $@convention(method) (@guaranteed _ContiguousArrayBuffer<UInt8>) -> UnsafeMutablePointer<UInt8> // user: %40
%40 = apply %39(%38) : $@convention(method) (@guaranteed _ContiguousArrayBuffer<UInt8>) -> UnsafeMutablePointer<UInt8> // user: %42
destroy_value %38 : $_ContiguousArrayBuffer<UInt8> // id: %41
%42 = tuple (%36 : $Array<UInt8>, %40 : $UnsafeMutablePointer<UInt8>) // user: %43
br bb3(%42 : $(Array<UInt8>, UnsafeMutablePointer<UInt8>)) // id: %43
bb8: // Preds: bb5
br bb6 // id: %44
bb9: // Preds: bb5
%45 = destructure_struct %13 : $Int // user: %46
%46 = builtin "truncOrBitCast_Int16_Int8"(%45 : $Builtin.Int16) : $Builtin.Int8 // users: %50, %47
%47 = builtin "zextOrBitCast_Int8_Int16"(%46 : $Builtin.Int8) : $Builtin.Int16 // user: %48
%48 = builtin "cmp_eq_Int16"(%47 : $Builtin.Int16, %16 : $Builtin.Int16) : $Builtin.Int1 // user: %49
cond_br %48, bb10, bb11 // id: %49
bb10: // Preds: bb9
%50 = builtin "cmp_ult_Int8"(%14 : $Builtin.Int8, %46 : $Builtin.Int8) : $Builtin.Int1 // user: %51
br bb12(%50 : $Builtin.Int1) // id: %51
bb11: // Preds: bb9
%52 = builtin "cmp_slt_Int16"(%15 : $Builtin.Int16, %16 : $Builtin.Int16) : $Builtin.Int1 // user: %53
br bb12(%52 : $Builtin.Int1) // id: %53
// %54 // user: %55
bb12(%54 : $Builtin.Int1): // Preds: bb11 bb10
cond_br %54, bb13, bb14 // id: %55
bb13: // Preds: bb12
%56 = struct $Int (%15 : $Builtin.Int16) // user: %58
// function_ref specialized _ContiguousArrayBuffer.init(_uninitializedCount:minimumCapacity:)
%57 = function_ref @$ss22_ContiguousArrayBufferV19_uninitializedCount15minimumCapacityAByxGSi_SitcfCs5UInt8V_Tgm5 : $@convention(thin) (Int, Int) -> @owned _ContiguousArrayBuffer<UInt8> // user: %58
%58 = apply %57(%56, %13) : $@convention(thin) (Int, Int) -> @owned _ContiguousArrayBuffer<UInt8> // user: %59
%59 = move_value [lexical] [var_decl] %58 : $_ContiguousArrayBuffer<UInt8> // users: %63, %64, %60
%60 = copy_value %59 : $_ContiguousArrayBuffer<UInt8> // user: %61
%61 = move_value [lexical] %60 : $_ContiguousArrayBuffer<UInt8> // user: %65
// function_ref specialized _ContiguousArrayBuffer.count.setter
%62 = function_ref @$ss22_ContiguousArrayBufferV5countSivss5UInt8V_Tg5 : $@convention(method) (Int, @guaranteed _ContiguousArrayBuffer<UInt8>) -> () // user: %63
%63 = apply %62(%13, %59) : $@convention(method) (Int, @guaranteed _ContiguousArrayBuffer<UInt8>) -> ()
destroy_value %59 : $_ContiguousArrayBuffer<UInt8> // id: %64
br bb7(%61 : $_ContiguousArrayBuffer<UInt8>) // id: %65
bb14: // Preds: bb12
br bb6 // id: %66
} // end sil function '$sSa22_allocateUninitializedySayxG_SpyxGtSizFZs5UInt8V_Tgm5'
...the SIL instruction that is causing the diagnostic to emit is...
%2 = metatype $@thick UInt8.Type
...is this making any sense to you why it's emitting a diagnostic for me, but I guess works in normal embedded swift?
Any help greatly appreciated.
Carl