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-2266] Typealiased function-types are @escaping by default #44873

Closed
karwa opened this issue Aug 3, 2016 · 4 comments
Closed

[SR-2266] Typealiased function-types are @escaping by default #44873

karwa opened this issue Aug 3, 2016 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@karwa
Copy link
Contributor

karwa commented Aug 3, 2016

Previous ID SR-2266
Radar None
Original Reporter @karwa
Type Bug
Status Resolved
Resolution Duplicate
Environment

Swift 6f743db

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: 312a42469bcc5f7c24a5b6f50ae236fd

duplicates:

  • SR-2053 [Regression] Using @NoEscape for a type aliased closure results in a compile error

Issue Description:

The following code does not seem to be parseable:

protocol A { 
    associatedType Index 
    typealias TransformBlock = @noescape (_ oldIndex: Index, _ aParam: Bool) -> Index 
     
    mutating func doSomething(at: Index, with: TransformBlock) 
} 
error: repl.swift:3:43: error: expected type
    typealias TransformBlock = @noescape (_ oldIndex: Index, _ aParam: Bool) -> Index
                                          ^

error: repl.swift:3:43: error: expected ',' separator
    typealias TransformBlock = @noescape (_ oldIndex: Index, _ aParam: Bool) -> Index
                                          ^
                                          ,

error: repl.swift:3:62: error: expected type
    typealias TransformBlock = @noescape (_ oldIndex: Index, _ aParam: Bool) -> Index
                                                             ^

error: repl.swift:3:62: error: expected ',' separator
    typealias TransformBlock = @noescape (_ oldIndex: Index, _ aParam: Bool) -> Index
@belkadan
Copy link
Contributor

belkadan commented Aug 3, 2016

I'm getting "use of undeclared type Index", which is better but not much.

@karwa
Copy link
Contributor Author

karwa commented Aug 3, 2016

Also, a couple more points:

  • I removed the labels and it seemed to parse. However, any types which conform to the protocol do not inherit the typealias (no such member 'TransformBlock'), meaning I have to write the full expanded signatures in the implementation.

  • "@escaping" attributes have to be part of the typealias; you can't have an `@escaping TransformBlock` because the compiler complains `TransformBlock` is not a function-type.

@belkadan
Copy link
Contributor

belkadan commented Aug 3, 2016

cc @milseman

@karwa
Copy link
Contributor Author

karwa commented Aug 4, 2016

After more investigation:

  • The parameter label complaint seems to be some issue with SourceKit. The code compiles without errors, but as soon as I view the file Xcode will add a few errors of its own (`Expected type`, `Expected ',' separator`). I'll build again from `master` and see if maybe it's some kind of Xcode version thing.
  • You can access associated types. The diagnostics aren't very helpful, I don't know why it told you that:
// Works (but my Xcode displays an error because of aforementioned labels issue)
protocol MyProto {  
    associatedtype Blob
    typealias Executable = (_ argOne: Blob, _ argTwo: Bool)->()
    func execute(_ block: Executable)
}
  • Function typealiases seem to be escaping, rather than non-escaping, by default (whether in a protocol or not). This is the actual bug. It's a coincidence that the thing above affected the same declarations.
protocol MyProto {
    
    typealias Executable = ()->()
    func execute(_ block: Executable)
}

struct TestStruct : MyProto {

    // FIXME: SWIFT: We should inherit this from the protocol
    typealias Executable = ()->()

    func execute(_ block: Executable) {
        block()
    }

    private func doThing() {
        print("Doing a thing...")
    }

    mutating func execute_doThing() {
        execute {
            doThing()
        }
    }
}

Produces:

(source_file
  (protocol "MyProto"<Self : MyProto> type='MyProto.Protocol' access=internal @_fixed_layout
    (typealias "Executable" type='Executable.Type' access=internal type='() -> ()')
*    (func_decl "execute(_:)" type='<Self : MyProto> (Self) -> (Executable) -> ()' interface type='<τ_0_0 where τ_0_0 : MyProto> (τ_0_0) -> (@escaping () -> ()) -> ()' access=internal
      (parameter_list
        (parameter "self" type='Self'))
      (parameter_list
        (parameter "block" type='Executable'))))
  (struct_decl "TestStruct" type='TestStruct.Type' access=internal @_fixed_layout inherits: MyProto
    (typealias "Executable" type='Executable.Type' access=internal type='() -> ()')
*    (func_decl "execute(_:)" type='(TestStruct) -> (Executable) -> ()' interface type='(TestStruct) -> (@escaping () -> ()) -> ()' access=internal
      (conformance test.(file).MyProto.execute@/Users/karl/Desktop/test.swift:4:7)
      (parameter_list
        (parameter "self" type='TestStruct'))
      (parameter_list
        (parameter "block" type='Executable'))
      (brace_stmt
        (call_expr type='()' location=/Users/karl/Desktop/test.swift:13:3 range=[/Users/karl/Desktop/test.swift:13:3 - line:13:9] nothrow  arg_labels=
          (declref_expr type='() -> ()' location=/Users/karl/Desktop/test.swift:13:3 range=[/Users/karl/Desktop/test.swift:13:3 - line:13:3] decl=test.(file).TestStruct.func decl.block@/Users/karl/Desktop/test.swift:12:17 function_ref=single specialized=no)
          (tuple_expr type='()' location=/Users/karl/Desktop/test.swift:13:8 range=[/Users/karl/Desktop/test.swift:13:8 - line:13:9]))))
  • You can't add `@escaping` or `@noescape` to a parameter whose type is a typealiased-function (the typealias is not considered to be a function-type)

@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
Projects
None yet
Development

No branches or pull requests

2 participants