Skip to content

[Clang importer] Make sure that the first argument of Set/Dictionary types are Hashable. #10602

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
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
17 changes: 11 additions & 6 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,13 +984,12 @@ namespace {
return Type();

// The first type argument for Dictionary or Set needs
// to be Hashable. Everything that inherits NSObject has a
// -hash code in ObjC, but if something isn't NSObject, fall back
// to be Hashable. If something isn't Hashable, fall back
// to AnyHashable as a key type.
if (unboundDecl == Impl.SwiftContext.getDictionaryDecl() ||
unboundDecl == Impl.SwiftContext.getSetDecl()) {
auto &keyType = importedTypeArgs[0];
if (!Impl.matchesNSObjectBound(keyType)) {
if (!Impl.matchesHashableBound(keyType)) {
if (auto anyHashable = Impl.SwiftContext.getAnyHashableDecl())
keyType = anyHashable->getDeclaredType();
else
Expand Down Expand Up @@ -2413,7 +2412,7 @@ Type ClangImporter::Implementation::getNSObjectType() {
return Type();
}

bool ClangImporter::Implementation::matchesNSObjectBound(Type type) {
bool ClangImporter::Implementation::matchesHashableBound(Type type) {
Type NSObjectType = getNSObjectType();
if (!NSObjectType)
return false;
Expand All @@ -2425,8 +2424,14 @@ bool ClangImporter::Implementation::matchesNSObjectBound(Type type) {
// Struct or enum type must have been bridged.
// TODO: Check that the bridged type is Hashable?
if (type->getStructOrBoundGenericStruct() ||
type->getEnumOrBoundGenericEnum())
return true;
type->getEnumOrBoundGenericEnum()) {
auto nominal = type->getAnyNominal();
auto hashable = SwiftContext.getProtocol(KnownProtocolKind::Hashable);
SmallVector<ProtocolConformance *, 2> conformances;
return hashable &&
nominal->lookupConformance(nominal->getParentModule(), hashable,
conformances);
}

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation

/// \brief Determines whether the given type matches an implicit type
/// bound of "Hashable", which is used to validate NSDictionary/NSSet.
bool matchesNSObjectBound(Type type);
bool matchesHashableBound(Type type);

/// \brief Look up and attempt to import a Clang declaration with
/// the given name.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import ObjectiveC;
@import Foundation;

@interface ObjCBridgeNonconforming
@property NSSet<NSDictionary<NSString *, id> *> * _Nonnull foo;
@end
4 changes: 4 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/module.map
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,7 @@ module MacrosRedefB {
module IndirectFields {
header "IndirectFields.h"
}

module ObjCBridgeNonconforming {
header "ObjCBridgeNonconforming.h"
}
8 changes: 7 additions & 1 deletion test/ClangImporter/objc_bridging_generics.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -parse-as-library -verify -swift-version 4 %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -parse-as-library -verify -swift-version 4 -I %S/Inputs/custom-modules %s

// REQUIRES: objc_interop

import Foundation
import objc_generics
import ObjCBridgeNonconforming

func testNSArrayBridging(_ hive: Hive) {
_ = hive.bees as [Bee]
Expand Down Expand Up @@ -392,3 +393,8 @@ let third: Third! = Third()
func useThird() {
_ = third.description
}


func testNonconforming(bnc: ObjCBridgeNonconforming) {
let _: Int = bnc.foo // expected-error{{cannot convert value of type 'Set<AnyHashable>' to specified type 'Int'}}
}