Skip to content

Commit 54362d8

Browse files
committed
PrintAsClang: Protect null dereference and document Scalar behavior
Fix crash when printing references to Unicode.Scalar in the compatibility header. Unicode.Scalar should not be printed in the first place, it's a Swift struct. It should either be considered non-representable or printed as a C / Objective-C type if that's the intent. rdar://157120538
1 parent d8cfe92 commit 54362d8

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2406,7 +2406,8 @@ class DeclAndTypePrinter::Implementation
24062406
}
24072407

24082408
void maybePrintTagKeyword(const TypeDecl *NTD) {
2409-
if (auto *ED = dyn_cast<EnumDecl>(NTD); !NTD->hasClangNode()) {
2409+
auto *ED = dyn_cast<EnumDecl>(NTD);
2410+
if (ED && !NTD->hasClangNode()) {
24102411
if (ED->getAttrs().hasAttribute<CDeclAttr>()) {
24112412
// We should be able to use the tag macro for all printed enums but
24122413
// for now restrict it to @cdecl to guard it behind the feature flag.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// Test the behavior of printing Unicode.Scalar in the compatibility header.
4+
// This is wrong, it should either be rejected and considered non-representable
5+
// or actually be printed using a C / Objective-C compatible type.
6+
7+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) \
8+
// RUN: %s -emit-module -verify -o %t \
9+
// RUN: -emit-clang-header-path %t/compat.h
10+
// RUN: %FileCheck %s --input-file %t/compat.h
11+
12+
@_cdecl("referencesScalar")
13+
func referencesScalar() -> Unicode.Scalar { fatalError() }
14+
// CHECK: SWIFT_EXTERN Scalar referencesScalar(void)

0 commit comments

Comments
 (0)