Skip to content

Unexpected compiler diagnostics when checking async Bools with Swift Testing’s #expect #1128

Closed as duplicate of#162
@yakovmanshin

Description

@yakovmanshin

Description

When Swift Testing’s #expect macro is used to check actors’ Bool values (accessed asynchronously), seemingly equivalent expressions (isEnabled vs. isEnabled == true) result in unexpected—inconsistent, misleading, or missing—compiler diagnostics.

Reported in swiftlang/swift#81765.

Reproduction

import Testing

actor Service {
    var isEnabled = true
}

struct ServiceTests {
    
    let service = Service()
    
    @Test func isEnabled1() async {
        // No inline warnings or errors;
        // Errors on build: “(…) can not be referenced from a nonisolated context”:
        #expect(service.isEnabled)
        #expect(!service.isEnabled)
        
        // These build fine (no warnings or errors, inline or build-time):
        #expect(await service.isEnabled)
        #expect(await !service.isEnabled)
        #expect(!(await service.isEnabled))
        
        // Misleading inline warnings: “No 'async' operations occur within 'await' expression”;
        // Errors on build: “(…) can not be referenced from a nonisolated context”:
        await #expect(service.isEnabled)
        await #expect(!service.isEnabled)
    }
    
    // With explicit equality checks, the compiler produces more reasonable diagnostics:
    @Test func isEnabled2() async {
        // Inline errors “Expression is 'async' (…)"; suggest fix-its:
        #expect(service.isEnabled == true)
        #expect(service.isEnabled != true)
        #expect(service.isEnabled == false)
        
        // The fix-its hoist `await`s, resulting in:
        await #expect(service.isEnabled == true)
        await #expect(service.isEnabled != true)
        await #expect(service.isEnabled == false)
        
        // But these work too:
        #expect(await service.isEnabled == true)
        #expect((await service.isEnabled) == true)
        #expect(await service.isEnabled != true)
        #expect((await service.isEnabled) != true)
        #expect(await service.isEnabled == false)
        #expect((await service.isEnabled) == false)
    }
    
}

Expected behavior

The plain isEnabled checks produce the same (correct) diagnostics as the isEnabled == true expressions.

Environment

  • macOS 15.5 (24F74)
  • Xcode 16.4 (16F6)
  • Swift 6.1
swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: x86_64-apple-macosx15.0

Additional information

This issue is also reported in the Swift compiler repo: swiftlang/swift#81765.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug🪲 Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions