Skip to content

AST/SILGen: Consider bridgeable value types to be bridgeable when bound to generic parameters. #2708

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: 4 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,10 @@ getForeignRepresentable(Type type, ForeignLanguage language,
nullptr };
}

// In Objective-C, type parameters are always objects.
if (type->isTypeParameter() && language == ForeignLanguage::ObjectiveC)
return { ForeignRepresentableKind::Object, nullptr };

auto nominal = type->getAnyNominal();
if (!nominal)
return failure();
Expand Down
3 changes: 0 additions & 3 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,6 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
};

{
GenericContextScope genericScope(SGM.Types,
foreignFnTy->getGenericSignature());

for (unsigned nativeParamIndex : indices(params)) {
// Bring the parameter to +1.
auto paramValue = params[nativeParamIndex];
Expand Down
5 changes: 5 additions & 0 deletions test/IRGen/objc_generic_class_metadata.sil
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ entry(%0: $ImplicitlyUnwrappedOptional<NSString>, %1: $Subclass):
unreachable
}

sil @_TToFC27objc_generic_class_metadata8SubclasscfT13arrayOfThingsGSaCSo8NSString__GSQS0__ : $@convention(objc_method) (NSArray, @owned Subclass) -> @owned ImplicitlyUnwrappedOptional<Subclass> {
entry(%0: $NSArray, %1: $Subclass):
unreachable
}

sil @_TToFC27objc_generic_class_metadata8SubclasscfT_S0_ : $@convention(objc_method) (@owned Subclass) -> @owned Subclass {
entry(%0: $Subclass):
unreachable
Expand Down
3 changes: 3 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/objc_generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

@interface GenericClass<T> : NSObject
- (id)initWithThing:(T)thing;
- (id)initWithArrayOfThings:(NSArray<T> *__nonnull)things;
- (void)dealloc;
- (__nullable T)thing;
- (int)count;
+ (__nullable T)classThing;
- (__nonnull NSArray<T> *)arrayOfThings;
- (void)setArrayOfThings:(NSArray<T> *__nonnull)things;

- (T __nonnull)objectAtIndexedSubscript:(uint16_t)i;
- (void)setObject:(T __nonnull)object atIndexedSubscript:(uint16_t)i;

@property (nonatomic) __nullable T propertyThing;
@property (nonatomic) __nullable NSArray<T> *propertyArrayOfThings;
@end

@interface GenericClass<T> (Private)
Expand Down
15 changes: 15 additions & 0 deletions test/SILGen/objc_imported_generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ public func genericPropertyOnAnyObject(o: AnyObject, b: Bool) -> AnyObject?? {
// CHECK-LABEL: sil @_TF21objc_imported_generic26genericPropertyOnAnyObject
// CHECK: dynamic_method_br %4 : $@opened([[TAG:.*]]) AnyObject, #GenericClass.propertyThing!getter.1.foreign, bb1
// CHECK: bb1({{%.*}} : $@convention(objc_method) (@opened([[TAG]]) AnyObject) -> @autoreleased Optional<AnyObject>):

// CHECK-LABEL: sil @_TF21objc_imported_generic20arraysOfGenericParam
public func arraysOfGenericParam<T: AnyObject>(y: Array<T>) {
// CHECK: function_ref {{@_TFCSo12GenericClassCfT13arrayOfThings.*}} : $@convention(method) <τ_0_0 where τ_0_0 : AnyObject> (@owned Array<τ_0_0>, @thick GenericClass<τ_0_0>.Type) -> @owned ImplicitlyUnwrappedOptional<GenericClass<τ_0_0>>
let x = GenericClass<T>(arrayOfThings: y)!
// CHECK: class_method [volatile] {{%.*}} : $GenericClass<T>, #GenericClass.setArrayOfThings!1.foreign {{.*}}, $@convention(objc_method) <τ_0_0 where τ_0_0 : AnyObject> (NSArray, GenericClass<τ_0_0>) -> ()
x.setArrayOfThings(y)
// CHECK: class_method [volatile] {{%.*}} : $GenericClass<T>, #GenericClass.propertyArrayOfThings!getter.1.foreign {{.*}}, $@convention(objc_method) <τ_0_0 where τ_0_0 : AnyObject> (GenericClass<τ_0_0>) -> @autoreleased Optional<NSArray>
_ = x.propertyArrayOfThings
// CHECK: class_method [volatile] {{%.*}} : $GenericClass<T>, #GenericClass.propertyArrayOfThings!setter.1.foreign {{.*}}, $@convention(objc_method) <τ_0_0 where τ_0_0 : AnyObject> (Optional<NSArray>, GenericClass<τ_0_0>) -> ()
x.propertyArrayOfThings = y
}

// CHECK-LABEL: sil shared [thunk] @_TTOFCSo12GenericClasscfT13arrayOfThings
// CHECK: class_method [volatile] {{%.*}} : $GenericClass<T>, #GenericClass.init!initializer.1.foreign {{.*}}, $@convention(objc_method) <τ_0_0 where τ_0_0 : AnyObject> (NSArray, @owned GenericClass<τ_0_0>) -> @owned ImplicitlyUnwrappedOptional<GenericClass<τ_0_0>>