From 63f91d13a7fdd7b98cc4de49b1a705377e4a48b1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 21 May 2020 17:18:16 -0400 Subject: [PATCH] PrintAsObjC: Fix crash when printing typedef that was imported inside an Objective-C generic class The assertion here is too strict. The TypeAliasDecl will inherit a generic signature from the outer context even if it does not have generic parameters of its own. Instead, let's just assert that the TypeAliasDecl does not have its own generic parameters. Fixes . --- lib/PrintAsObjC/ModuleContentsWriter.cpp | 2 +- .../Inputs/imported-generic-typealias.h | 11 +++++++++++ test/PrintAsObjC/imported-generic-typealias.swift | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/PrintAsObjC/Inputs/imported-generic-typealias.h create mode 100644 test/PrintAsObjC/imported-generic-typealias.swift diff --git a/lib/PrintAsObjC/ModuleContentsWriter.cpp b/lib/PrintAsObjC/ModuleContentsWriter.cpp index d850955c51e4b..9fdb09e682273 100644 --- a/lib/PrintAsObjC/ModuleContentsWriter.cpp +++ b/lib/PrintAsObjC/ModuleContentsWriter.cpp @@ -56,7 +56,7 @@ class ReferencedTypeFinder : public TypeDeclFinder { Action visitTypeAliasType(TypeAliasType *aliasTy) override { if (aliasTy->getDecl()->hasClangNode() && !aliasTy->getDecl()->isCompatibilityAlias()) { - assert(!aliasTy->getGenericSignature()); + assert(!aliasTy->getDecl()->isGeneric()); Callback(*this, aliasTy->getDecl()); } else { Type(aliasTy->getSinglyDesugaredType()).walk(*this); diff --git a/test/PrintAsObjC/Inputs/imported-generic-typealias.h b/test/PrintAsObjC/Inputs/imported-generic-typealias.h new file mode 100644 index 0000000000000..f25f2bec8e323 --- /dev/null +++ b/test/PrintAsObjC/Inputs/imported-generic-typealias.h @@ -0,0 +1,11 @@ +@interface NSObject +- (void) init; +@end; + +@interface Horse : NSObject +@end + +@interface Barn : NSObject +@end + +typedef int Hay __attribute__((swift_name("Horse.Hay"))); diff --git a/test/PrintAsObjC/imported-generic-typealias.swift b/test/PrintAsObjC/imported-generic-typealias.swift new file mode 100644 index 0000000000000..dc442656c33b6 --- /dev/null +++ b/test/PrintAsObjC/imported-generic-typealias.swift @@ -0,0 +1,14 @@ +// REQUIRES: objc_interop + +// RUN: %empty-directory(%t) +// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -parse-as-library %s -typecheck -emit-objc-header-path %t/imported-generic-typealias.h -import-objc-header %S/Inputs/imported-generic-typealias.h -disable-objc-attr-requires-foundation-module +// RUN: %FileCheck -check-prefix=CHECK -check-prefix=CHECK-PUBLIC %s < %t/imported-generic-typealias.h + +@objc public class MyRedBarn : Barn { + @objc public func feed(_: Horse.Hay) {} +} + +// CHECK-LABEL: SWIFT_CLASS("_TtC4main9MyRedBarn") +// CHECK-NEXT: @interface MyRedBarn : Barn +// CHECK-NEXT: - (void)feed:(Hay)_; +// CHECK-NEXT: @end