-
Notifications
You must be signed in to change notification settings - Fork 10.5k
C++ Interop: support mutating attribute for C++ methods #39999
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_MUTABILITY_ANNOTATIONS_H | ||
#define TEST_INTEROP_CXX_CLASS_INPUTS_MUTABILITY_ANNOTATIONS_H | ||
|
||
struct HasConstMethodAnnotatedAsMutating { | ||
int a; | ||
|
||
int annotatedMutating() const __attribute__((__swift_attr__("mutating"))) { | ||
const_cast<HasConstMethodAnnotatedAsMutating *>(this)->a++; | ||
return a; | ||
} | ||
|
||
int annotatedMutatingWithOtherAttrs() const __attribute__((__swift_attr__("public"))) __attribute__((__swift_attr__("mutating"))) { | ||
const_cast<HasConstMethodAnnotatedAsMutating *>(this)->a++; | ||
return a; | ||
} | ||
}; | ||
|
||
#endif // TEST_INTEROP_CXX_CLASS_INPUTS_MUTABILITY_ANNOTATIONS_H |
7 changes: 7 additions & 0 deletions
7
test/Interop/Cxx/class/mutability-annotations-module-interface.swift
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,7 @@ | ||
// RUN: %target-swift-ide-test -print-module -module-to-print=MutabilityAnnotations -I %S/Inputs -source-filename=x -enable-cxx-interop | %FileCheck %s | ||
|
||
// CHECK: struct HasConstMethodAnnotatedAsMutating { | ||
// CHECK: mutating func annotatedMutating() -> Int32 | ||
egorzhdan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// CHECK: mutating func annotatedMutatingWithOtherAttrs() -> Int32 | ||
// CHECK: var a: Int32 | ||
// CHECK: } |
6 changes: 6 additions & 0 deletions
6
test/Interop/Cxx/class/mutability-annotations-typechecker.swift
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,6 @@ | ||
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-cxx-interop | ||
|
||
import MutabilityAnnotations | ||
|
||
let obj = HasConstMethodAnnotatedAsMutating(a: 42) // expected-note {{change 'let' to 'var' to make it mutable}} | ||
let i = obj.annotatedMutating() // expected-error {{cannot use mutating member on immutable value: 'obj' is a 'let' constant}} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Maybe a test where there are multiple
swift_attr
attributes. Also, you can spell it like this:struct __attribute__((swift_attr("mutating"))) X { };
.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.
Good idea, I added a test.
Hmm, that currently produces
mutating struct X
which doesn't look right, but it also worked this way before this change :)Do you think
mutating
on a struct should apply it to all members methods?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.
Hmm, I don't know. But the test looks good how it is now. I think that's the correct way to write it.