Skip to content

Commit a741d59

Browse files
authored
Merge pull request #22310 from DougGregor/importer-existentials-not-hashable-5.0
[5.0] [Clang importer] Existentials do not conform to Hashable.
2 parents 3155805 + 2f3468f commit a741d59

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 13 additions & 4 deletions
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"
@@ -2435,13 +2436,21 @@ bool ClangImporter::Implementation::matchesHashableBound(Type type) {
24352436
// Match generic parameters against their bounds.
24362437
if (auto *genericTy = type->getAs<GenericTypeParamType>()) {
24372438
if (auto *generic = genericTy->getDecl()) {
2438-
type = generic->getSuperclass();
2439-
if (!type)
2440-
return false;
2439+
auto genericSig =
2440+
generic->getDeclContext()->getGenericSignatureOfContext();
2441+
if (genericSig && genericSig->getConformsTo(type).empty()) {
2442+
type = genericSig->getSuperclassBound(type);
2443+
if (!type)
2444+
return false;
2445+
}
24412446
}
24422447
}
24432448

2444-
// Class type or existential that inherits from NSObject.
2449+
// Existentials cannot match the Hashable bound.
2450+
if (type->isAnyExistentialType())
2451+
return false;
2452+
2453+
// Class type that inherits from NSObject.
24452454
if (NSObjectType->isExactSuperclassOf(type))
24462455
return true;
24472456

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: 4 additions & 2 deletions
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'}}
415-
let _: Int = extra.foo // expected-error{{cannot convert value of type 'Set<ElementConcrete>' to specified type 'Int'}}
416+
let _: Int = extra.foo // expected-error{{cannot convert value of type 'Set<AnyHashable>' 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)