From 07c613b48530a116b80be69b6fa6aaf9ecfd43cc Mon Sep 17 00:00:00 2001 From: Sean Kim Date: Mon, 22 Jul 2024 16:35:36 +0200 Subject: [PATCH] Cleanup: Adjust GSpan and GArray asserts Previously, 4e9e44ad made changes to allow GSpan and GMutableSpan to not have a type when they are empty. This mirrors the same change in the conversion from GArray to both span types and when converting to an actual typed Span<> or MutableSpan<> via typed(). Fixes #125013 Pull Request: https://projects.blender.org/blender/blender/pulls/125018 --- source/blender/blenlib/BLI_generic_array.hh | 8 ++++---- source/blender/blenlib/BLI_generic_span.hh | 6 ++++-- source/blender/blenlib/tests/BLI_generic_array_test.cc | 9 +++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/source/blender/blenlib/BLI_generic_array.hh b/source/blender/blenlib/BLI_generic_array.hh index 37e26f2069a..7355e8021d4 100644 --- a/source/blender/blenlib/BLI_generic_array.hh +++ b/source/blender/blenlib/BLI_generic_array.hh @@ -169,14 +169,14 @@ class GArray { operator GSpan() const { - BLI_assert(type_ != nullptr); - return GSpan(*type_, data_, size_); + BLI_assert(size_ == 0 || type_ != nullptr); + return GSpan(type_, data_, size_); } operator GMutableSpan() { - BLI_assert(type_ != nullptr); - return GMutableSpan(*type_, data_, size_); + BLI_assert(size_ == 0 || type_ != nullptr); + return GMutableSpan(type_, data_, size_); } GSpan as_span() const diff --git a/source/blender/blenlib/BLI_generic_span.hh b/source/blender/blenlib/BLI_generic_span.hh index 42443ae604d..5fc118f825b 100644 --- a/source/blender/blenlib/BLI_generic_span.hh +++ b/source/blender/blenlib/BLI_generic_span.hh @@ -85,7 +85,8 @@ class GSpan { template Span typed() const { - BLI_assert(type_->is()); + BLI_assert(size_ == 0 || type_ != nullptr); + BLI_assert(type_ == nullptr || type_->is()); return Span(static_cast(data_), size_); } @@ -212,7 +213,8 @@ class GMutableSpan { template MutableSpan typed() const { - BLI_assert(type_->is()); + BLI_assert(size_ == 0 || type_ != nullptr); + BLI_assert(type_ == nullptr || type_->is()); return MutableSpan(static_cast(data_), size_); } diff --git a/source/blender/blenlib/tests/BLI_generic_array_test.cc b/source/blender/blenlib/tests/BLI_generic_array_test.cc index 0b85d7e807f..7c6b56cf280 100644 --- a/source/blender/blenlib/tests/BLI_generic_array_test.cc +++ b/source/blender/blenlib/tests/BLI_generic_array_test.cc @@ -133,4 +133,13 @@ TEST(generic_array, AssignDefault) EXPECT_EQ(array.data(), nullptr); } +TEST(generic_array, DefaultConstructor) +{ + GArray<> array; + + EXPECT_TRUE(array.is_empty()); + EXPECT_TRUE(array.as_mutable_span().is_empty()); + EXPECT_TRUE(array.as_span().is_empty()); +} + } // namespace blender::tests