Skip to content

[cxx-interop][SwiftToCxx] Do not crash while trying to expose a macro to C++ #73510

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 1 commit into from
May 10, 2024
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,8 @@ ERROR(expose_move_only_to_cxx,none,
"noncopyable %kind0 can not yet be represented in C++", (ValueDecl *))
ERROR(expose_nested_type_to_cxx,none,
"nested %kind0 can not yet be represented in C++", (ValueDecl *))
ERROR(expose_macro_to_cxx,none,
"Swift macro can not yet be represented in C++", (ValueDecl *))
ERROR(unexposed_other_decl_in_cxx,none,
"%kind0 is not yet exposed to C++", (ValueDecl *))
ERROR(unsupported_other_decl_in_cxx,none,
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/SwiftNameTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ enum RepresentationError {
UnrepresentableProtocol,
UnrepresentableMoveOnly,
UnrepresentableNested,
UnrepresentableMacro,
};

/// Constructs a diagnostic that describes the given C++ representation error.
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
return {Unsupported, UnrepresentableObjC};
if (getActorIsolation(const_cast<ValueDecl *>(VD)).isActorIsolated())
return {Unsupported, UnrepresentableIsolatedInActor};
if (isa<MacroDecl>(VD))
return {Unsupported, UnrepresentableMacro};
GenericSignature genericSignature;
// Don't expose @_alwaysEmitIntoClient decls as they require their
// bodies to be emitted into client.
Expand Down Expand Up @@ -382,5 +384,7 @@ swift::cxx_translation::diagnoseRepresenationError(RepresentationError error,
return Diagnostic(diag::expose_move_only_to_cxx, vd);
case UnrepresentableNested:
return Diagnostic(diag::expose_nested_type_to_cxx, vd);
case UnrepresentableMacro:
return Diagnostic(diag::expose_macro_to_cxx, vd);
}
}
16 changes: 16 additions & 0 deletions test/Interop/SwiftToCxx/macros/macro-name-collision.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name MacroNameCollision -clang-header-expose-decls=all-public -emit-clang-header-path %t/macros.h
// RUN: %FileCheck %s < %t/macros.h

// RUN: %check-interop-cxx-header-in-clang(%t/macros.h)

// CHECK-LABEL: namespace MacroNameCollision SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("MacroNameCollision") {

@freestanding(expression)
public macro myLogMacro(error: String) = #externalMacro(module: "CompilerPlugin", type: "LogMacro")

@freestanding(expression)
public macro myLogMacro(fault: String) = #externalMacro(module: "CompilerPlugin", type: "LogMacro")

// CHECK: // Unavailable in C++: Swift macro 'myLogMacro(error:)'
// CHECK: // Unavailable in C++: Swift macro 'myLogMacro(fault:)'