This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/main by this push:
new 1cfa5f7c Constrain Optional's constructors for C++20 (#887)
1cfa5f7c is described below
commit 1cfa5f7c0e940e2173f1d4583cec7c00136a4626
Author: Tom Fogal <[email protected]>
AuthorDate: Fri Oct 30 09:17:35 2026 -0700
Constrain Optional's constructors for C++20 (#887)
Optional<T>'s non-template std::optional<T> constructor makes copying an
Optional consider std::optional<T>'s converting constructor from
Optional. The standard constrains that constructor on whether T is
constructible from Optional<T>. For types with an explicit Any
constructor (e.g. VisitInterrupt), that check routes back through the
Optional copy constructor, so the constraint depends on itself.
Implementations are not required to accept such a constraint, and Clang
rejects it, so structural_visit.cc and structural_mutate.cc fail to
build in C++20.
Accept only std::optional sources via a constrained template, in all
three Optional specializations. This removes the dependency cycle
without relying on any particular standard library's behavior.
Co-Authored-By: Claude
---
include/tvm/ffi/optional.h | 38 +++++++++++++++++++++++++++++++-------
tests/cpp/test_optional.cc | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 69 insertions(+), 7 deletions(-)
diff --git a/include/tvm/ffi/optional.h b/include/tvm/ffi/optional.h
index 2bf2da4c..8a499785 100644
--- a/include/tvm/ffi/optional.h
+++ b/include/tvm/ffi/optional.h
@@ -72,6 +72,12 @@ inline constexpr bool is_arc_type_v<Arc<TObject>> = true;
template <typename T>
inline constexpr bool use_object_ptr_optional_v =
use_object_ref_optional_v<T> || is_object_ptr_type_v<T> ||
is_arc_type_v<T>;
+
+template <typename T>
+inline constexpr bool is_std_optional_v = false;
+
+template <typename T>
+inline constexpr bool is_std_optional_v<std::optional<T>> = true;
/// \endcond
// Fallback specialization for types that do NOT enable Any storage
@@ -88,7 +94,11 @@ class Optional<T,
// NOLINTBEGIN(google-explicit-constructor)
TVM_FFI_INLINE Optional(const Optional& other) = default;
TVM_FFI_INLINE Optional(Optional&& other) noexcept = default;
- TVM_FFI_INLINE Optional(std::optional<T> other) : data_(std::move(other)) {}
+ // See the ObjectRef specialization for why std::optional sources are
constrained.
+ template <typename U, typename = std::enable_if_t<std::conjunction_v<
+
std::bool_constant<is_std_optional_v<std::decay_t<U>>>,
+ std::is_convertible<U, std::optional<T>>>>>
+ TVM_FFI_INLINE Optional(U&& other) : data_(std::forward<U>(other)) {}
Optional(std::nullopt_t) {}
TVM_FFI_INLINE Optional(T other) : data_(std::move(other)) {}
// NOLINTEND(google-explicit-constructor)
@@ -196,9 +206,14 @@ class Optional<T,
/*! \brief construct from a value of type T (move). */
TVM_FFI_INLINE Optional(T&& value) : data_(std::move(value)) {}
/*! \brief construct from a std::optional<T>. */
- TVM_FFI_INLINE Optional(std::optional<T> other) {
- if (other.has_value()) {
- data_ = Any(*std::move(other));
+ // See the ObjectRef specialization for why std::optional sources are
constrained.
+ template <typename U, typename = std::enable_if_t<std::conjunction_v<
+
std::bool_constant<is_std_optional_v<std::decay_t<U>>>,
+ std::is_convertible<U, std::optional<T>>>>>
+ TVM_FFI_INLINE Optional(U&& other) {
+ std::optional<T> value(std::forward<U>(other));
+ if (value.has_value()) {
+ data_ = Any(*std::move(value));
}
}
// NOLINTEND(google-explicit-constructor)
@@ -413,9 +428,18 @@ class Optional<T,
std::enable_if_t<use_object_ref_optional_v<T>>> {
explicit Optional(UnsafeInit) {}
Optional(std::nullopt_t) {}
Optional(std::nullptr_t) {}
- TVM_FFI_INLINE Optional(std::optional<T> other) {
- if (other.has_value()) {
- *this = *std::move(other);
+ // Accept only std::optional sources. A non-template std::optional<T>
parameter makes copying an
+ // Optional consider std::optional<T>'s converting constructor from
Optional. The standard
+ // constrains that constructor on whether T is constructible from Optional,
which for types with
+ // an explicit Any constructor routes back through this very constructor:
the constraint depends
+ // on itself, so a conforming implementation may reject it.
+ template <typename U, typename = std::enable_if_t<std::conjunction_v<
+
std::bool_constant<is_std_optional_v<std::decay_t<U>>>,
+ std::is_convertible<U, std::optional<T>>>>>
+ TVM_FFI_INLINE Optional(U&& other) {
+ std::optional<T> value(std::forward<U>(other));
+ if (value.has_value()) {
+ *this = *std::move(value);
}
}
TVM_FFI_INLINE Optional(T other)
diff --git a/tests/cpp/test_optional.cc b/tests/cpp/test_optional.cc
index 87dec761..bb185284 100644
--- a/tests/cpp/test_optional.cc
+++ b/tests/cpp/test_optional.cc
@@ -20,11 +20,14 @@
#include <tvm/ffi/any.h>
#include <tvm/ffi/container/array.h>
#include <tvm/ffi/container/tensor.h>
+#include <tvm/ffi/expected.h>
#include <tvm/ffi/memory.h>
#include <tvm/ffi/optional.h>
#include <cstdint>
#include <cstring>
+#include <optional>
+#include <type_traits>
#include "./testing_object.h"
@@ -57,6 +60,22 @@ static_assert(sizeof(Optional<Optional<int>>) ==
sizeof(Any));
static_assert(sizeof(Optional<Optional<TInt>>) == sizeof(Any));
static_assert(detail::storage_enabled_v<Optional<int>>);
+// Regression guard: copying an Optional must not consider std::optional<T>'s
converting
+// constructor from Optional. The standard constrains that constructor on T
being constructible
+// from Optional, which for types with an explicit Any constructor recurses
back into the copy
+// and makes the constraint self-dependent.
+class TAnyConstructible : public ObjectRef {
+ public:
+ explicit TAnyConstructible(Any value);
+ TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TAnyConstructible, ObjectRef,
Object);
+};
+static_assert(
+ std::is_convertible_v<Optional<TAnyConstructible>,
Expected<Optional<TAnyConstructible>>>);
+static_assert(std::is_copy_constructible_v<std::optional<Optional<TAnyConstructible>>>);
+static_assert(std::is_convertible_v<Optional<Optional<int>>,
Expected<Optional<Optional<int>>>>);
+static_assert(std::is_copy_constructible_v<std::optional<Optional<Optional<int>>>>);
+static_assert(!std::is_constructible_v<Optional<TInt>, Optional<TNumber>>);
+
TEST(Optional, StorageEnabledPassThrough) {
// Optional<int> is storage-enabled, so a nested Optional round-trips through
// Any via the Any-backed path.
@@ -110,6 +129,25 @@ TEST(Optional, ObjectRefPointerABI) {
EXPECT_FALSE(Any(absent).cast<Optional<ObjectRef>>().has_value());
}
+TEST(Optional, FromStdOptional) {
+ std::optional<TInt> engaged = TInt(1);
+ Optional<TInt> from_lvalue = engaged;
+ EXPECT_EQ(from_lvalue.value()->value, 1);
+ Optional<TNumber> from_derived = std::optional<TInt>(TInt(2));
+ EXPECT_EQ(from_derived.value().as<TIntObj>()->value, 2);
+ Optional<TInt> from_empty = std::optional<TInt>();
+ EXPECT_FALSE(from_empty.has_value());
+
+ Optional<int> from_int = std::optional<int>(3);
+ EXPECT_EQ(from_int.value(), 3);
+ Optional<Optional<int>> nested =
std::optional<Optional<int>>(Optional<int>(4));
+ EXPECT_EQ(nested.value().value(), 4);
+
+ std::optional<TensorView> view;
+ Optional<TensorView> from_view = view;
+ EXPECT_FALSE(from_view.has_value());
+}
+
TEST(Optional, double) {
Optional<double> x;
Optional<double> y = 11.0;