-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Fix metadata mismatch regarding fields of structs #81035
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
#ifndef SWIFT_IRGEN_STRUCTMETADATALAYOUT_H | ||
#define SWIFT_IRGEN_STRUCTMETADATALAYOUT_H | ||
|
||
#include "Field.h" | ||
#include "NominalMetadataVisitor.h" | ||
#include "swift/AST/IRGenOptions.h" | ||
|
||
|
@@ -64,7 +65,8 @@ template <class Impl> class StructMetadataVisitor | |
// Struct field offsets. | ||
asImpl().noteStartOfFieldOffsets(); | ||
for (VarDecl *prop : Target->getStoredProperties()) | ||
asImpl().addFieldOffset(prop); | ||
if (!isPrivateField(prop)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we can get out of sync with |
||
asImpl().addFieldOffset(prop); | ||
|
||
asImpl().noteEndOfFieldOffsets(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_SIMPLE_STRUCTS_H | ||
#define TEST_INTEROP_CXX_CLASS_INPUTS_SIMPLE_STRUCTS_H | ||
|
||
#include <string> | ||
|
||
struct HasPrivateFieldsOnly { | ||
private: | ||
int privInt; | ||
std::string privStr; | ||
|
||
public: | ||
HasPrivateFieldsOnly(int i, std::string s) : privInt(i), privStr(s) {} | ||
}; | ||
|
||
struct HasPublicFieldsOnly { | ||
int pubInt; | ||
std::string pubStr; | ||
|
||
HasPublicFieldsOnly(int i, std::string s) : pubInt(i), pubStr(s) {} | ||
}; | ||
|
||
struct HasIntFieldsOnly { | ||
private: | ||
int a = 1; | ||
|
||
public: | ||
int b = 2; | ||
|
||
private: | ||
int c = 3; | ||
|
||
public: | ||
int d = 4; | ||
}; | ||
|
||
struct HasPrivateAndPublicFields { | ||
private: | ||
std::string privStr; | ||
|
||
public: | ||
int pubInt; | ||
|
||
private: | ||
int privInt; | ||
|
||
public: | ||
std::string pubStr; | ||
|
||
HasPrivateAndPublicFields(int i1, int i2, std::string s1, std::string s2) | ||
: privStr(s2), pubInt(i2), privInt(i1), pubStr(s1) {} | ||
}; | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-build-swift %s -o %t/a.out -Xfrontend -enable-experimental-cxx-interop -I %S/Inputs | ||
// RUN: %target-codesign %t/a.out | ||
// RUN: %target-run %t/a.out | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-os | ||
|
||
// REQUIRES: executable_test | ||
|
||
import SimpleStructs | ||
|
||
func printCxxStructPrivateFields() { | ||
let s = HasPrivateFieldsOnly(42, "Hello") | ||
print(s) | ||
} | ||
|
||
func printCxxStructPublicFields() { | ||
let s = HasPublicFieldsOnly(42, "Hello") | ||
print(s) | ||
} | ||
|
||
func printCxxStructIntFields() { | ||
let s = HasIntFieldsOnly() | ||
print(s) | ||
} | ||
|
||
func printCxxStructPrivateAndPublicFields() { | ||
let s = HasPrivateAndPublicFields(24, 42, "Hello", "World") | ||
print(s) | ||
} | ||
|
||
printCxxStructPrivateFields() | ||
// CHECK: HasPrivateFieldsOnly() | ||
|
||
printCxxStructIntFields() | ||
// CHECK: HasIntFieldsOnly(b: 2, d: 4) | ||
|
||
printCxxStructPublicFields() | ||
// CHECK-macosx: HasPublicFieldsOnly(pubInt: 42, pubStr: __C.std.std::__1.basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>()) | ||
// CHECK-linux-gnu: HasPublicFieldsOnly(pubInt: 42, pubStr: __C.std.std::__cxx11.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>()) | ||
// CHECK-windows-msvc: HasPublicFieldsOnly(pubInt: 42, pubStr: __C.std.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>()) | ||
|
||
printCxxStructPrivateAndPublicFields() | ||
// CHECK-macosx: HasPrivateAndPublicFields(pubInt: 42, pubStr: __C.std.std::__1.basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>()) | ||
// CHECK-linux-gnu: HasPrivateAndPublicFields(pubInt: 42, pubStr: __C.std.std::__cxx11.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>()) | ||
// CHECK-windows-msvc: HasPrivateAndPublicFields(pubInt: 42, pubStr: __C.std.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_STD_TYPES_H | ||
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_TYPES_H | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
std::unique_ptr<int> makeInt() { return std::make_unique<int>(42); } | ||
|
||
std::unique_ptr<std::string> makeString() { | ||
return std::make_unique<std::string>("Unique string"); | ||
} | ||
|
||
std::shared_ptr<int> makeIntShared() { return std::make_unique<int>(42); } | ||
|
||
std::shared_ptr<std::string> makeStringShared() { | ||
return std::make_unique<std::string>("Shared string"); | ||
} | ||
|
||
std::vector<int> makeVecOfInt() { return {1, 2, 3}; } | ||
|
||
std::vector<std::string> makeVecOfString() { return {"Hello", "World"}; } | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-build-swift %s -o %t/a.out -Xfrontend -enable-experimental-cxx-interop -I %S/Inputs | ||
// RUN: %target-codesign %t/a.out | ||
// RUN: %target-run %t/a.out | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-os | ||
|
||
// REQUIRES: executable_test | ||
|
||
import StdlibTypes | ||
|
||
func printStdString(s: std.string) { | ||
print(s) | ||
let swiftString = String(s) | ||
print(swiftString) | ||
} | ||
|
||
#if !os(Windows) | ||
// FIXME: We can't import std::unique_ptr or std::shared_ptr properly on Windows (https://github.com/apple/swift/issues/70226) | ||
func printStdUniquePtrOfInt() { | ||
let uint = makeInt() | ||
print(uint.pointee) | ||
} | ||
|
||
func printStdUniquePtrOfString() { | ||
let ustring = makeString() | ||
print(ustring.pointee) | ||
} | ||
|
||
func printStdSharedPtrOfInt() { | ||
let sint = makeIntShared() | ||
print(sint.pointee) | ||
print(sint) | ||
} | ||
|
||
func printStdSharedPtrOfString() { | ||
let sstring = makeStringShared() | ||
print(sstring.pointee) | ||
print(sstring) | ||
} | ||
#endif | ||
|
||
func printStdVectorOfInt() { | ||
let vecOfInt = makeVecOfInt() | ||
print(vecOfInt[0]) | ||
print(vecOfInt) | ||
} | ||
|
||
func printStdVectorOfString() { | ||
let vecOfString = makeVecOfString() | ||
print(vecOfString[0]) | ||
print(vecOfString) | ||
} | ||
|
||
printStdString(s: "Hello") | ||
// CHECK-macosx: basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>() | ||
// CHECK-linux-gnu: basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>() | ||
// CHECK-windows-msvc: basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>() | ||
// CHECK: Hello | ||
|
||
#if !os(Windows) | ||
printStdUniquePtrOfInt() | ||
// CHECK-macosx: 42 | ||
// CHECK-linux-gnu: 42 | ||
printStdUniquePtrOfString() | ||
// CHECK-macosx: basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>() | ||
// CHECK-linux-gnu: basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>() | ||
printStdSharedPtrOfInt() | ||
// CHECK-macosx: 42 | ||
// CHECK-macosx: shared_ptr<CInt>() | ||
// CHECK-linux-gnu: 42 | ||
// CHECK-linux-gnu: shared_ptr<CInt>() | ||
printStdSharedPtrOfString() | ||
// CHECK-macosx: basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, you could add regexes that match on all platforms. I don't have a preference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, the string one is temporary (maybe not the shared_ptr one though). Let's leave it as is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right! |
||
// CHECK-macosx: shared_ptr<std.__1.basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>>() | ||
// CHECK-linux-gnu: basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>() | ||
// CHECK-linux-gnu: shared_ptr<std.__cxx11.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>>() | ||
|
||
#endif | ||
|
||
printStdVectorOfInt() | ||
// CHECK: 1 | ||
// CHECK-macosx: vector<CInt, std.__1.allocator<CInt>>() | ||
// CHECK-linux-gnu: vector<CInt, std.allocator<CInt>>() | ||
// CHECK-windows-msvc: vector<CInt, std.allocator<CInt>>() | ||
printStdVectorOfString() | ||
// CHECK-macosx: basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>() | ||
// CHECK-macosx: vector<std.__1.basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>, std.__1.allocator<std.__1.basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>>>() | ||
// CHECK-linux-gnu: basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>() | ||
// CHECK-linux-gnu: vector<std.__cxx11.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>, std.allocator<std.__cxx11.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>>>() | ||
// CHECK-windows-msvc: basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>() | ||
// CHECK-windows-msvc: vector<std.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>, std.allocator<std.basic_string<CChar, std.char_traits<CChar>, std.allocator<CChar>>>>() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we need to do something similar for
ClassContextDescriptorBuilder
too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am less sure about
emitInitializeFieldOffsetVector
and co. Maybe those are good as is but maybe we want to ask someone familiar with type metadata.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think you're right. I intend to check if we need a similar fix for classes and add those changes in a future PR
Regarding
emitInitializeFieldOffsetVector
, I'm also not sure. I'll investigate this further