Skip to content

Two @Test funcs with the same name crashes the compiler #82631

Open
@ole

Description

@ole

Motivation

Create a Swift Testing target and define 2 test functions. Give both functions the same name:

import Testing

@Test func someTest() {}

@Test func someTest() {}

Expected behavior:

A nice compiler error of the form "Invalid redeclaration of 'someTest`

Observed behavior:

The compiler crashes during macro expansion.

$ swift build --build-tests
Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocation)
macro expansion @Test:2:25: error: invalid redeclaration of '$s41SwiftTestingTwoTestFuncsWithSameNameTests04someD00D0fMp_8f5bed0c2fMu_()'
`- …/SwiftTestingTwoTestFuncsWithSameNameTests.swift:4:25: note: expanded code originates here
2 | import SwiftTestingTwoTestFuncsWithSameName
3 |
4 | @Test func someTest() {}
  +--- macro expansion @Test -------------------------------------------
  | 1 | @available(*, deprecated, message: "This function is an implementation detail of the testing library. Do not use it directly.")


macro expansion @Test:10:6: error: invalid redeclaration of '$s41SwiftTestingTwoTestFuncsWithSameNameTests04someD00D0fMp_38__🟠$test_container__function__f5bed0c2fMu_'
`- …/SwiftTestingTwoTestFuncsWithSameNameTests.swift:4:25: note: expanded code originates here

When running the tests in Xcode, you see "Command SwiftCompile failed with a nonzero exit code" and need to dig into the build log to discover the actual issue, which isn't a good user experience.

Proposed solution

It would be nice to have a better diagnostic for this, especially because I believe this is a fairly common error to make when copy-pasting an existing test function.

Alternatives considered

No response

Additional information

I tested this on macOS 15.5 with Swift 6.1 (Xcode 16.4) and Swift 6.2 beta (Xcode 26.0 beta 2). The result is the same in both cases, except that Swift 6.2 also prints a stack trace, presumably because it's built with more assertions enabled?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.swift macroFeature → declarations: Swift `macro` declarations

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions