Skip to content

Commit d2c8823

Browse files
committed
[cxx-interop][SwiftToCxx] Do not crash while trying to expose a macro to C++
This fixes a compiler crash that happened when emitting a Clang header for a Swift module that declares multiple macros with the same base name and different argument names. Swift macros are not currently designed to be exposed to C++. This teaches the compiler to explicitly mark them as unavailable in C++. rdar://117969472 / resolves #69656 (cherry picked from commit f42609b)
1 parent a72679c commit d2c8823

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

include/swift/AST/DiagnosticsSema.def

+2
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,8 @@ ERROR(expose_move_only_to_cxx,none,
20332033
"noncopyable %kind0 can not yet be represented in C++", (ValueDecl *))
20342034
ERROR(expose_nested_type_to_cxx,none,
20352035
"nested %kind0 can not yet be represented in C++", (ValueDecl *))
2036+
ERROR(expose_macro_to_cxx,none,
2037+
"Swift macro can not yet be represented in C++", (ValueDecl *))
20362038
ERROR(unexposed_other_decl_in_cxx,none,
20372039
"%kind0 is not yet exposed to C++", (ValueDecl *))
20382040
ERROR(unsupported_other_decl_in_cxx,none,

include/swift/AST/SwiftNameTranslation.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ enum RepresentationError {
8383
UnrepresentableProtocol,
8484
UnrepresentableMoveOnly,
8585
UnrepresentableNested,
86+
UnrepresentableMacro,
8687
};
8788

8889
/// Constructs a diagnostic that describes the given C++ representation error.

lib/AST/SwiftNameTranslation.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
215215
return {Unsupported, UnrepresentableObjC};
216216
if (getActorIsolation(const_cast<ValueDecl *>(VD)).isActorIsolated())
217217
return {Unsupported, UnrepresentableIsolatedInActor};
218+
if (isa<MacroDecl>(VD))
219+
return {Unsupported, UnrepresentableMacro};
218220
GenericSignature genericSignature;
219221
// Don't expose @_alwaysEmitIntoClient decls as they require their
220222
// bodies to be emitted into client.
@@ -382,5 +384,7 @@ swift::cxx_translation::diagnoseRepresenationError(RepresentationError error,
382384
return Diagnostic(diag::expose_move_only_to_cxx, vd);
383385
case UnrepresentableNested:
384386
return Diagnostic(diag::expose_nested_type_to_cxx, vd);
387+
case UnrepresentableMacro:
388+
return Diagnostic(diag::expose_macro_to_cxx, vd);
385389
}
386390
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name MacroNameCollision -clang-header-expose-decls=all-public -emit-clang-header-path %t/macros.h
3+
// RUN: %FileCheck %s < %t/macros.h
4+
5+
// RUN: %check-interop-cxx-header-in-clang(%t/macros.h)
6+
7+
// CHECK-LABEL: namespace MacroNameCollision SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("MacroNameCollision") {
8+
9+
@freestanding(expression)
10+
public macro myLogMacro(error: String) = #externalMacro(module: "CompilerPlugin", type: "LogMacro")
11+
12+
@freestanding(expression)
13+
public macro myLogMacro(fault: String) = #externalMacro(module: "CompilerPlugin", type: "LogMacro")
14+
15+
// CHECK: // Unavailable in C++: Swift macro 'myLogMacro(error:)'
16+
// CHECK: // Unavailable in C++: Swift macro 'myLogMacro(fault:)'

0 commit comments

Comments
 (0)