Skip to content

Strange behavior with 'any RegexComponent' #84138

@stefanspringer1

Description

@stefanspringer1

Description

With the following code:

func check1(text: String, pattern1: (any RegexComponent)?, pattern2: any RegexComponent) -> Bool {
    let pattern = pattern1 ?? pattern2 // type of 'pattern' is 'any RegexComponent'
    return text.contains(pattern) // OK
}

func check2(text: String, pattern1: (any RegexComponent)?, pattern2: any RegexComponent) -> Bool {
    return text.contains(pattern1 ?? pattern2) // Compiler error: Type 'any RegexComponent' cannot conform to 'StringProtocol'
}

there is the strange compiler message Type 'any RegexComponent' cannot conform to 'StringProtocol' for the second function. It is not clear why this is the case in comparison to the first function, the second function should have not such an issue, at least the compiler message appears to be strange, as in the first function an instance of the same type is passed to String.contains.

Reproduction

Try to compile the above code.

Expected behavior

The second function should compile and work the same as the first function, at least the compiler message could be better so the behaviour does not appear as strange as it now does.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions