Skip to content

[SR-8705] Attempt to use same type requirement in the method description causes compiler error #51217

@swift-ci

Description

@swift-ci
Previous ID SR-8705
Radar None
Original Reporter spiceginger (JIRA User)
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AssociatedTypeInference
Assignee None
Priority Medium

md5: 8df8fbcda4ed1cc019b6f866dad7f962

Issue Description:

import UIKit

protocol Factory {
    
    associatedtype ViewController: UIViewController
    
    associatedtype Context

    func build(with context: Context) -> ViewController

}

struct NavigationFactory: Factory {
    
    func build(with context: Any?) -> UINavigationController {
        return UINavigationController(nibName: nil, bundle: nil)
    }
    
}

struct Duplicator<VC: UIViewController, C>: Factory {
    typealias ViewController = VC
    typealias Context = C
    func build(with context: Context) -> ViewController {
        return ViewController(nibName: nil, bundle: nil)
    }
}

struct MergerWorks<F1: Factory, F2: Factory> where F1.ViewController == F2.ViewController, F1.Context == F2.Context {
    let vc1: UIViewController
    let vc2: UIViewController
    init(factory1: F1, factory2:F2, context: F1.Context) {
        self.vc1 = factory1.build(with: context)
        self.vc2 = factory2.build(with: context)
    }
}

struct MergerBroken  {
    let vc1: UIViewController
    let vc2: UIViewController
    init<F1: Factory, F2: Factory>(factory1: F1, factory2:F2, context: F1.Context) where F1.ViewController == F2.ViewController, F1.Context == F2.Context {
        self.vc1 = factory1.build(with: context)
        self.vc2 = factory2.build(with: context)
    }
    
}

let mergerWorks = MergerWorks(factory1: NavigationFactory(), factory2: Duplicator(), context: nil)
let mergerBroken = MergerBroken(factory1: NavigationFactory(), factory2: Duplicator(), context: nil) // Causes error: error: generic parameter 'C' could not be inferred, note: explicitly specify the generic arguments to fix this issue

Implicitly specifying the type of `Duplicator` helps:

let mergerBroken = MergerBroken(factory1: NavigationFactory(), factory2: Duplicator<UINavigationController, Any?>(), context: nil)

But expected the same behaviour as in MergerWorks where compiler is able to resolve the type of Duplicator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfgeneric constraintsFeature → generics: generic constraintsgenericsFeature: generic declarations and typestype checkerArea → compiler: Semantic analysistype inferenceFeature: type inferenceunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions