-
Notifications
You must be signed in to change notification settings - Fork 10.5k
🍒[cxx-interop] Fix metadata mismatch regarding fields of structs #81740
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
susmonteiro
merged 1 commit into
release/6.2
from
susmonteiro/6.2-metadata-private-fields
May 27, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_SIMPLE_STRUCTS_H | ||
#define TEST_INTEROP_CXX_CLASS_INPUTS_SIMPLE_STRUCTS_H | ||
|
||
struct HasPrivateFieldsOnly { | ||
private: | ||
int priv1; | ||
int priv2; | ||
|
||
public: | ||
HasPrivateFieldsOnly(int i1, int i2) : priv1(i1), priv2(i2) {} | ||
}; | ||
|
||
struct HasPublicFieldsOnly { | ||
int publ1; | ||
int publ2; | ||
|
||
HasPublicFieldsOnly(int i1, int i2) : publ1(i1), publ2(i2) {} | ||
}; | ||
|
||
struct HasPrivatePublicProtectedFields { | ||
private: | ||
int priv1; | ||
|
||
public: | ||
int publ1; | ||
|
||
protected: | ||
int prot1; | ||
|
||
protected: | ||
int prot2; | ||
|
||
private: | ||
int priv2; | ||
|
||
public: | ||
int publ2; | ||
|
||
HasPrivatePublicProtectedFields(int i1, int i2, int i3, int i4, int i5, | ||
int i6) | ||
: priv1(i1), publ1(i2), prot1(i3), prot2(i4), priv2(i5), | ||
publ2(i6) {} | ||
}; | ||
|
||
struct Outer { | ||
private: | ||
HasPrivatePublicProtectedFields privStruct; | ||
|
||
public: | ||
HasPrivatePublicProtectedFields publStruct; | ||
|
||
Outer() : privStruct(1, 2, 3, 4, 5, 6), publStruct(7, 8, 9, 10, 11, 12) {} | ||
}; | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// RUN: %target-run-simple-swift(-cxx-interoperability-mode=default -Xfrontend -disable-availability-checking -I %S/Inputs) | ||
|
||
// REQUIRES: executable_test | ||
// REQUIRES: reflection | ||
|
||
@_spi(Reflection) import Swift | ||
import SimpleStructs | ||
import StdlibUnittest | ||
|
||
func checkFieldsWithKeyPath<T>( | ||
of type: T.Type, | ||
options: _EachFieldOptions = [], | ||
fields: [String: PartialKeyPath<T>] | ||
) { | ||
var count = 0 | ||
|
||
_forEachFieldWithKeyPath(of: T.self, options: options) { | ||
charPtr, keyPath in | ||
count += 1 | ||
|
||
let fieldName = String(cString: charPtr) | ||
if fieldName == "" { | ||
expectTrue(false, "Empty field name") | ||
return true | ||
} | ||
guard let checkKeyPath = fields[fieldName] else { | ||
expectTrue(false, "Unexpected field '\(fieldName)'") | ||
return true | ||
} | ||
|
||
expectTrue(checkKeyPath == keyPath) | ||
return true | ||
} | ||
|
||
expectEqual(fields.count, count) | ||
} | ||
|
||
var ForEachFieldTestSuite = TestSuite("ForEachField") | ||
|
||
ForEachFieldTestSuite.test("HasPrivateFieldsOnly") { | ||
checkFieldsWithKeyPath( | ||
of: HasPrivateFieldsOnly.self, | ||
fields: [:] | ||
) | ||
} | ||
|
||
ForEachFieldTestSuite.test("HasPublicFieldsOnly") { | ||
checkFieldsWithKeyPath( | ||
of: HasPublicFieldsOnly.self, | ||
fields: [ | ||
"publ1": \HasPublicFieldsOnly.publ1, | ||
"publ2": \HasPublicFieldsOnly.publ2 | ||
] | ||
) | ||
} | ||
|
||
ForEachFieldTestSuite.test("HasPrivatePublicProtectedFields") { | ||
checkFieldsWithKeyPath( | ||
of: HasPrivatePublicProtectedFields.self, | ||
fields: [ | ||
"publ1": \HasPrivatePublicProtectedFields.publ1, | ||
"publ2": \HasPrivatePublicProtectedFields.publ2 | ||
] | ||
) | ||
} | ||
|
||
ForEachFieldTestSuite.test("Outer") { | ||
checkFieldsWithKeyPath( | ||
of: Outer.self, | ||
fields: [ | ||
"publStruct": \Outer.publStruct | ||
] | ||
) | ||
} | ||
|
||
runAllTests() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// RUN: %target-run-simple-swift(-cxx-interoperability-mode=default -Xfrontend -disable-availability-checking -I %S/Inputs) | %FileCheck %s | ||
|
||
// REQUIRES: executable_test | ||
|
||
import SimpleStructs | ||
|
||
func printCxxStructPrivateFields() { | ||
let s = HasPrivateFieldsOnly(1, 2) | ||
print(s) | ||
} | ||
|
||
func printCxxStructPublicFields() { | ||
let s = HasPublicFieldsOnly(1, 2) | ||
print(s) | ||
} | ||
|
||
func printCxxStructPrivatePublicProtectedFields() { | ||
let s = HasPrivatePublicProtectedFields(1, 2, 3, 4, 5, 6) | ||
print(s) | ||
} | ||
|
||
func printCxxStructNested() { | ||
let s = Outer() | ||
print(s) | ||
} | ||
|
||
printCxxStructPrivateFields() | ||
// CHECK: HasPrivateFieldsOnly() | ||
|
||
printCxxStructPublicFields() | ||
// CHECK: HasPublicFieldsOnly(publ1: 1, publ2: 2) | ||
|
||
printCxxStructPrivatePublicProtectedFields() | ||
// CHECK: HasPrivatePublicProtectedFields(publ1: 2, publ2: 6) | ||
|
||
printCxxStructNested() | ||
// CHECK: Outer(publStruct: {{.*}}.HasPrivatePublicProtectedFields(publ1: 8, publ2: 12)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd prefer if this check went through the same function as isExportableField to determine whether the particular VarDecl is exported. It doesn't matter so much on the 6.2 branch, but I can imagine us tuning this predicate in the future, and it would be best to have it in a single place.
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.
Thanks, I'll make this change