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

Non-copyable: compiler produces a copy of non-copyable value returned by pointer using 'unsafeAddress' accessor in a subscript #70047

Open
hyp opened this issue Nov 27, 2023 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. noncopyable struct/enum Feature → declarations: Noncopyable value type declarations ownership Feature: Ownership modifiers and semantics

Comments

@hyp
Copy link
Contributor

hyp commented Nov 27, 2023

Description

Using the unsafeAddress accessor in a subscript that returns a non-copyable type makes Swift copy the value when it's returned by a subscript get access that intends only to borrow the value. The following error is produced:

error: copy of noncopyable typed value. This is a compiler bug. Please file a bug with a small example of the bug
    _ = borrowSwiftNC(holder[0])
                            ^

This accessor is needed for C++ interop, to support use of non-copyable return references in accessors that get synthesized for an imported operated [].

Reproduction

public struct SwiftNonCopyable: ~Swift.Copyable {
    let x: Int
}

public struct SwiftNonCopyableHolderConstSubscript: ~Swift.Copyable {
    let x: SwiftNonCopyable
}

extension SwiftNonCopyableHolderConstSubscript {
    subscript(_ i: Int) -> SwiftNonCopyable {
        unsafeAddress {
            var ptr: UnsafePointer<SwiftNonCopyable>?
            withUnsafePointer(to: self.x) {
                ptr = $0
            }
            return ptr!
        }
    }
}

func borrowSwiftNC(_ x: borrowing SwiftNonCopyable) -> Int {
    return x.x
}

func test() {
    let holder = SwiftNonCopyableHolderConstSubscript(x: SwiftNonCopyable(x: 23))
    _ = borrowSwiftNC(holder[0])
}

test()

Expected behavior

The code should compile, as it should be okay to borrow the address value returned from the subscript accessor.

Environment

Swift main @:

commit 7680332 (origin/main, origin/HEAD)
Merge: 975519b 46c4da3
Date: Thu Nov 16 10:06:03 2023 -0800

Additional information

No response

@hyp hyp added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Nov 27, 2023
@hyp
Copy link
Contributor Author

hyp commented Nov 27, 2023

Note : the sample code needs -enable-experimental-feature NoncopyableGenerics at the moment.

@hyp
Copy link
Contributor Author

hyp commented Nov 27, 2023

It also works for property accessors, but not for subscripts :(

@kavon kavon self-assigned this Nov 29, 2023
@hborla hborla added ownership Feature: Ownership modifiers and semantics noncopyable struct/enum Feature → declarations: Noncopyable value type declarations and removed triage needed This issue needs more specific labels labels Apr 27, 2024
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. noncopyable struct/enum Feature → declarations: Noncopyable value type declarations ownership Feature: Ownership modifiers and semantics
Projects
None yet
Development

No branches or pull requests

3 participants