@@ -8768,12 +8768,13 @@ SourceFile &ClangImporter::Implementation::getClangSwiftAttrSourceFile(
87688768}
87698769
87708770bool swift::importer::isMainActorAttr (const clang::SwiftAttrAttr *swiftAttr) {
8771- if (swiftAttr->getAttribute () == " @MainActor" ||
8772- swiftAttr->getAttribute () == " @UIActor" ) {
8773- return true ;
8774- }
8771+ return swiftAttr->getAttribute () == " @MainActor" ||
8772+ swiftAttr->getAttribute () == " @UIActor" ;
8773+ }
87758774
8776- return false ;
8775+ bool swift::importer::isMutabilityAttr (const clang::SwiftAttrAttr *swiftAttr) {
8776+ return swiftAttr->getAttribute () == " mutating" ||
8777+ swiftAttr->getAttribute () == " nonmutating" ;
87778778}
87788779
87798780void
@@ -8792,7 +8793,8 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
87928793 if (maybeDefinition.getValue ())
87938794 ClangDecl = cast<clang::NamedDecl>(maybeDefinition.getValue ());
87948795
8795- Optional<const clang::SwiftAttrAttr *> SeenMainActorAttr;
8796+ Optional<const clang::SwiftAttrAttr *> seenMainActorAttr;
8797+ Optional<const clang::SwiftAttrAttr *> seenMutabilityAttr;
87968798 PatternBindingInitializer *initContext = nullptr ;
87978799
87988800 auto importAttrsFromDecl = [&](const clang::NamedDecl *ClangDecl) {
@@ -8803,26 +8805,47 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
88038805 // FIXME: Hard-code @MainActor and @UIActor, because we don't have a
88048806 // point at which to do name lookup for imported entities.
88058807 if (isMainActorAttr (swiftAttr)) {
8806- if (SeenMainActorAttr ) {
8808+ if (seenMainActorAttr ) {
88078809 // Cannot add main actor annotation twice. We'll keep the first
88088810 // one and raise a warning about the duplicate.
88098811 HeaderLoc attrLoc (swiftAttr->getLocation ());
88108812 diagnose (attrLoc, diag::import_multiple_mainactor_attr,
88118813 swiftAttr->getAttribute (),
8812- SeenMainActorAttr .getValue ()->getAttribute ());
8814+ seenMainActorAttr .getValue ()->getAttribute ());
88138815 continue ;
88148816 }
88158817
88168818 if (Type mainActorType = SwiftContext.getMainActorType ()) {
88178819 auto typeExpr = TypeExpr::createImplicit (mainActorType, SwiftContext);
88188820 auto attr = CustomAttr::create (SwiftContext, SourceLoc (), typeExpr);
88198821 MappedDecl->getAttrs ().add (attr);
8820- SeenMainActorAttr = swiftAttr;
8822+ seenMainActorAttr = swiftAttr;
88218823 }
88228824
88238825 continue ;
88248826 }
88258827
8828+ if (isMutabilityAttr (swiftAttr)) {
8829+ if (seenMutabilityAttr) {
8830+ StringRef seenAttribute =
8831+ seenMutabilityAttr.getValue ()->getAttribute ();
8832+ if ((seenAttribute == " nonmutating" &&
8833+ swiftAttr->getAttribute () == " mutating" ) ||
8834+ (seenAttribute == " mutating" &&
8835+ swiftAttr->getAttribute () == " nonmutating" )) {
8836+ const clang::SwiftAttrAttr *nonmutatingAttr =
8837+ seenAttribute == " nonmutating" ? seenMutabilityAttr.getValue ()
8838+ : swiftAttr;
8839+
8840+ diagnose (HeaderLoc (nonmutatingAttr->getLocation ()),
8841+ diag::contradicting_mutation_attrs);
8842+ continue ;
8843+ }
8844+ }
8845+
8846+ seenMutabilityAttr = swiftAttr;
8847+ }
8848+
88268849 // Hard-code @actorIndependent, until Objective-C clients start
88278850 // using nonisolated.
88288851 if (swiftAttr->getAttribute () == " @actorIndependent" ) {
0 commit comments