Skip to content

Commit 7db3666

Browse files
committed
[cxx-interop] Fix test/Interop/Cxx/stdlib/use-std-string.swift
Temporarily disable the checks for mutable fields in a C++ struct. A better solution would be to use some heuristic to detect if a method mutates a mutable field, and allow the user to adjust mutability with the existing `nonmutating` annotation. rdar://92621793
1 parent eb8df86 commit 7db3666

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5655,10 +5655,20 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
56555655
}
56565656

56575657
bool ClangImporter::isCXXMethodMutating(const clang::CXXMethodDecl *method) {
5658-
return isa<clang::CXXConstructorDecl>(method) || !method->isConst() ||
5659-
(method->getParent()->hasMutableFields() &&
5660-
!isAnnotatedWith(method, "nonmutating")) ||
5661-
isAnnotatedWith(method, "mutating");
5658+
if (isa<clang::CXXConstructorDecl>(method) || !method->isConst())
5659+
return true;
5660+
if (isAnnotatedWith(method, "mutating"))
5661+
return true;
5662+
if (method->getParent()->hasMutableFields()) {
5663+
if (isAnnotatedWith(method, "nonmutating"))
5664+
return false;
5665+
// FIXME(rdar://91961524): figure out a way to handle mutable fields
5666+
// without breaking classes from the C++ standard library (e.g.
5667+
// `std::string` which has a mutable member in old libstdc++ version used on
5668+
// CentOS 7)
5669+
return false;
5670+
}
5671+
return false;
56625672
}
56635673

56645674
bool ClangImporter::isAnnotatedWith(const clang::CXXMethodDecl *method,

test/Interop/Cxx/class/mutability-annotations-module-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// CHECK: struct HasMutableProperty {
1010
// CHECK: func annotatedNonMutating() -> Int32
11-
// CHECK: mutating func noAnnotation() -> Int32
11+
// TODO-CHECK: mutating func noAnnotation() -> Int32
1212
// CHECK: mutating func contradictingAnnotations() -> Int32
1313
// CHECK: func duplicateAnnotations() -> Int32
1414
// CHECK: var a: Int32

test/Interop/Cxx/class/mutability-annotations-typechecker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ let obj = HasConstMethodAnnotatedAsMutating(a: 42) // expected-note {{change 'le
66
let i = obj.annotatedMutating() // expected-error {{cannot use mutating member on immutable value: 'obj' is a 'let' constant}}
77

88
let objWMutableProperty = HasMutableProperty(a: 42, b: 21) // expected-note {{change 'let' to 'var' to make it mutable}}
9-
// expected-note@-1 {{change 'let' to 'var' to make it mutable}}
9+
// TODO-note@-1 {{change 'let' to 'var' to make it mutable}}
1010

1111
let _ = objWMutableProperty.annotatedNonMutating()
12-
let _ = objWMutableProperty.noAnnotation() // expected-error {{cannot use mutating member on immutable value: 'objWMutableProperty' is a 'let' constant}}
12+
let _ = objWMutableProperty.noAnnotation() // TODO-error {{cannot use mutating member on immutable value: 'objWMutableProperty' is a 'let' constant}}
1313
let _ = objWMutableProperty.contradictingAnnotations() // expected-error {{cannot use mutating member on immutable value: 'objWMutableProperty' is a 'let' constant}}
1414
let _ = objWMutableProperty.duplicateAnnotations()
1515

test/Interop/Cxx/stdlib/use-std-string.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Enable this everywhere once we have a solution for modularizing other C++ stdlibs: rdar://87654514
66
// REQUIRES: OS=macosx || OS=linux-gnu
77

8-
// REQUIRES: rdar92621793
9-
108
import StdlibUnittest
119
import StdString
1210
#if os(Linux)

0 commit comments

Comments
 (0)