Skip to content

Commit 4056273

Browse files
committed
[Clang importer] Existentials do not conform to Hashable.
Fixes rdar://problem/47564982. (cherry picked from commit 9084682)
1 parent d2f60e6 commit 4056273

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/Decl.h"
2424
#include "swift/AST/DiagnosticEngine.h"
2525
#include "swift/AST/DiagnosticsClangImporter.h"
26+
#include "swift/AST/ExistentialLayout.h"
2627
#include "swift/AST/GenericEnvironment.h"
2728
#include "swift/AST/Module.h"
2829
#include "swift/AST/NameLookup.h"
@@ -2441,7 +2442,11 @@ bool ClangImporter::Implementation::matchesHashableBound(Type type) {
24412442
}
24422443
}
24432444

2444-
// Class type or existential that inherits from NSObject.
2445+
// Existentials cannot match the Hashable bound.
2446+
if (type->isAnyExistentialType())
2447+
return false;
2448+
2449+
// Class type that inherits from NSObject.
24452450
if (NSObjectType->isExactSuperclassOf(type))
24462451
return true;
24472452

test/ClangImporter/Inputs/custom-modules/ObjCBridgeNonconforming.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@
2727
@interface ObjCBridgeGenericConstrainedExtra<Element: NSObject <ExtraElementProtocol> *> : NSObject
2828
@property NSSet<Element> * _Nonnull foo;
2929
@end
30+
31+
@interface ObjCBridgeExistential : NSObject
32+
@property NSSet<NSObject<ExtraElementProtocol> *> * _Nonnull foo;
33+
@end

test/ClangImporter/objc_bridging_generics.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,13 @@ func testHashableGenerics(
408408
any: ObjCBridgeGeneric<ElementConcrete>,
409409
constrained: ObjCBridgeGenericConstrained<ElementConcrete>,
410410
insufficient: ObjCBridgeGenericInsufficientlyConstrained<ElementConcrete>,
411-
extra: ObjCBridgeGenericConstrainedExtra<ElementConcrete>) {
411+
extra: ObjCBridgeGenericConstrainedExtra<ElementConcrete>,
412+
existential: ObjCBridgeExistential) {
412413
let _: Int = any.foo // expected-error{{cannot convert value of type 'Set<AnyHashable>' to specified type 'Int'}}
413414
let _: Int = constrained.foo // expected-error{{cannot convert value of type 'Set<ElementConcrete>' to specified type 'Int'}}
414415
let _: Int = insufficient.foo // expected-error{{cannot convert value of type 'Set<AnyHashable>' to specified type 'Int'}}
415416
let _: Int = extra.foo // expected-error{{cannot convert value of type 'Set<ElementConcrete>' to specified type 'Int'}}
417+
let _: Int = existential.foo // expected-error{{cannot convert value of type 'Set<AnyHashable>' to specified type 'Int'}}
416418
}
417419

418420
func testGenericsWithTypedefBlocks(hba: HasBlockArray) {

0 commit comments

Comments
 (0)