Skip to content

[clang][test] Avoid some C++14 warnings in discrim-union.cpp #146829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Jul 3, 2025

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.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jul 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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 &amp;get(select&lt;0&gt;) { 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&lt;unsigned N&gt; constexpr const decltype(static_cast&lt;const rest_t&amp;&gt;(rest).get(select&lt;N-1&gt;{})) get(select&lt;N&gt;) {
      |                                                                                                        ^
      |                                                                                                                       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&lt;N&gt; 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 &amp;get() {
      |                      ^
      |                            const
5 warnings generated.

Full diff: https://github.com/llvm/llvm-project/pull/146829.diff

1 Files Affected:

  • (modified) clang/test/SemaCXX/discrim-union.cpp (+5-5)
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>())>();
   }
 };

@cor3ntin
Copy link
Contributor

cor3ntin commented Jul 3, 2025

Can we add -verify to the runline (+ expected-no-diagnostics)?

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.
@tbaederr
Copy link
Contributor Author

tbaederr commented Jul 3, 2025

yes, done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category test-suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants