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-9615] @escaping and inout should work together but don't #52061

Open
swift-ci opened this issue Jan 8, 2019 · 7 comments
Open

[SR-9615] @escaping and inout should work together but don't #52061

swift-ci opened this issue Jan 8, 2019 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers parser Area → compiler: The legacy C++ parser

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jan 8, 2019

Previous ID SR-9615
Radar None
Original Reporter Anandabits (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, Parser, StarterBug
Assignee rayfix (JIRA)
Priority Medium

md5: 2869f1f48cc0d67f32ed9775f6516d10

Issue Description:

The following code does not work, but according to @jckarter it should: https://twitter.com/jckarter/status/1082421111982219264

// 'inout' may only be used on parameters
func foo(f: @escaping inout () -> Void) {}

// @escaping attribute may only be used in function parameter position
func foo(f: inout @escaping () -> Void) {}

It must be possible to combine these attributes in order to capture a copy of an inout argument and call it later.

@belkadan
Copy link
Contributor

belkadan commented Jan 8, 2019

Note that the @escaping is unnecessary in this case; by being inout it's structurally not a top-level parameter type (and therefore implicitly escaping), and it also doesn't make sense to have a non-escaping inout parameter. But we should diagnose it better.

@belkadan
Copy link
Contributor

belkadan commented Jan 8, 2019

@slavapestov also pointed out that the existing diagnostic for things like Array<@escaping () -> Void> isn't great either.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 8, 2019

Comment by Matthew Johnson (JIRA)

@jrose I don’t believe this code works without `@escaping` either though. I’m on my phone right now and will try without it when get backup my computer.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 8, 2019

Comment by Matthew Johnson (JIRA)

@belkadan as it turns out this works when the function is meeting a protocol requirement, but crashes the compiler when it does meet a protocol conformance, this the following code crashes the compiler:

protocol P {
    associatedtype T
    func foo(t: inout T)
}
struct S: P {
    func foo(t: inout () -> Void) {}
}

An attempt to capture a copy of the function isn't required and doesn't appear to be the problem.

@belkadan
Copy link
Contributor

belkadan commented Jan 8, 2019

Can you file a separate issue for that, then? It's still worth improving the diagnostic(s) if someone tries what you tried.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 8, 2019

Comment by Matthew Johnson (JIRA)

Sure. For reference: https://bugs.swift.org/browse/SR-9624

@swift-ci
Copy link
Collaborator Author

Comment by Ray Fix (JIRA)

CodaFi (JIRA User) I saw your invitation on the (now defunct) try! Swift slack. It seems like maybe you had some hints about fixing this one. Any chance you could share those hints here?

My understanding is that the diagnostic should be changed to say something like:

"@escaping is not needed here" or perhaps "@escaping can only be applied to top-level types" If there is a particular wording you have in mind, please let me know.

It seems like the fix has something to do with determining if the type is a top level type and emit the warning (error?) if it is not. Will this PR also need to address `Array<@escaping () -> Void>` @belkadan or is that a different PR?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers parser Area → compiler: The legacy C++ parser
Projects
None yet
Development

No branches or pull requests

2 participants