Skip to content

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 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4351,7 +4351,14 @@ ClangImporter::instantiateCXXClassTemplate(

bool ClangImporter::isCXXMethodMutating(const clang::CXXMethodDecl *method) {
return isa<clang::CXXConstructorDecl>(method) || !method->isConst() ||
method->getParent()->hasMutableFields();
method->getParent()->hasMutableFields() ||
(method->hasAttrs() &&
llvm::any_of(method->getAttrs(), [](clang::Attr *a) {
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(a)) {
return swiftAttr->getAttribute() == "mutating";
}
return false;
}));
}

SwiftLookupTable *
Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/class/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ module MutableMembers {
requires cplusplus
}

module MutabilityAnnotations {
header "mutability-annotations.h"
requires cplusplus
}

module ProtocolConformance {
header "protocol-conformance.h"
requires cplusplus
Expand Down
18 changes: 18 additions & 0 deletions test/Interop/Cxx/class/Inputs/mutability-annotations.h
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"))) {
Copy link
Contributor

@zoecarver zoecarver Nov 1, 2021

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 { };.

Copy link
Contributor Author

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

Good idea, I added a test.

Also, you can spell it like this: struct __attribute__((swift_attr("mutating"))) X { };.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think mutating on a struct should apply it to all members methods?

Hmm, I don't know. But the test looks good how it is now. I think that's the correct way to write it.

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
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
// CHECK: mutating func annotatedMutatingWithOtherAttrs() -> Int32
// CHECK: var a: Int32
// CHECK: }
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}}