Uploaded image for project: 'Swift'
  1. Swift
  2. SR-11429

Don't look through CoerceExprs in markDirectCallee

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
    • Environment:

      Swift version 5.1-dev (LLVM c5340df2d1, Swift c14addb448)
      Target: x86_64-apple-darwin18.5.0

      Description

      Currently in markDirectCallee, we look through CoerceExprs when marking a callee as being directly applied. However the fact that it gets marked as being directly applied means that any functions with argument labels cannot be used with a coercion to a user-written function type:

      func foo(x: Int) {}
      (foo as (Int) -> ())(5)
      // error: Cannot convert value of type '(Swift.Int) -> ()' to type '(Swift.Int) -> ()' in coercion
      

      This is because function types written by the user cannot include argument labels.

      The only way to get code like this to compile is to let the compiler infer the type in the coercion through a generic type alias, which allows the argument label to be propagated:

      typealias Magic<T> = T
      
      func foo(x: Int) {}
      (foo as Magic)(x: 5) // Legal.
      

      But I would hope that nobody is actually writing code like this.

      In addition, trying to rely on the fact that the referenced function within the coercion is marked as being directly applied in order to get an implicit IUO unwrap currently crashes (SR-10492):

      func foo() -> Int! {}
      (foo as () -> Int)()
      

      And I can't immediately think of any other compelling examples that would break if we were to stop looking through CoerceExprs in markDirectCallee.

        Attachments

          Activity

            People

            Assignee:
            originaluser2 Hamish Knight
            Reporter:
            originaluser2 Hamish Knight
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: