From 88b629c1214761b1a996ce1cfa6a3ffd7417c4e6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 17 Aug 2026 12:52:42 +0200 Subject: [PATCH] Fix expected::operator*() const& and && failing to compile (#1538) ETL_ASSERT_OR_RETURN_VALUE tried to return ETL_NULLPTR from functions returning references, which cannot be initialized from nullptr. Use plain ETL_ASSERT instead, matching the other operator*() overloads. Add regression test for dereferencing const and temporary expected. Co-authored-by: John Wellbelove --- include/etl/expected.h | 4 ++-- test/test_expected.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/etl/expected.h b/include/etl/expected.h index bbfc514b..1feb1487 100644 --- a/include/etl/expected.h +++ b/include/etl/expected.h @@ -746,7 +746,7 @@ namespace etl //******************************************* const value_type& operator*() const ETL_LVALUE_REF_QUALIFIER { - ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); return etl::get(storage); } @@ -757,7 +757,7 @@ namespace etl //******************************************* value_type&& operator*() && { - ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); return etl::move(etl::get(storage)); } diff --git a/test/test_expected.cpp b/test/test_expected.cpp index 621727b2..b7bee13d 100644 --- a/test/test_expected.cpp +++ b/test/test_expected.cpp @@ -618,6 +618,20 @@ namespace etl::expected_invalid); } + //************************************************************************* + // See https://github.com/ETLCPP/etl/issues/1532 + TEST(test_dereference_const_and_rvalue_expected) + { + typedef etl::expected Expected1532; + + Expected1532 e = 42; + const Expected1532& ce = e; + + CHECK_EQUAL(42, *e); + CHECK_EQUAL(42, *ce); + CHECK_EQUAL(42, *Expected1532(42)); + } + //************************************************************************* struct value_or_helper {