Skip to content

Commit

Permalink
Merge pull request #15272 from aschwaighofer/swift-4.1-branch-irgen_f…
Browse files Browse the repository at this point in the history
…ix_typelayout_protocol_metatype

[4.1] IRGen: The type layout of a metatype is not the same as the NativeObject.Type
  • Loading branch information
tkremenek authored Mar 15, 2018
2 parents b2e070f + b62adb9 commit e5e5a80
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,10 @@ namespace {
return emitFromValueWitnessTable(IGF.IGM.Context.TheEmptyTupleType);
}
case MetatypeRepresentation::Thick:
if (isa<ExistentialMetatypeType>(type)) {
return emitFromTypeMetadata(type);
}
// Otherwise, this is a metatype that looks like a pointer.
case MetatypeRepresentation::ObjC:
// Thick metatypes look like pointers with spare bits.
return emitFromValueWitnessTable(
Expand Down
18 changes: 18 additions & 0 deletions test/IRGen/type_layout_reference_storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,21 @@ struct ReferenceStorageTypeLayout<T, Native : C, Unknown : AnyObject> {
// CHECK: store i8** getelementptr inbounds (i8*, i8** @_T0[[UNKNOWN]]SgXwWV, i32 9)
weak var uwi: Unknown!
}


public class Base {
var a: UInt32 = 0
}

// CHECK-LABEL: %swift.type* @create_generic_metadata_Derived(%swift.type_pattern*, i8**)
// CHECK-NOT: store {{.*}}getelementptr{{.*}}T0BomWV
// CHECK: call %swift.type* @{{.*}}type_layout_reference_storage1P_pXmTMa()
// CHECK: store {{.*}}getelementptr{{.*}}T0BoWV
// CHECK: ret
public class Derived<T> : Base {
var type : P.Type
var k = C()
init(_ t: P.Type) {
type = t
}
}
29 changes: 29 additions & 0 deletions test/Interpreter/metatype.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test

protocol Bar : class {
}

public class Foo : Bar {
}

public class Base {
final fileprivate(set) var a: UInt32 = 0
}

public class Derived<T> : Base {
final var type : Bar.Type
final var k = Foo()

init(_ t: Bar.Type, _ kl: Foo ) {
type = t
k = kl
}
}

public func dontCrash() {
// CHECK: Derived<Swift.Int>
print(Derived<Int>(Foo.self, Foo()))
}

dontCrash()

0 comments on commit e5e5a80

Please sign in to comment.