https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/146829
Before this patch, we emitted a bunch of irrelevant (to this test) warnings: ``` ../clang/test/SemaCXX/discrim-union.cpp:49:24: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 49 | constexpr const T &get(select<0>) { return val; } | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:50:104: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 50 | template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) { | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:82:22: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 82 | constexpr unsigned index() noexcept { return elem; } | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:88:33: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 88 | constexpr const_get_result<N> get() { | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:96:22: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 96 | constexpr const U &get() { | ^ | const 5 warnings generated. ``` >From 8f95d47d40b20d25074cbcf7597f8e233472f4ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com> Date: Thu, 3 Jul 2025 09:57:40 +0200 Subject: [PATCH] [clang][test] Avoid some C++14 warnings in discrim-union.cpp Before this patch, we emitted a bunch of irrelevant (to this test) warnings: ../clang/test/SemaCXX/discrim-union.cpp:49:24: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 49 | constexpr const T &get(select<0>) { return val; } | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:50:104: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 50 | template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) { | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:82:22: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 82 | constexpr unsigned index() noexcept { return elem; } | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:88:33: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 88 | constexpr const_get_result<N> get() { | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:96:22: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 96 | constexpr const U &get() { | ^ | const 5 warnings generated. --- clang/test/SemaCXX/discrim-union.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clang/test/SemaCXX/discrim-union.cpp b/clang/test/SemaCXX/discrim-union.cpp index 15c9a225ed9a9..9877b70205104 100644 --- a/clang/test/SemaCXX/discrim-union.cpp +++ b/clang/test/SemaCXX/discrim-union.cpp @@ -46,8 +46,8 @@ namespace detail { val.~T(); } - constexpr const T &get(select<0>) { return val; } - template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) { + constexpr const T &get(select<0>) const { return val; } + template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) const { return rest.get(select<N-1>{}); } }; @@ -79,13 +79,13 @@ class either { // FIXME: declare a destructor iff any element has a nontrivial destructor //~either() { impl.destroy(elem); } - constexpr unsigned index() noexcept { return elem; } + constexpr unsigned index() const noexcept { return elem; } template<unsigned N> using const_get_result = decltype(static_cast<const impl_t&>(impl).get(detail::select<N>{})); template<unsigned N> - constexpr const_get_result<N> get() { + constexpr const_get_result<N> get() const { // Can't just use throw here, since that makes the conditional a prvalue, // which means we return a reference to a temporary. return (elem != N ? throw_<const_get_result<N>>("bad_either_get") @@ -93,7 +93,7 @@ class either { } template<typename U> - constexpr const U &get() { + constexpr const U &get() const { return get<impl_t::index(detail::type<U>())>(); } }; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits