Skip to content

[cxx-interop] Fix nullptr casts in auto-conformance logic #61853

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
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ClangImporter/ClangDerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ void swift::conformToCxxIteratorIfNeeded(

// Try to conform to UnsafeCxxRandomAccessIterator if possible.

auto minus = dyn_cast<FuncDecl>(getMinusOperator(decl));
auto minus = dyn_cast_or_null<FuncDecl>(getMinusOperator(decl));
if (!minus)
return;
auto distanceTy = minus->getResultInterfaceType();
// distanceTy conforms to BinaryInteger, this is ensured by getMinusOperator.

auto plusEqual = dyn_cast<FuncDecl>(getPlusEqualOperator(decl, distanceTy));
auto plusEqual = dyn_cast_or_null<FuncDecl>(getPlusEqualOperator(decl, distanceTy));
if (!plusEqual)
return;

Expand Down
90 changes: 90 additions & 0 deletions test/Interop/Cxx/stdlib/overlay/Inputs/custom-iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,94 @@ struct HasNoDereferenceOperator {
}
};

// MARK: Types that claim to be random access iterators, but actually aren't.

struct HasNoMinusOperator {
int value;

using iterator_category = std::random_access_iterator_tag;
using value_type = int;
using pointer = int *;
using reference = const int &;
using difference_type = int;

HasNoMinusOperator(int value) : value(value) {}
HasNoMinusOperator(const HasNoMinusOperator &other) = default;

const int &operator*() const { return value; }

HasNoMinusOperator &operator++() {
value++;
return *this;
}
void operator+=(difference_type v) { value += v; }
void operator-=(difference_type v) { value -= v; }
HasNoMinusOperator operator+(difference_type v) const {
return HasNoMinusOperator(value + v);
}
HasNoMinusOperator operator-(difference_type v) const {
return HasNoMinusOperator(value - v);
}
friend HasNoMinusOperator operator+(difference_type v,
const HasNoMinusOperator &it) {
return it + v;
}

bool operator<(const HasNoMinusOperator &other) const {
return value < other.value;
}

bool operator==(const HasNoMinusOperator &other) const {
return value == other.value;
}
bool operator!=(const HasNoMinusOperator &other) const {
return value != other.value;
}
};

struct HasNoPlusEqualOperator {
int value;

using iterator_category = std::random_access_iterator_tag;
using value_type = int;
using pointer = int *;
using reference = const int &;
using difference_type = int;

HasNoPlusEqualOperator(int value) : value(value) {}
HasNoPlusEqualOperator(const HasNoPlusEqualOperator &other) = default;

const int &operator*() const { return value; }

HasNoPlusEqualOperator &operator++() {
value++;
return *this;
}
void operator-=(difference_type v) { value -= v; }
HasNoPlusEqualOperator operator+(difference_type v) const {
return HasNoPlusEqualOperator(value + v);
}
HasNoPlusEqualOperator operator-(difference_type v) const {
return HasNoPlusEqualOperator(value - v);
}
friend HasNoPlusEqualOperator operator+(difference_type v,
const HasNoPlusEqualOperator &it) {
return it + v;
}
int operator-(const HasNoPlusEqualOperator &other) const {
return value - other.value;
}

bool operator<(const HasNoPlusEqualOperator &other) const {
return value < other.value;
}

bool operator==(const HasNoPlusEqualOperator &other) const {
return value == other.value;
}
bool operator!=(const HasNoPlusEqualOperator &other) const {
return value != other.value;
}
};

#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_CUSTOM_ITERATOR_H