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

Unexpected Behavior in Swift 5.9 with ~Copyable and consuming in For Loop #68230

Open
ojun9 opened this issue Aug 31, 2023 · 0 comments
Open
Labels
accepts invalid Bug: Accepts invalid bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. for-in loops Feature: for-in loops noncopyable struct/enum Feature → declarations: Noncopyable value type declarations struct Feature → type declarations: Structure declarations swift 5.9

Comments

@ojun9
Copy link
Contributor

ojun9 commented Aug 31, 2023

Description

When using the ~Copyable and consuming features of Swift 5.9 within a for loop, the expected compile-time error is not produced.
Instead, the code executes and results in unexpected deinitialization behavior.

Steps to reproduce

Here's the code sample:

struct MyValue: ~Copyable {
    var value = 0

    init() { print("init:", value) }
    deinit { print("deinit:", value) }

    consuming func run() {
        return
    }
}

func main() {
    var myValue = MyValue()

    for tmp in 1 ... 3 {
        myValue.value = tmp
        myValue.run()
    }
}
main()

Output:

init: 0
deinit: 1
deinit: 2
deinit: 3
deinit: 3

Expected behavior

When using ~Copyable and consuming within a for loop, the compiler should generate a compile-time error.
This error is not produced, leading to unnecessary calls to deinit. The lack of a compile-time error could lead to incorrect object creation and destruction.

Environment

  • Swift compiler version info: Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)
  • Xcode version info: Xcode 15.0 Build version 15A5229m (beta 8)
  • Deployment target: Any

Additional information

  1. Adding a member variable like the following to the struct causes a runtime error.
var id = UUID().uuidString
  1. I have also disassembled the main() and run() functions and verified that deinit is being called in both functions.
@ojun9 ojun9 added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Aug 31, 2023
@LucianoPAlmeida LucianoPAlmeida added accepts invalid Bug: Accepts invalid for-in loops Feature: for-in loops swift 5.9 struct Feature → type declarations: Structure declarations noncopyable struct/enum Feature → declarations: Noncopyable value type declarations and removed triage needed This issue needs more specific labels labels Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepts invalid Bug: Accepts invalid bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. for-in loops Feature: for-in loops noncopyable struct/enum Feature → declarations: Noncopyable value type declarations struct Feature → type declarations: Structure declarations swift 5.9
Projects
None yet
Development

No branches or pull requests

2 participants