Skip to content

[cxx-interop] Add tests for operator++() that returns void #59663

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 1 commit into from
Jun 23, 2022

Conversation

egorzhdan
Copy link
Contributor

This pattern was discovered on llvm::detail::SafeIntIterator (llvm/ADT/Sequence.h:153):

  // Pre Increment/Decrement
  void operator++() { offset(1); }
  void operator--() { offset(-1); }

Previous patch: #59643.

rdar://95684692

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jun 23, 2022
@egorzhdan egorzhdan requested a review from Huddie June 23, 2022 11:31
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@Huddie Huddie left a comment

Choose a reason for hiding this comment

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

Had a Q on the mapping going on when return type is void

// CHECK: }

// CHECK: struct HasPreIncrementOperatorWithVoidReturnType {
// CHECK: func successor() -> HasPreIncrementOperatorWithVoidReturnType
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this function be mutating and just update the value in lhs.value or something? Feels odd that a void function in Cxx is being mapped into a function that returns a type (a copy I assume)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't want to map them differently for a couple reasons:

  • We're importing operator++ mainly to make it possible to increment C++ iterators from Swift. If the signature of func successor() -> Self changes depending on the return type of operator++, we can't conform iterators to a single protocol (UnsafeCxxInputIterator), and that defeats the purpose of importing operator++ in the first place.
  • In most C++ use cases, there is no difference between void operator++() and T& operator++(). I think it would be a bit confusing to introduce it in Swift.

let anotherReturnTypeResult: HasPreIncrementOperatorWithAnotherReturnType = anotherReturnType.successor()

let voidReturnType = HasPreIncrementOperatorWithVoidReturnType()
let voidReturnTypeResult: HasPreIncrementOperatorWithVoidReturnType = voidReturnType.successor()
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a check that the successors .value is indeed 1 more than the predecessor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, we should have an execution test for this.

This pattern was discovered on `llvm::detail::SafeIntIterator` (`llvm/ADT/Sequence.h:153`):
```cpp
  // Pre Increment/Decrement
  void operator++() { offset(1); }
  void operator--() { offset(-1); }
```

rdar://95684692
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-plusplus-void-tests branch from 90513c8 to 0f1ef6d Compare June 23, 2022 11:56
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan merged commit 56024b3 into main Jun 23, 2022
@egorzhdan egorzhdan deleted the egorzhdan/cxx-plusplus-void-tests branch June 23, 2022 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants