Skip to content

[3.0] Fixes for generic ObjC classes conforming to Swift protocols. #4356

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
4 changes: 2 additions & 2 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ getTypeEntityInfo(IRGenModule &IGM, CanType conformingType) {

auto nom = conformingType->getAnyNominal();
auto clas = dyn_cast<ClassDecl>(nom);
if (nom->isGenericContext() ||
if ((nom->isGenericContext() && (!clas || !clas->usesObjCGenericsModel())) ||
(clas && doesClassMetadataRequireDynamicInitialization(IGM, clas))) {
// Conformances for generics and concrete subclasses of generics
// are represented by referencing the nominal type descriptor.
Expand Down Expand Up @@ -2152,7 +2152,7 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
LinkEntity::forProtocolDescriptor(conformance->getProtocol()),
getPointerAlignment(), ProtocolDescriptorStructTy);
auto typeEntity = getTypeEntityInfo(*this,
conformance->getType()->getCanonicalType());
conformance->getType()->getCanonicalType());
auto flags = typeEntity.flags
.withConformanceKind(ProtocolConformanceReferenceKind::WitnessTable);

Expand Down
6 changes: 6 additions & 0 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,9 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
SGM.M, SGM.M.getSwiftModule(), subs);
SILType substSILTy = SILType::getPrimitiveObjectType(substTy);

// Use the same context generic params as the native entry point.
F.setContextGenericParams(nativeInfo.ContextGenericParams);

auto loc = thunk.getAsRegularLocation();
loc.markAutoGenerated();
Scope scope(Cleanups, CleanupLocation::get(loc));
Expand Down Expand Up @@ -1116,6 +1119,9 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
auto nativeFnTy = F.getLoweredFunctionType();
assert(nativeFnTy == nativeCI.SILFnType);

// Use the same context generic params as the native entry point.
F.setContextGenericParams(nativeCI.ContextGenericParams);

// Find the foreign error convention and 'self' parameter index.
Optional<ForeignErrorConvention> foreignError;
if (nativeFnTy->hasErrorResult()) {
Expand Down
5 changes: 5 additions & 0 deletions test/IRGen/Inputs/objc_bridged_generic_conformance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import Foundation;

@interface Thingy<T>: NSObject

@end
14 changes: 14 additions & 0 deletions test/IRGen/objc_bridged_generic_conformance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-ir %s -import-objc-header %S/Inputs/objc_bridged_generic_conformance.h | %FileCheck %s
// REQUIRES: objc_interop

// CHECK-NOT: _TMnCSo

// CHECK: @"\01l_protocol_conformances" = {{.*}} @"got.OBJC_CLASS_$_Thingy"

// CHECK-NOT: _TMnCSo

protocol P { func test() }

extension Thingy: P {
func test() {}
}
5 changes: 5 additions & 0 deletions test/SILGen/Inputs/objc_bridged_generic_conformance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import Foundation;

@interface Thingy<T>: NSObject

@end
8 changes: 8 additions & 0 deletions test/SILGen/objc_bridged_generic_conformance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-silgen %s -import-objc-header %S/Inputs/objc_bridged_generic_conformance.h -verify
// REQUIRES: objc_interop

protocol P { func test() }

extension Thingy: P {
func test() {}
}