Skip to content

Fix for missing conformance with id-as-Any bridging #4298

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
20 changes: 16 additions & 4 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4246,13 +4246,25 @@ void TypeChecker::useObjectiveCBridgeableConformances(DeclContext *dc,
: TC(tc), DC(dc), Proto(proto) { }

virtual Action walkToTypePre(Type ty) {
ConformanceCheckOptions options = ConformanceCheckFlags::InExpression
| ConformanceCheckFlags::Used
| ConformanceCheckFlags::SuppressDependencyTracking;

// If we have a nominal type, "use" its conformance to
// _ObjectiveCBridgeable if it has one.
if (ty->getAnyNominal()) {
ConformanceCheckOptions options = ConformanceCheckFlags::InExpression
| ConformanceCheckFlags::Used
| ConformanceCheckFlags::SuppressDependencyTracking;
if (auto *nominalDecl = ty->getAnyNominal()) {
(void)TC.conformsToProtocol(ty, Proto, DC, options);

if (nominalDecl == TC.Context.getSetDecl() ||
nominalDecl == TC.Context.getDictionaryDecl()) {
auto args = ty->castTo<BoundGenericType>()->getGenericArgs();
if (!args.empty()) {
auto keyType = args[0];
auto *hashableProto =
TC.Context.getProtocol(KnownProtocolKind::Hashable);
(void)TC.conformsToProtocol(keyType, hashableProto, DC, options);
}
}
}

return Action::Continue;
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 @@ -71,6 +71,11 @@ entry(%0: $Subclass):
unreachable
}

sil @_TToFC27objc_generic_class_metadata8SubclasscfT7optionsGSqGVs10DictionaryVSC13GenericOptionP____GSQS0__ : $@convention(objc_method) (@owned Subclass, @owned NSDictionary) -> @owned Subclass {
entry(%0: $Subclass, %1: $NSDictionary):
unreachable
}

// CHECK-LABEL: define linkonce_odr hidden %swift.type* @_TMaCSo12GenericClass()
// CHECK: [[T0:%.*]] = load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_GenericClass",
// CHECK: call %objc_class* @rt_swift_getInitializedObjCClass(%objc_class* [[T0]])
Expand Down
10 changes: 10 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/objc_generics.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#import <Foundation.h>

#define _CF_TYPED_ENUM __attribute__((swift_wrapper(enum)))
#define NS_STRING_ENUM _CF_TYPED_ENUM
#define NS_SWIFT_NAME(Name) __attribute__((swift_name(#Name)))

typedef NSString * GenericOption NS_STRING_ENUM;

GenericOption const GenericOptionMultithreaded NS_SWIFT_NAME(multithreaded);


@interface GenericClass<T> : NSObject
- (id)initWithThing:(T)thing;
- (id)initWithArrayOfThings:(NSArray<T> *__nonnull)things;
- (id)initWithOptions:(nullable NSDictionary<GenericOption, id> *)options;
- (void)dealloc;
- (__nullable T)thing;
- (int)count;
Expand Down
24 changes: 24 additions & 0 deletions test/SILGen/objc_imported_generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,29 @@ func genericFunc<V: AnyObject>(_ v: V.Type) {
}
}

// CHECK-LABEL: sil hidden @_TF21objc_imported_generic23configureWithoutOptionsFT_T_ : $@convention(thin) () -> ()
// CHECK: [[NIL_FN:%.*]] = function_ref @_TFSqCfT10nilLiteralT__GSqx_ : $@convention(method) <τ_0_0> (@thin Optional<τ_0_0>.Type) -> @out Optional<τ_0_0>
// CHECK: apply [[NIL_FN]]<[GenericOption : Any]>({{.*}})
// CHECK: return
func configureWithoutOptions() {
_ = GenericClass<NSObject>(options: nil)
}

// foreign to native thunk for init(options:), uses GenericOption : Hashable
// conformance

// CHECK-LABEL: sil shared [thunk] @_TTOFCSo12GenericClasscfT7optionsGSqGVs10DictionaryVSC13GenericOptionP____GSQGS_x__ : $@convention(method) <T where T : AnyObject> (@owned Optional<Dictionary<GenericOption, Any>>, @owned GenericClass<T>) -> @owned ImplicitlyUnwrappedOptional<GenericClass<T>>
// CHECK: [[FN:%.*]] = function_ref @_TFE10FoundationVs10Dictionary19_bridgeToObjectiveCfT_CSo12NSDictionary : $@convention(method) <τ_0_0, τ_0_1 where τ_0_0 : Hashable> (@guaranteed Dictionary<τ_0_0, τ_0_1>) -> @owned NSDictionary
// CHECK: apply [[FN]]<GenericOption, Any>({{.*}}) : $@convention(method) <τ_0_0, τ_0_1 where τ_0_0 : Hashable> (@guaranteed Dictionary<τ_0_0, τ_0_1>) -> @owned NSDictionary
// CHECK: return

// This gets emitted down here for some reason

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

// Make sure we emitted the witness table for the above conformance

// CHECK-LABEL: sil_witness_table shared [fragile] GenericOption: Hashable module objc_generics {
// CHECK: method #Hashable.hashValue!getter.1: @_TTWVSC13GenericOptions8Hashable13objc_genericsFS0_g9hashValueSi
// CHECK: }
2 changes: 1 addition & 1 deletion validation-test/stdlib/SceneKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ if #available(iOS 8.0, *) {
let sceneData = sceneDescription.data(
using: .utf8,
allowLossyConversion: true)!
let sceneSource = SCNSceneSource(data: sceneData as Data, options: [:])!
let sceneSource = SCNSceneSource(data: sceneData as Data, options: nil)!

do {
var unarchivedPlaneGeometry =
Expand Down