Please, help identify the appropriate place to report this issue.
Is the issue in fact a bug? Is it a bug with Swift 6.1, a bug in Xcode-beta, or a macro bug? The issue can allow tests to pass when they should fail.
import Testing
// Xcode Version 16.3 beta 2 (16E5121h)
// Apple Swift version 6.1 (swiftlang-6.1.0.109.103 clang-1700.0.13.2)
// Target: arm64-apple-macosx15.0
@Test
func invalidDeclarationErrorSuppressed() {
protocol Foo<Bar> {
associatedtype Bar
var bar: Bar { get }
}
struct Conforms: Foo {
let bar: Int = 0
}
let explicit: Conforms = Conforms()
#expect((explicit as Foo<Int>).bar != explicit.bar)
// Using `any` the test correcty fails
#expect((explicit as any Foo<Int>).bar != explicit.bar)
// Cannot reference invalid declaration, un-comment code below to get compiler error
// let referece = (explicit as Foo<Int>)
// if referece.bar != explicit.bar {
// #expect(Bool(false))
// }
}
I ran into the issue while trying to specifically test code in protocol extensions. I also want to learn why the error is suppressed in the macro. The compiler warns to use any, and informs of a future error without. Ignoring the warning, the code compiles and runs. The test passes when it should not.
Is it an issue with the macro, or specifically the expect macro, that suppresses the error referencing an invalid declaration? Or, is it an issue with the compiler or Xcode?
If this is a known issue, please forgive the redundant question.
I am not sure what issue you're seeing. The code you've shared works as I'd expect using a current Swift compiler: the first #expect() invocation fails to compile due to a missing any, while the second compiles and runs correctly. The third one (commented out) also fails to compile due to a missing any.
If you're able to consistently reproduce the issue with Swift 6.1, please file a bug report against the Swift compiler and be as clear as you can about the issue you're seeing. Thanks!
My uneducated guess would be that the wrong overload of != is being selected by the type checker after macro expansion. Please file a bug against the Swift compiler. Thanks!
I have confirmed the issue is not specific to Testing as you surmised. The issue can be reproduced with a simple freestanding macro that produces a simple print statement. Inserting that expression ((explicit as Foo<Int>).bar == explicit.bar), will prevent the simple macro accepting a single Bool argument from returning the ExprSyntax with the print statement.
Of interest, though, testing the code in a fashion similar to SwiftSyntaxMacroExpansionTest and swift-testing's ../TestSupport/Parse.swift.parse, the macro expands. So this error seems to bypass current macro testing protocols. If you know of a way to capture the appropriate context, please advise.