Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3694] Generic parameter 'T' could not be inferred #46279

Closed
krzyzanowskim opened this issue Jan 22, 2017 · 3 comments
Closed

[SR-3694] Generic parameter 'T' could not be inferred #46279

krzyzanowskim opened this issue Jan 22, 2017 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@krzyzanowskim
Copy link
Contributor

Previous ID SR-3694
Radar None
Original Reporter @krzyzanowskim
Type Bug
Status Resolved
Resolution Invalid
Environment

Swift 3.0.2

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

md5: c83c6d2f3fcfe12ce96731dda60215a4

Issue Description:

For the given generic code, the compiler reports an error that type could not be inferred. It looks to me that constraints are set to "Collection" everywhere and inferred type should be ArraySlice<UInt8>. It should compile and run without the problem.

error: generic parameter 'T' could not be inferred
        execute(withCollection: foo[2])
func execute<T: Collection>(withCollection: T) where T.Index == Int, T.SubSequence: Collection { 
}

struct Foo<Base: Collection> where Base.Index == Int, Base.SubSequence: Collection {
    let base: Base
    
    subscript(idx: Int) -> Base.SubSequence {
        return base[0..<idx]
    }
}

class Bar {
    func run<C: Collection>(_ arr: C) where C.Index == Int, C.SubSequence: Collection {
        let foo = Foo(base: arr)

        print(type(of: foo[2])) // ArraySlice<UInt8>
        execute(withCollection: foo[2]) // error: generic parameter 'T' could not be inferred
        // foo[2] is a Collection because Foo.Base.SubSequence: Collection.
    }
}

let arr = [1,2,3,4,5] as [UInt8]
Bar().run(arr)
@belkadan
Copy link
Contributor

You've guaranteed that Base.SubSequence is a Collection, but not that Base.SubSequence.SubSequence is a Collection, or that Base.SubSequence.Index == Base.Index. The standard library wants to make a lot of these additional conditions unnecessary (though it's blocked on a few missing generics features), but for now the language is behaving as expected.

@krzyzanowskim
Copy link
Contributor Author

on that note, why Collection.SubSequence is IndexableBase, not a Collection?

@belkadan
Copy link
Contributor

That's part of the same issue: the compiler currently doesn't support recursive constraints.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants