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-13991] Generic return types can't be resolved when called as a function #56386

Closed
amomchilov opened this issue Dec 24, 2020 · 3 comments
Closed
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

@amomchilov
Copy link
Contributor

Previous ID SR-13991
Radar rdar://problem/72864709
Original Reporter @amomchilov
Type Bug
Status Resolved
Resolution Invalid
Environment

Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
Target: x86_64-apple-darwin20.2.0

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

md5: 3fb9705b5c40a279bdc4eb0e6544ce5a

Issue Description:

I was playing around with @dynamicMemberLookup, trying to implement a library version of something like C#'s built-in dynamic feature.

@dynamicMemberLookup
class Dynamic {
    private var members = [String: Any]()
    
    subscript<T>(dynamicMember member: String) -> T! {
        get { members[member] as? T }
        set { members[member] = newValue }
    }
}

let x = Dynamic()
x.i = 1
x.s = "abc"
x.f = { $0 + 10 } as (Int) -> Int

print(x.i as Int)
print(x.s as String)
let i: Int = x.f(10) # Generic parameter 'T' could not be inferred
print(i)

It's a silly example, and I can't imagine myself using something like this in prod, but it seems like it should be possible, anyway.

The expression x.f(10) should have enough information for the generic return type to be inferred.

  • It's known to be a function, because it's being called with ()

  • It's known to return an Int.

  • It's known to have one parameter, which is an integer literal, which in the absence of other constraints should be inferred to IntegerLiteralType (Int by default)

@typesanitizer
Copy link

@swift-ci create

@typesanitizer
Copy link

Simplified example:

func f<T>() -> T {
    fatalError("aaa")
}
let x : Int = f()(10 as Int) // error: generic parameter 'T' could not be inferred

@slavapestov
Copy link
Member

theindigamer (JIRA User) I don't think this can ever work. The return type of "f()" is some type variable; then you're applying the type variable to an argument of Int type. We don't "work backwards" to infer that the type of the type variable is "(Int) -> ()", and in fact we cannot, because it might be "(Any) -> ()", or it might be a dynamicCallable value, or whatever.

@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

3 participants