Skip to content

Expectation containing mutating method fails to build #1360

@orchetect

Description

@orchetect

Description

When using the #expect macro to evaluate a Bool result of a mutating method, the test fails to compile, citing this compiler error:

Cannot use mutating member on immutable value: '$0' is immutable

Reproduction

Example of broken compilation:

struct Bar {
    mutating func update() -> Bool { true }
}

@Test func foo() {
    var bar = Bar()
    #expect(bar.update()) // fails to compile
}

The macro expansion results in this output:

Testing.__checkFunctionCall(bar.self,calling: {
  $0.update()
},expression: .__fromFunctionCall(.__fromSyntaxNode("bar"),"update"),comments: [],isRequired: false,sourceLocation: Testing.SourceLocation.__here()).__expected()

Expected behavior

It is intuitive and idiomatic to expect that calling a mutating method within a testing expectation should compile and work as intended, while being able to assert the boolean return value of the method.

Environment

This issue is present in Xcode 26.0 (public release).

swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: x86_64-apple-macosx15.0

Darwin Rig.local 24.6.0 Darwin Kernel Version 24.6.0: Mon Aug 11 21:16:05 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_X86_64 x86_64

Additional information

A workaround that does compile and evaluates successfully is possible by using the following syntax:

@Test func foo() {
    var bar = Bar()
    #expect(bar.update() == true) // works
}

The macro expansion results in this output:

Testing.__checkBinaryOperation(bar.update(),{ $0 == $1() },true,expression: .__fromBinaryOperation(.__fromSyntaxNode("bar.update()"),"==",.__fromSyntaxNode("true")),comments: [],isRequired: false,sourceLocation: Testing.SourceLocation.__here()).__expected()

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