Skip to content

IRGen: The type layout of a metatype is not the same as the NativeObj… #14891

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,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
17 changes: 17 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,20 @@ struct ReferenceStorageTypeLayout<T, Native : C, Unknown : AnyObject> {
// CHECK: store i8** getelementptr inbounds (i8*, i8** @"$S[[UNKNOWN]]SgXwWV", i32 9)
weak var uwi: Unknown!
}


public class Base {
var a: UInt32 = 0
}
// CHECK-LABEL: %swift.type* @{{.*}}7DerivedCMi"(%swift.type_descriptor*, i8**)
// CHECK-NOT: store {{.*}}getelementptr{{.*}}SBomWV
// CHECK: call %swift.type* @"$S29type_layout_reference_storage1P_pXmTMa"()
// CHECK: store {{.*}}getelementptr{{.*}}SBoWV
// 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()