From 94445346584fd74a37a7592b7d1d15a7fb21ce34 Mon Sep 17 00:00:00 2001 From: Allan Shortlidge Date: Sat, 9 Dec 2023 21:51:33 -0800 Subject: [PATCH 1/3] AST: Handle MacroExpansionDecl in DeclExportabilityVisitor. Instances of `MacroExpansionDecl` may be found in the AST when declarations are expanded from freestanding declaration macros and therefore exportability computations must be prepared to handle them. Resolves rdar://119449439 --- include/swift/AST/DeclExportabilityVisitor.h | 2 +- test/Macros/skip_non_exportable_decls.swift | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/Macros/skip_non_exportable_decls.swift diff --git a/include/swift/AST/DeclExportabilityVisitor.h b/include/swift/AST/DeclExportabilityVisitor.h index 2bbe4bfc1efaa..b2180c1730d8e 100644 --- a/include/swift/AST/DeclExportabilityVisitor.h +++ b/include/swift/AST/DeclExportabilityVisitor.h @@ -153,7 +153,6 @@ class DeclExportabilityVisitor UNREACHABLE(PoundDiagnostic); UNREACHABLE(Missing); UNREACHABLE(MissingMember); - UNREACHABLE(MacroExpansion); UNREACHABLE(GenericTypeParam); UNREACHABLE(Param); @@ -172,6 +171,7 @@ class DeclExportabilityVisitor UNINTERESTING(PrecedenceGroup); UNINTERESTING(EnumCase); UNINTERESTING(Operator); + UNINTERESTING(MacroExpansion); #undef UNINTERESTING }; diff --git a/test/Macros/skip_non_exportable_decls.swift b/test/Macros/skip_non_exportable_decls.swift new file mode 100644 index 0000000000000..69700ae62dace --- /dev/null +++ b/test/Macros/skip_non_exportable_decls.swift @@ -0,0 +1,17 @@ +// REQUIRES: swift_swift_parser, executable_test + +// RUN: %empty-directory(%t) +// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift + +// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/freestanding_macro_library.swiftmodule %S/Inputs/freestanding_macro_library.swift -module-name freestanding_macro_library -load-plugin-library %t/%target-library-name(MacroDefinition) + +// RUN: %target-swift-frontend -parse-as-library -emit-sil -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -experimental-skip-non-exportable-decls | %FileCheck %s + +@freestanding(declaration) +macro anonymousTypes(public: Bool = false, causeErrors: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro") + +// CHECK: sil @$s9MacroUser03$s9A70User33_B2D49A1BE4DC7AF5CC327EB8EE2214BDLl14anonymousTypesfMf_4namefMu_C5helloSSyF : $@convention(method) (@guaranteed $s9MacroUser33_B2D49A1BE4DC7AF5CC327EB8EE2214BDLl14anonymousTypesfMf_4namefMu_) -> @owned String { +#anonymousTypes(public: true) { "hello" } + +// CHECK-NOT: s9MacroUser03$s9A71User33_B2D49A1BE4DC7AF5CC327EB8EE2214BDLl14anonymousTypesfMf_4namefMu0_O5helloSSyF +#anonymousTypes(public: false) { "goodbye" } From a038f7d8c6cb418a5ccad5e9fa68d71da8f855d7 Mon Sep 17 00:00:00 2001 From: Allan Shortlidge Date: Sat, 9 Dec 2023 22:01:23 -0800 Subject: [PATCH 2/3] AST: Handle PoundDiagnosticDecl in DeclExportabilityVisitor. --- include/swift/AST/DeclExportabilityVisitor.h | 2 +- test/SILGen/skip_non_exportable_decls.swift | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/swift/AST/DeclExportabilityVisitor.h b/include/swift/AST/DeclExportabilityVisitor.h index b2180c1730d8e..f4240903ea54f 100644 --- a/include/swift/AST/DeclExportabilityVisitor.h +++ b/include/swift/AST/DeclExportabilityVisitor.h @@ -150,7 +150,6 @@ class DeclExportabilityVisitor } UNREACHABLE(Module); UNREACHABLE(TopLevelCode); - UNREACHABLE(PoundDiagnostic); UNREACHABLE(Missing); UNREACHABLE(MissingMember); UNREACHABLE(GenericTypeParam); @@ -168,6 +167,7 @@ class DeclExportabilityVisitor bool visit##KIND##Decl(const KIND##Decl *D) { return true; } UNINTERESTING(IfConfig); UNINTERESTING(Import); + UNINTERESTING(PoundDiagnostic); UNINTERESTING(PrecedenceGroup); UNINTERESTING(EnumCase); UNINTERESTING(Operator); diff --git a/test/SILGen/skip_non_exportable_decls.swift b/test/SILGen/skip_non_exportable_decls.swift index aef39c7d940a3..8ea2dcbe4df94 100644 --- a/test/SILGen/skip_non_exportable_decls.swift +++ b/test/SILGen/skip_non_exportable_decls.swift @@ -4,6 +4,8 @@ import Swift +#warning("!") + // CHECK-NO-SKIP: sil_global private @$s4Test17internalGlobalVar_Wz : $Builtin.Word // CHECK-SKIP-NOT: s4Test17internalGlobalVar_Wz From 3c68c20ef5bd290bbfa92754413a50e30f1be13f Mon Sep 17 00:00:00 2001 From: Allan Shortlidge Date: Sat, 9 Dec 2023 22:11:19 -0800 Subject: [PATCH 3/3] AST: Handle TopLevelCodeDecl in DeclExportabilityVisitor. --- include/swift/AST/DeclExportabilityVisitor.h | 2 +- test/SILGen/skip_non_exportable_decls_top_level.swift | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 test/SILGen/skip_non_exportable_decls_top_level.swift diff --git a/include/swift/AST/DeclExportabilityVisitor.h b/include/swift/AST/DeclExportabilityVisitor.h index f4240903ea54f..70efb55c5afb3 100644 --- a/include/swift/AST/DeclExportabilityVisitor.h +++ b/include/swift/AST/DeclExportabilityVisitor.h @@ -149,7 +149,6 @@ class DeclExportabilityVisitor return true; \ } UNREACHABLE(Module); - UNREACHABLE(TopLevelCode); UNREACHABLE(Missing); UNREACHABLE(MissingMember); UNREACHABLE(GenericTypeParam); @@ -165,6 +164,7 @@ class DeclExportabilityVisitor // context has already been checked. #define UNINTERESTING(KIND) \ bool visit##KIND##Decl(const KIND##Decl *D) { return true; } + UNINTERESTING(TopLevelCode); UNINTERESTING(IfConfig); UNINTERESTING(Import); UNINTERESTING(PoundDiagnostic); diff --git a/test/SILGen/skip_non_exportable_decls_top_level.swift b/test/SILGen/skip_non_exportable_decls_top_level.swift new file mode 100644 index 0000000000000..38e19d007838d --- /dev/null +++ b/test/SILGen/skip_non_exportable_decls_top_level.swift @@ -0,0 +1,11 @@ +// RUN: %empty-directory(%t) +// RUN: %target-swift-frontend -emit-silgen %s -module-name Test | %FileCheck %s --check-prefixes=CHECK,CHECK-NO-SKIP +// RUN: %target-swift-frontend -emit-silgen %s -module-name Test -experimental-skip-non-exportable-decls | %FileCheck %s --check-prefixes=CHECK,CHECK-SKIP + +// CHECK-NO-SKIP: sil_global hidden @$s4Test1xSivp : $Int +// CHECK-SKIP: sil_global hidden_external @$s4Test1xSivp : $Int +var x = foo() + +// CHECK-NO-SKIP: sil hidden{{.*}} @$s4Test3fooSiyF : $@convention(thin) () -> Int { +// CHECK-SKIP: sil hidden_external @$s4Test3fooSiyF : $@convention(thin) () -> Int +func foo() -> Int { return 1 }