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-6103] Law of Exclusivity is not applied for extension of Array with closure which returns Element #48658

Closed
omochi opened this issue Oct 10, 2017 · 15 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@omochi
Copy link
Collaborator

omochi commented Oct 10, 2017

Previous ID SR-6103
Radar rdar://problem/35215926
Original Reporter @omochi
Type Bug
Status Closed
Resolution Done
Environment

I confirmed issue in those environment.

  • Xcode 9.0 (9A235)

  • swift-DEVELOPMENT-SNAPSHOT-2017-10-09-a.xctoolchain

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

md5: d6252cd613b8dadf61dfa718a82d951b

Issue Description:

This code can be compiled incorrectly.

extension Array {
    mutating func update(_ f: () -> Element) {}
}

func main() {
    var xs: [Int] = [1, 2, 3]
    var ys: [Int] = []
    xs.update {
        ys = xs
        return 3
    }
}

main()

Calling mutating func of `xs` and reading `xs` on right hand of assignment should be conflict.

Strangely, this problem occurs only when `Element` is used at closure result.
If return type changed to `Void`, `Array<Element>` or `Int`,
all cases will be compile error correctly.

This is `Void` case.

extension Array {
    mutating func update(_ f: () -> Void) {}
}

func main() {
    var xs: [Int] = [1, 2, 3]
    var ys: [Int] = []
    xs.update {
        ys = xs
    }
}

main()
$ swift run
Compile Swift Module 'swex' (1 sources)
/Users/omochi/temp/swex/Sources/swex/main.swift:7:9: warning: variable 'ys' was written to, but never read
    var ys: [Int] = []
        ^
/Users/omochi/temp/swex/Sources/swex/main.swift:8:5: error: overlapping accesses to 'xs', but modification requires exclusive access; consider copying to a local variable
    xs.update {
    ^~
/Users/omochi/temp/swex/Sources/swex/main.swift:9:12: note: conflicting access is here
        ys = xs
        ~~~^~~~
@belkadan
Copy link
Contributor

belkadan commented Oct 10, 2017

@devincoughlin, @rjmccall, any insights?

@rjmccall
Copy link
Member

rjmccall commented Oct 10, 2017

The type-specificity suggests that this is related to the code-generation differences for an indirect-out parameter, but I don't know why. Regardless, clearly a bug.

@rjmccall
Copy link
Member

rjmccall commented Oct 27, 2017

Ah, it's the reabstraction thunk.

@rjmccall
Copy link
Member

rjmccall commented Oct 27, 2017

Reduced test case:

extension Array {
    mutating func update(_ f: () -> (Element)) {}
}

func test(xs: inout [Int], ys: inout [Int]) {
    xs.update {
        ys = xs
        return 3
    }
}

@rjmccall
Copy link
Member

rjmccall commented Oct 27, 2017

Probably possible to address this directly with the current representation, but might be easier to fix if we had a reabstract_function instruction.

@rjmccall
Copy link
Member

rjmccall commented Oct 27, 2017

Tracking internally as rdar://35215926.

@devincoughlin
Copy link
Member

devincoughlin commented Nov 17, 2017

There is a fix in #12929

@devincoughlin
Copy link
Member

devincoughlin commented Nov 17, 2017

Should be fixed in #12929

Thanks for reporting the issue!

@omochi
Copy link
Collaborator Author

omochi commented Nov 21, 2017

Thanks to fix it.
I am grad to report it.

Sorry, why am I set to assignee ?
What should I do?

@devincoughlin
Copy link
Member

devincoughlin commented Nov 22, 2017

We assign the bug to the originator so you can confirm that the the bug is fixed and close the JIRA issue. If you have time, it would be helpful if you could download a nightly toolchain from swift.org and confirm that the bug is fixed. This helps us make sure that we fixed the bug properly. If you don't have the time, then you can close the JIRA issue.

@omochi
Copy link
Collaborator Author

omochi commented Nov 22, 2017

Thanks Devin to explain for me.
I understand operation.

@omochi
Copy link
Collaborator Author

omochi commented Nov 22, 2017

I checked with snapshot at 2017/11/21.

Unfortunately compiler is still wrong a little.
It reports conflict correctly, but it say that is warning.

Of course If I change `Element` to `Int` at line 2,
Compiler say that is error.

This is log.

[omochi@omochi-iMac bbb]$ cat a.swift
extension Array {
    mutating func update(_ f: () -> Element) {}
}

func main() {
    var xs: [Int] = [1, 2, 3]
    var ys: [Int] = []
    xs.update {
        ys = xs
        return 3
    }
}

main()
[omochi@omochi-iMac bbb]$ export TOOLCHAINS=org.swift.3020171121a
[omochi@omochi-iMac bbb]$ swift --version
Apple Swift version 4.1-dev (LLVM fe49d8f2ca, Clang 839070845c, Swift c0c04864db)
Target: x86_64-apple-darwin16.7.0
[omochi@omochi-iMac bbb]$ swift a.swift
a.swift:8:9: warning: variable 'ys' was written to, but never read
    var ys: [Int] = []
        ^
a.swift:9:5: warning: overlapping accesses to 'xs', but modification requires exclusive access; consider copying to a local variable
    xs.update {
    ^~
a.swift:10:12: note: conflicting access is here
        ys = xs
        ~~~^~~~

@devincoughlin
Copy link
Member

devincoughlin commented Nov 22, 2017

Thanks for for checking this.

For Swift 4.1 the new diagnostics will be warnings and not errors. This is for source compatibility. Where possible, we would like to avoid code that compiles with the 4.0 compiler having an error in 4.1. The plan is to turn the new diagnostics into hard errors in a future version of the Swift compiler.

@omochi
Copy link
Collaborator Author

omochi commented Nov 22, 2017

Oh I see. I close issue.

@omochi
Copy link
Collaborator Author

omochi commented Nov 22, 2017

thank you

@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

4 participants