Skip to content

IRGen: Cast the type of class pointer to the storage type #5449

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
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
5 changes: 5 additions & 0 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ OwnedAddress irgen::projectPhysicalClassMemberAddress(IRGenFunction &IGF,
case FieldAccess::ConstantDirect: {
Address baseAddr(base, baseClassTI.getHeapAlignment(IGF.IGM, baseType));
auto &element = baseClassTI.getElements(IGF.IGM, baseType)[fieldIndex];
// We might run into a case where the type of baseAddr is an opaque pointer.
if (baseAddr->getType() != baseClassTI.getStorageType()) {
baseAddr = IGF.Builder.CreateBitCast(baseAddr,
baseClassTI.getStorageType());
}
Address memberAddr = element.project(IGF, baseAddr, None);
// We may need to bitcast the address if the field is of a generic type.
if (memberAddr.getType()->getElementType() != fieldTI.getStorageType())
Expand Down
12 changes: 12 additions & 0 deletions test/IRGen/class_bounded_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,15 @@ func class_bounded_metatype<T: SomeSwiftClass>(_ t : T) {
class WeakRef<T: AnyObject> {
weak var value: T?
}

class A<T> {
required init() {}
}

class M<T, S: A<T>> {
private var s: S
init() {
// Don't crash generating the reference to 's'.
s = S.init()
}
}