-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Prevent Swift from importing fields marked with [[no_unique_address]] #80497
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
Closed
+51
−4
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop | ||
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop -verify-additional-file %S/Inputs/member-variables.h | ||
|
||
import MemberVariables | ||
|
||
var s = MyClass() | ||
s.const_member = 42 // expected-error {{cannot assign to property: 'const_member' setter is inaccessible}} | ||
var s1 = MyClass() | ||
s1.const_member = 42 // expected-error {{cannot assign to property: 'const_member' setter is inaccessible}} | ||
|
||
// TODO: rdar://148437848 Swift doesn't support fields marked with [[no_unique_address]] | ||
var s2 = HasNoUniqueAddressField() | ||
_ = s2.simpleField.const_member | ||
_ = s2.noUniqueAddressField.const_member // expected-error {{value of type 'HasNoUniqueAddressField' has no member 'noUniqueAddressField'}} |
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,18 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-build-swift %s -o %t/a.out -Xfrontend -enable-experimental-cxx-interop | ||
// RUN: %target-codesign %t/a.out | ||
// RUN: %target-run %t/a.out | %FileCheck %s | ||
|
||
// REQUIRES: executable_test | ||
|
||
import CxxStdlib | ||
|
||
func printString(s: std.string) { | ||
print(s) | ||
let swiftString = String(s) | ||
print(swiftString) | ||
} | ||
|
||
printString(s: "Hello") | ||
// CHECK: basic_string<CChar, std.__1.char_traits<CChar>, std.__1.allocator<CChar>>() | ||
// CHECK: Hello |
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.
Is
NoUniqueAddressAttr
the source of the problem or is it something else e.g., the field having0
as its size? In case it is the latter we might want to make this workaround more targeted.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 think the problem is that we are incorrectly handling fields with the
no_unique_address
attribute, and consequently the swift runtime tries to access fields ofstd::string
that were not imported, resulting in a runtime crash. We eventually want to correctly handle this attribute, but the impact of the bug required a quicker fix.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.
Ah, let me elaborate a bit. According to CppReference:
So this attribute can have two effects:
If we know exactly which one of this is problematic, we could make this check more targeted. E.g., if we only have trouble with empty fields we can check for the size instead of the presence of the attribute, so we end up rejecting less code.
That being said, I am OK with landing this as is and doing the rest of the investigation as a follow-up.
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.
This makes sense to me. I can for sure investigate this further and, if possible, refine this workaround. However, if you agree, I would merge this patch and open a new one when we have a better fix.
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.
Sounds good!