Skip to content

Commit 0276d46

Browse files
author
Gabor Horvath
committed
[cxx-interop] Fix reverse interop crash when using raw modules
Some fields in the AST are cached values that are populated lazily. We should not use those values directly as in case they are not yet computed we get back null pointers. Use ASTContext instead which can call the slow path if the cache is not yet populated. rdar://132746445
1 parent da61cd6 commit 0276d46

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,10 @@ class ModuleWriter {
284284
if (!isa<clang::TypeDecl>(typeDecl->getClangDecl()))
285285
return;
286286
// Get the underlying clang type from a type alias decl or record decl.
287-
auto clangType =
288-
clang::QualType(
289-
cast<clang::TypeDecl>(typeDecl->getClangDecl())->getTypeForDecl(),
290-
0)
291-
.getCanonicalType();
287+
auto clangDecl = typeDecl->getClangDecl();
288+
auto clangType = clangDecl->getASTContext()
289+
.getTypeDeclType(cast<clang::TypeDecl>(clangDecl))
290+
.getCanonicalType();
292291
if (!isa<clang::RecordType>(clangType.getTypePtr()))
293292
return;
294293
auto it = seenClangTypes.insert(clangType.getTypePtr());

test/Interop/CxxToSwiftToCxx/span/span-execution.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %empty-directory(%t)
22
// RUN: split-file %s %t
33

4-
// RUN: %target-swift-frontend -typecheck %t/use-span.swift -typecheck -module-name UseSpan -emit-clang-header-path %t/UseSpan.h -I %t -enable-experimental-cxx-interop -Xcc -std=c++20 -clang-header-expose-decls=all-public
4+
// RUN: %target-swift-frontend -typecheck %t/use-span.swift -typecheck -module-name UseSpan -emit-clang-header-path %t/UseSpan.h -I %t -enable-experimental-cxx-interop -Xcc -Xclang -Xcc -fmodule-format=raw -Xcc -std=c++20 -clang-header-expose-decls=all-public
55

66
// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-span.cpp -I %t -o %t/swift-cxx-execution.o
77
// RUN: %target-interop-build-swift %t/use-span.swift -o %t/swift-cxx-execution -Xlinker %t/swift-cxx-execution.o -module-name UseSpan -Xfrontend -entry-point-function-name -Xfrontend swiftMain -I %t -O -Xcc --std=c++20
@@ -21,6 +21,10 @@
2121
using Span = std::span<int>;
2222
using SpanOfString = std::span<std::string>;
2323

24+
namespace ns {
25+
using SpanOfConstUInt8 = std::span<const uint8_t>;
26+
}
27+
2428
static int staticArr[] = {1, 2, 3};
2529
static Span staticSpan = {staticArr};
2630

@@ -34,49 +38,46 @@ module CxxTest {
3438
//--- use-span.swift
3539
import CxxTest
3640

37-
@_expose(Cxx)
3841
public func createEmptySpan() -> Span {
3942
return Span()
4043
}
4144

42-
@_expose(Cxx)
4345
public func printSpan() {
4446
print("{\(staticSpan[0]), \(staticSpan[1]), \(staticSpan[2])}")
4547
}
4648

47-
@_expose(Cxx)
4849
public func printSpan(_ sp: Span) {
4950
print("{\(sp[0]), \(sp[1]), \(sp[2])}")
5051
}
5152

52-
@_expose(Cxx)
5353
public func printSpanOfString(_ sp: SpanOfString) {
5454
print("{\(sp[0]), \(sp[1]), \(sp[2])}")
5555
}
5656

57-
@_expose(Cxx)
5857
public func passthroughSpan(_ sp: Span) -> Span {
5958
return sp;
6059
}
6160

62-
@_expose(Cxx)
6361
public func changeSpan(_ sp: inout Span) {
6462
sp[0] = 0;
6563
}
6664

67-
@_expose(Cxx)
6865
public func mapSpan(_ sp: Span) {
6966
let result = sp.map { $0 + 3 }
7067
print(result)
7168
}
7269

73-
@_expose(Cxx)
7470
public func receiveArr(_ arr: inout [Int32]) -> Span {
7571
arr.withUnsafeMutableBufferPointer { ubpointer in
7672
return Span(ubpointer);
7773
}
7874
}
7975

76+
public typealias SpanConstUInt8 = ns.SpanOfConstUInt8
77+
78+
public func receiveSpanAlias(_ sp1: SpanConstUInt8, _ sp2: SpanConstUInt8) {
79+
}
80+
8081
//--- use-span.cpp
8182
#include <cassert>
8283
#include "header.h"

0 commit comments

Comments
 (0)