Skip to content

Swift will not infer opaque return type of generic function #73449

@yosshi4486

Description

@yosshi4486

Description

No response

Reproduction

NG Pattern

When using an opaque type as the return-value, the compiler says "Type 'Impl' does not conform to protocol 'X'".

import SwiftUI

protocol X {
    associatedtype Body: View
    func doSomething<T>(parameter: T) -> Body
}

struct Impl: X {
    // Type 'Impl' does not conform to protocol 'X'
    func doSomething<T>(parameter: T) -> some View {
        Text("Problem Occur")
    }
}

OK Pattern

When using a concrete type as the return-value, the compiler is silent.

import SwiftUI

protocol X {
    associatedtype Body: View
    func doSomething<T>(parameter: T) -> Body
}

struct Impl: X {
    func doSomething<T>(parameter: T) -> Text {
        Text("Problem Occur")
    }
}

Expected behavior

Impl conform X even when it returns some View, and the compiler says nothing.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

Xcode 15.3 (15E204a)
macOS 14.4.1(23E224)

swift-forum discussion: https://forums.swift.org/t/swift-will-not-infer-opaque-return-type-of-generic-function/67233

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesgenericsFeature: generic declarations and typesnot a bugResolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer erroropaque result typesFeature → types → opaque types: opaque result typesopaque typesFeature → types: opaque typesswift 5.10type checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions