From 1a95ab161d2159be788b06e4c67ec38320a4a620 Mon Sep 17 00:00:00 2001 From: Mihai Boros Date: Wed, 5 Feb 2025 23:05:13 +0200 Subject: [PATCH 1/3] 1328 Fixed issue comparing different numerical types as equal. --- include/cpp2util.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/include/cpp2util.h b/include/cpp2util.h index 6b9d48894..c0c80cefd 100644 --- a/include/cpp2util.h +++ b/include/cpp2util.h @@ -2102,10 +2102,7 @@ constexpr auto is( X const& x ) -> bool { if (!x.has_value()) { return std::same_as; } - if constexpr (requires { static_cast(*x);}) { - return true; - } - return false; + return std::same_as; } // is Value From 8b3eaef5bcc16b36dc83bcc14e9b15e315566b9d Mon Sep 17 00:00:00 2001 From: Mihai Boros Date: Thu, 6 Feb 2025 21:15:46 +0200 Subject: [PATCH 2/3] 1328 Added back type comparison in case of inheritance. --- include/cpp2util.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/cpp2util.h b/include/cpp2util.h index c0c80cefd..49e0c72c8 100644 --- a/include/cpp2util.h +++ b/include/cpp2util.h @@ -2102,7 +2102,8 @@ constexpr auto is( X const& x ) -> bool { if (!x.has_value()) { return std::same_as; } - return std::same_as; + return std::same_as + || std::derived_from, std::remove_pointer_t>; } // is Value From 6c5ab2fc8a5026f1885f8912f24f8c771c56d761 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Fri, 7 Feb 2025 12:52:09 -0800 Subject: [PATCH 3/3] Add regression test cases --- regression-tests/pure2-type-safety-1.cpp2 | 32 ++++++++++ .../pure2-type-safety-1.cpp.execution | 2 + .../pure2-type-safety-1.cpp.execution | 2 + .../pure2-type-safety-1.cpp.execution | 2 + .../pure2-type-safety-1.cpp.execution | 2 + .../test-results/pure2-type-safety-1.cpp | 60 +++++++++++++++++-- regression-tests/test-results/version | 2 +- 7 files changed, 96 insertions(+), 6 deletions(-) diff --git a/regression-tests/pure2-type-safety-1.cpp2 b/regression-tests/pure2-type-safety-1.cpp2 index 11734de99..68b8b9fba 100644 --- a/regression-tests/pure2-type-safety-1.cpp2 +++ b/regression-tests/pure2-type-safety-1.cpp2 @@ -19,6 +19,14 @@ main: () -> int = test_generic(v, "variant"); test_generic(a, "any"); test_generic(o, "optional"); + + oi: std::optional = 5; + std::cout << "optional is: "; + test_1365(oi); + od: std::optional<*D> = nullptr; + std::cout << "\noptional<*D> is: "; + test_1365(od); + std::cout << "\n"; } test_generic: ( x, msg ) = { @@ -28,6 +36,30 @@ test_generic: ( x, msg ) = { ::print( msgx + " is int? ", x is int ); } +B: type = { } +D: type = { this: B; } + +test_1365: (o) = { + if o is int { + std::cout << "int "; + } + if o is bool { + std::cout << "bool "; + } + if o is float { + std::cout << "float "; + } + if o is *B { + std::cout << "*B "; + } + if o is *D { + std::cout << "*D "; + } + if o is std::string { + std::cout << "std::string "; + } +} + print: ( msg: std::string, b: bool ) = { bmsg: * const char; if b { bmsg = "true"; } diff --git a/regression-tests/test-results/clang-12-c++20/pure2-type-safety-1.cpp.execution b/regression-tests/test-results/clang-12-c++20/pure2-type-safety-1.cpp.execution index d1aa6265a..df9d257d3 100644 --- a/regression-tests/test-results/clang-12-c++20/pure2-type-safety-1.cpp.execution +++ b/regression-tests/test-results/clang-12-c++20/pure2-type-safety-1.cpp.execution @@ -7,3 +7,5 @@ variant is int? true any is int? true optional is int? true +optional is: int +optional<*D> is: *B *D diff --git a/regression-tests/test-results/gcc-10-c++20/pure2-type-safety-1.cpp.execution b/regression-tests/test-results/gcc-10-c++20/pure2-type-safety-1.cpp.execution index d1aa6265a..df9d257d3 100644 --- a/regression-tests/test-results/gcc-10-c++20/pure2-type-safety-1.cpp.execution +++ b/regression-tests/test-results/gcc-10-c++20/pure2-type-safety-1.cpp.execution @@ -7,3 +7,5 @@ variant is int? true any is int? true optional is int? true +optional is: int +optional<*D> is: *B *D diff --git a/regression-tests/test-results/gcc-14-c++2b/pure2-type-safety-1.cpp.execution b/regression-tests/test-results/gcc-14-c++2b/pure2-type-safety-1.cpp.execution index d1aa6265a..df9d257d3 100644 --- a/regression-tests/test-results/gcc-14-c++2b/pure2-type-safety-1.cpp.execution +++ b/regression-tests/test-results/gcc-14-c++2b/pure2-type-safety-1.cpp.execution @@ -7,3 +7,5 @@ variant is int? true any is int? true optional is int? true +optional is: int +optional<*D> is: *B *D diff --git a/regression-tests/test-results/msvc-2022-c++latest/pure2-type-safety-1.cpp.execution b/regression-tests/test-results/msvc-2022-c++latest/pure2-type-safety-1.cpp.execution index d1aa6265a..df9d257d3 100644 --- a/regression-tests/test-results/msvc-2022-c++latest/pure2-type-safety-1.cpp.execution +++ b/regression-tests/test-results/msvc-2022-c++latest/pure2-type-safety-1.cpp.execution @@ -7,3 +7,5 @@ variant is int? true any is int? true optional is int? true +optional is: int +optional<*D> is: *B *D diff --git a/regression-tests/test-results/pure2-type-safety-1.cpp b/regression-tests/test-results/pure2-type-safety-1.cpp index 10d7e38c9..870c97a38 100644 --- a/regression-tests/test-results/pure2-type-safety-1.cpp +++ b/regression-tests/test-results/pure2-type-safety-1.cpp @@ -8,6 +8,10 @@ #line 1 "pure2-type-safety-1.cpp2" +#line 39 "pure2-type-safety-1.cpp2" +class B; +class D; + //=== Cpp2 type definitions and function declarations =========================== @@ -16,12 +20,28 @@ #line 2 "pure2-type-safety-1.cpp2" [[nodiscard]] auto main() -> int; -#line 24 "pure2-type-safety-1.cpp2" +#line 32 "pure2-type-safety-1.cpp2" auto test_generic(auto const& x, auto const& msg) -> void; -#line 31 "pure2-type-safety-1.cpp2" +#line 39 "pure2-type-safety-1.cpp2" +class B { + public: B() = default; + public: B(B const&) = delete; /* No 'that' constructor, suppress copy */ + public: auto operator=(B const&) -> void = delete; +}; +#line 40 "pure2-type-safety-1.cpp2" +class D: public B { + public: D() = default; + public: D(D const&) = delete; /* No 'that' constructor, suppress copy */ + public: auto operator=(D const&) -> void = delete; +}; +#line 41 "pure2-type-safety-1.cpp2" + +auto test_1365(auto const& o) -> void; + +#line 63 "pure2-type-safety-1.cpp2" auto print(cpp2::impl::in msg, cpp2::impl::in b) -> void; -#line 37 "pure2-type-safety-1.cpp2" +#line 69 "pure2-type-safety-1.cpp2" #line 1 "pure2-type-safety-1.cpp2" @@ -50,9 +70,17 @@ auto print(cpp2::impl::in msg, cpp2::impl::in b) -> void; test_generic(cpp2::move(v), "variant"); test_generic(cpp2::move(a), "any"); test_generic(cpp2::move(o), "optional"); + + std::optional oi {5}; + std::cout << "optional is: "; + test_1365(cpp2::move(oi)); + std::optional od {nullptr}; + std::cout << "\noptional<*D> is: "; + test_1365(cpp2::move(od)); + std::cout << "\n"; } -#line 24 "pure2-type-safety-1.cpp2" +#line 32 "pure2-type-safety-1.cpp2" auto test_generic(auto const& x, auto const& msg) -> void{ std::string msgx {msg}; // Full qualification is necessary to avoid ambiguity in C++23 @@ -60,7 +88,29 @@ auto test_generic(auto const& x, auto const& msg) -> void{ ::print(cpp2::move(msgx) + " is int? ", cpp2::impl::is(x)); } -#line 31 "pure2-type-safety-1.cpp2" +#line 42 "pure2-type-safety-1.cpp2" +auto test_1365(auto const& o) -> void{ + if (cpp2::impl::is(o)) { + std::cout << "int "; + } + if (cpp2::impl::is(o)) { + std::cout << "bool "; + } + if (cpp2::impl::is(o)) { + std::cout << "float "; + } + if (cpp2::impl::is(o)) { + std::cout << "*B "; + } + if (cpp2::impl::is(o)) { + std::cout << "*D "; + } + if (cpp2::impl::is(o)) { + std::cout << "std::string "; + } +} + +#line 63 "pure2-type-safety-1.cpp2" auto print(cpp2::impl::in msg, cpp2::impl::in b) -> void{ cpp2::impl::deferred_init bmsg; if (b) { bmsg.construct("true");} diff --git a/regression-tests/test-results/version b/regression-tests/test-results/version index 699ca83d9..034b6d55a 100644 --- a/regression-tests/test-results/version +++ b/regression-tests/test-results/version @@ -1,4 +1,4 @@ -cppfront compiler v0.8.0 Build 9B08:1148 +cppfront compiler v0.8.1 Build 10202:1808 SPDX-License-Identifier Apache-2.0 WITH LLVM-exception Copyright (c) 2022-2024 Herb Sutter