Skip to content

[copy-operator] Add support for the copy operator in preparation for making consuming and borrowing no implicit copy. #65935

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

Merged
merged 5 commits into from
May 19, 2023

Conversation

gottesmm
Copy link
Contributor

rdar://101862423

@gottesmm
Copy link
Contributor Author

@gottesmm gottesmm force-pushed the rdar101862423 branch 2 times, most recently from d14e9eb to f503da1 Compare May 17, 2023 20:31
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test

@gottesmm
Copy link
Contributor Author

swiftlang/swift-syntax#1669

@swift-ci smoke test

gottesmm added 5 commits May 17, 2023 22:42
…making consuming and borrowing no implicit copy.

Some notes:

1. I implemented this as a contextual keyword that can only apply directly to
lvalues. This ensures that we can still call functions called copy, define
variables named copy, etc. I added tests for both the c++ and swift-syntax based
parsers to validate this. So there shouldn't be any source breaks.

2. I did a little bit of type checker work to ensure that we do not treat
copy_expr's result as an lvalue. Otherwise, one could call mutating functions on
it or assign to it, which we do not want since the result of copy_value is

3. As expected, by creating a specific expr, I was able to have much greater
control of the SILGen codegen and thus eliminate extraneous copies and other
weirdness than if we used a function and had to go through SILGenApply.

rdar://101862423
…identifiers correctly.

This resulted from explorations around implementing the copy expr.

rdar://109479131
…d an lvalue.

Before this change it was possible to:

1. Call mutating methods on a consume result.
2. assign into a consume (e.x.: var x = ...; (consume x) = value.

From an implementation perspective, this involved just taking the logic I
already used for the CopyExpr and reusing it for ConsumeExpr with some small
tweaks.

rdar://109479440
…on var without errors to work.

I found that in the given case, an extra allocation was being emitted causing us
to in the success case to have an extra copy. In the previous commit, as part of
updating the codegen for consume, I also ensured that we wouldn't have that
extra allocation any more by handling the lvalue directly.

rdar://109222496
@gottesmm
Copy link
Contributor Author

@jckarter you probably want to take a quick additional look at this since I decided while I was here to also fix consume so it has the same behavior and also in the process fix another issue where consume was emitting proper errors for move only types, but due to an extra allocation that we introduced in the var case (due to us using rvalue logic instead of lvalue logic that the type checker work here allows us to), we would hit a copy of noncopyable type error. Now that we use the lvalue logic directly for consume like I do for copy, that is elided.

@gottesmm
Copy link
Contributor Author

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case you’re doing another revision of this PR: The general naming pattern of these files would be complete_copy_expr.swift

@gottesmm
Copy link
Contributor Author

@gottesmm
Copy link
Contributor Author

I updated the tests in lldb so that they work with these changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants