Skip to content

[ASTMangler] Mangle nested imported error structs correctly #24804

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
8 changes: 6 additions & 2 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ Type ASTBuilder::createNominalType(GenericTypeDecl *decl, Type parent) {
// Imported types can be renamed to be members of other (non-generic)
// types, but the mangling does not have a parent type. Just use the
// declared type directly in this case and skip the parent check below.
if (nominalDecl->hasClangNode() && !nominalDecl->isGenericContext())
bool isImported = nominalDecl->hasClangNode() ||
nominalDecl->getAttrs().hasAttribute<ClangImporterSynthesizedTypeAttr>();
if (isImported && !nominalDecl->isGenericContext())
return nominalDecl->getDeclaredType();

// Validate the parent type.
Expand All @@ -177,7 +179,9 @@ Type ASTBuilder::createTypeAliasType(GenericTypeDecl *decl, Type parent) {
// Imported types can be renamed to be members of other (non-generic)
// types, but the mangling does not have a parent type. Just use the
// declared type directly in this case and skip the parent check below.
if (aliasDecl->hasClangNode() && !aliasDecl->isGenericContext())
bool isImported = aliasDecl->hasClangNode() ||
aliasDecl->getAttrs().hasAttribute<ClangImporterSynthesizedTypeAttr>();
if (isImported && !aliasDecl->isGenericContext())
return aliasDecl->getDeclaredInterfaceType();

// Validate the parent type.
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,12 @@ ASTMangler::getSpecialManglingContext(const ValueDecl *decl,
}
}

// Importer-synthesized types should always be mangled in the
// ClangImporterContext, even if an __attribute__((swift_name())) nests them
// inside a Swift type syntactically.
if (decl->getAttrs().hasAttribute<ClangImporterSynthesizedTypeAttr>())
return ASTMangler::ClangImporterContext;

return None;
}

Expand Down
12 changes: 12 additions & 0 deletions test/IRGen/Inputs/error_domains.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ extern NSString *TypedefDomain2;
typedef enum __attribute__((ns_error_domain(TypedefDomain2))) {
Illness
} TypedefError2;

@interface Nested @end

extern NSString * const NestedTagDomain;
typedef MY_ERROR_ENUM(int, NestedTagError, NestedTagDomain) {
Trappedness
} __attribute__((swift_name("Nested.TagError")));

extern NSString *NestedTypedefDomain;
typedef enum __attribute__((ns_error_domain(NestedTypedefDomain))) {
Brokenness
} NestedTypedefError __attribute__((swift_name("Nested.TypedefError")));
18 changes: 18 additions & 0 deletions test/IRGen/related_entity.sil
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ sil @use_metatypes : $@convention(thin) () -> () {
bb0:
%take = function_ref @take_metatype : $@convention(thin) <T> (@thick T.Type) -> ()

// CHECK: [[NestedTypedefErrorCode_NAME:@[0-9]+]] = private constant [29 x i8] c"Code\00NNestedTypedefError\00St\00\00"
// CHECK: @"$sSo18NestedTypedefErroraMn" = linkonce_odr hidden constant
// CHECK-SAME: <i32 0x0006_0012>
// CHECK-SAME: @"$sSoMXM"
// CHECK-SAME: [29 x i8]* [[NestedTypedefErrorCode_NAME]]

// CHECK: [[NestedTagError_NAME:@[0-9]+]] = private constant [29 x i8] c"TagError\00NNestedTagError\00Re\00\00"
// CHECK: @"$sSC14NestedTagErrorLeVMn" = linkonce_odr hidden constant
// CHECK-SAME: <i32 0x0006_0011>
// CHECK-SAME: @"$sSCMXM"
// CHECK-SAME: [29 x i8]* [[NestedTagError_NAME]]

// CHECK: [[TypedefError2Code_NAME:@[0-9]+]] = private constant [24 x i8] c"Code\00NTypedefError2\00St\00\00"
// CHECK: @"$sSo13TypedefError2aMn" = linkonce_odr hidden constant
// CHECK-SAME: <i32 0x0006_0012>
Expand Down Expand Up @@ -49,6 +61,12 @@ bb0:
%3 = metatype $@thick TypedefError2.Code.Type
apply %take<TypedefError2.Code>(%3) : $@convention(thin) <T> (@thick T.Type) -> ()

%4 = metatype $@thick Nested.TagError.Type
apply %take<Nested.TagError>(%4) : $@convention(thin) <T> (@thick T.Type) -> ()

%5 = metatype $@thick Nested.TypedefError.Code.Type
apply %take<Nested.TypedefError.Code>(%5) : $@convention(thin) <T> (@thick T.Type) -> ()

%ret = tuple ()
return %ret : $()
}
9 changes: 7 additions & 2 deletions test/Inputs/custom-modules/ErrorEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ struct Wrapper {
int unrelatedValue;
};

// Not actually an error enum, since those can't be import-as-member'd right
// now, but it can still hang with us.
NSString * const MyMemberErrorDomain;
typedef NS_ENUM(int, MyMemberError) {
MyMemberErrorA,
MyMemberErrorB,
} __attribute__((ns_error_domain(MyMemberErrorDomain))) __attribute__((swift_name("Wrapper.MemberError")));

// Not actually an error enum, but it can still hang with us.
typedef NS_ENUM(int, MyMemberEnum) {
MyMemberEnumA,
MyMemberEnumB,
Expand Down
8 changes: 7 additions & 1 deletion test/RemoteAST/foreign_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ printType(RenamedError.self)
printType(RenamedError.Code.self)
// CHECK: found type: RenamedError.Code{{$}}

printType(Wrapper.MemberError.self)
// CHECK: found type: Wrapper.MemberError{{$}}

printType(Wrapper.MemberError.Code.self)
// CHECK: found type: Wrapper.MemberError.Code{{$}}

printType(Wrapper.MemberEnum.self)
// CHECK: found type: Wrapper.MemberEnum{{$}}

printType(WrapperByAttribute.self)
// CHECK: found type: WrapperByAttribute{{$}}

stopRemoteAST()
stopRemoteAST()
16 changes: 16 additions & 0 deletions test/TypeDecoder/foreign_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ do {
let x3 = MyError.Code.good
let x4 = RenamedError.good
let x5 = RenamedError.Code.good
let x5b = Wrapper.MemberError.A
let x5c = Wrapper.MemberError.Code.A
let x6 = Wrapper.MemberEnum.A
let x7 = WrapperByAttribute(0)
let x8 = IceCube(width: 0, height: 0, depth: 0)
Expand All @@ -38,6 +40,8 @@ do {
let x3 = MyError.Code.self
let x4 = RenamedError.self
let x5 = RenamedError.Code.self
let x5b = Wrapper.MemberError.self
let x5c = Wrapper.MemberError.Code.self
let x6 = Wrapper.MemberEnum.self
let x7 = WrapperByAttribute.self
let x8 = IceCube.self
Expand All @@ -50,6 +54,8 @@ do {
// DEMANGLE-TYPE: $sSo7MyErrorLeVD
// DEMANGLE-TYPE: $sSo14MyRenamedErrorVD
// DEMANGLE-TYPE: $sSo14MyRenamedErrorLeVD
// DEMANGLE-TYPE: $sSo13MyMemberErrorVD
// DEMANGLE-TYPE: $sSo13MyMemberErrorLeVD
// DEMANGLE-TYPE: $sSo12MyMemberEnumVD
// DEMANGLE-TYPE: $sSo18WrapperByAttributeaD
// DEMANGLE-TYPE: $sSo7IceCubeVD
Expand All @@ -62,6 +68,8 @@ do {
// CHECK-TYPE: MyError
// CHECK-TYPE: RenamedError.Code
// CHECK-TYPE: RenamedError
// CHECK-TYPE: Wrapper.MemberError.Code
// CHECK-TYPE: Wrapper.MemberError
// CHECK-TYPE: Wrapper.MemberEnum
// CHECK-TYPE: WrapperByAttribute
// CHECK-TYPE: IceCube
Expand All @@ -74,6 +82,8 @@ do {
// DEMANGLE-TYPE: $sSC7MyErrorLeVmD
// DEMANGLE-TYPE: $sSo14MyRenamedErrorVmD
// DEMANGLE-TYPE: $sSC14MyRenamedErrorLeVmD
// DEMANGLE-TYPE: $sSo13MyMemberErrorVmD
// DEMANGLE-TYPE: $sSC13MyMemberErrorLeVmD
// DEMANGLE-TYPE: $sSo12MyMemberEnumVmD
// DEMANGLE-TYPE: $sSo18WrapperByAttributeamD
// DEMANGLE-TYPE: $sSo7IceCubeVmD
Expand All @@ -86,6 +96,8 @@ do {
// CHECK-TYPE: MyError.Type
// CHECK-TYPE: RenamedError.Code.Type
// CHECK-TYPE: RenamedError.Type
// CHECK-TYPE: Wrapper.MemberError.Code.Type
// CHECK-TYPE: Wrapper.MemberError.Type
// CHECK-TYPE: Wrapper.MemberEnum.Type
// CHECK-TYPE: WrapperByAttribute.Type
// CHECK-TYPE: IceCube.Type
Expand All @@ -98,6 +110,8 @@ do {
// DEMANGLE-DECL: $sSo7MyErrorLeV
// DEMANGLE-DECL: $sSo14MyRenamedErrorV
// DEMANGLE-DECL: $sSo14MyRenamedErrorLeV
// DEMANGLE-DECL: $sSo13MyMemberErrorV
// DEMANGLE-DECL: $sSo13MyMemberErrorLeV
// DEMANGLE-DECL: $sSo12MyMemberEnumV
// DEMANGLE-DECL: $sSo18WrapperByAttributea
// DEMANGLE-DECL: $sSo7IceCubeV
Expand All @@ -110,6 +124,8 @@ do {
// CHECK-DECL: ErrorEnums.(file).MyError.Code
// CHECK-DECL: ErrorEnums.(file).RenamedError.Code
// CHECK-DECL: ErrorEnums.(file).RenamedError.Code
// CHECK-DECL: ErrorEnums.(file).Wrapper extension.MemberError.Code
// CHECK-DECL: ErrorEnums.(file).Wrapper extension.MemberError.Code
// CHECK-DECL: ErrorEnums.(file).Wrapper extension.MemberEnum
// CHECK-DECL: ErrorEnums.(file).WrapperByAttribute
// CHECK-DECL: CoreCooling.(file).IceCube
Expand Down