Add cross-module tests for default isolation behavior

This commit is contained in:
Doug Gregor
2025-07-25 13:33:12 -07:00
parent 9f7dff0417
commit d4f0efc217
2 changed files with 42 additions and 3 deletions

View File

@@ -0,0 +1,7 @@
public protocol ImportedSendableProto: Sendable { }
@MainActor public struct ImportedStruct: ImportedSendableProto { }
public struct ImportedOtherStruct { }
public protocol ImportedP { }

View File

@@ -1,11 +1,15 @@
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift5-
// RUN: %target-swift-frontend -swift-version 6 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift6-
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -swift-version 6 -module-name implicit_nonisolated_things -o %t/implicit_nonisolated_things.swiftmodule %S/Inputs/implicit_nonisolated_things.swift
// RUN: %target-swift-frontend -I %t -swift-version 5 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift5-
// RUN: %target-swift-frontend -I %t -swift-version 6 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift6-
// READ THIS! This test is meant to check the specific isolation when
// `-default-isolation` is set to `MainActor` in combination with validating
// behavior around explicitly non-Sendable types that trigger type checker
// specific errors. Please do not put other types of tests in here.
import implicit_nonisolated_things
// Fake Sendable Data
class SendableData : @unchecked Sendable {}
// expected-swift5-note@-1 {{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
@@ -82,7 +86,7 @@ func testTaskDetached() async {
// @MainActor
extension Int {
func memberOfInt() { } // expected-note 2{{calls to instance method 'memberOfInt()' from outside of its actor context are implicitly asynchronous}}
func memberOfInt() { } // expected-note 3{{calls to instance method 'memberOfInt()' from outside of its actor context are implicitly asynchronous}}
}
nonisolated func testMemberOfInt(i: Int) {
@@ -127,6 +131,34 @@ struct MyP: P {
}
}
// Above tests for imported types
extension ImportedStruct: @retroactive CustomStringConvertible {
public var description: String {
17.memberOfInt() // okay, on main actor
return "hello"
}
}
extension ImportedOtherStruct {
func f() {
17.memberOfInt() // okay, on main actor
}
}
nonisolated
extension ImportedOtherStruct {
func g() {
17.memberOfInt() // expected-swift5-warning{{call to main actor-isolated instance method 'memberOfInt()' in a synchronous nonisolated context}}
// expected-swift6-error@-1{{call to main actor-isolated instance method 'memberOfInt()' in a synchronous nonisolated context}}
}
}
struct MyImportedP: ImportedP {
func g() {
17.memberOfInt() // okay, on main actor
}
}
// https://github.com/swiftlang/swift/issues/82168 -
nonisolated protocol OtherP {
associatedtype AT