PR #2484: Support sanitizer build in GCC.

Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/2484

This is a workaround to address an issue when Abseil cannot compile using GCC with enabled sanitizer. See https://github.com/abseil/abseil-cpp/issues/1634 for more details.

Merge bb96bf3b3606962dc54d64ae1fb74cf47f759e81 into 86be10dfb67e80788634240aa32f6edd9945e8d0

Merging this change closes #2484

PiperOrigin-RevId: 1020987871
diff --git a/absl/container/internal/hash_policy_traits.h b/absl/container/internal/hash_policy_traits.h
index d0c211b..3ea4f9b 100644
--- a/absl/container/internal/hash_policy_traits.h
+++ b/absl/container/internal/hash_policy_traits.h
@@ -30,6 +30,19 @@
 ABSL_NAMESPACE_BEGIN
 namespace container_internal {
 
+// Avoid comparing function addresses with nullptr during constant evaluation,
+// which GCC rejects with UBSan enabled.
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71962
+template <HashSlotFn kPolicyFn, HashSlotFn kFallbackFn>
+struct HashSlotFnSelector {
+  static constexpr HashSlotFn kValue = kPolicyFn;
+};
+
+template <HashSlotFn kFallbackFn>
+struct HashSlotFnSelector<nullptr, kFallbackFn> {
+  static constexpr HashSlotFn kValue = kFallbackFn;
+};
+
 // Defines how slots are initialized/destroyed/moved.
 template <class Policy, class = void>
   struct hash_policy_traits : common_policy_traits<Policy> {
@@ -178,21 +191,10 @@
 
   template <class Hash, bool kIsAbsl, size_t kSeedShift>
   static constexpr HashSlotFn get_hash_slot_fn() {
-// get_hash_slot_fn may return nullptr to signal that non type erased function
-// should be used. GCC warns against comparing function address with nullptr.
-#if defined(__GNUC__) && !defined(__clang__)
-#pragma GCC diagnostic push
-// silent error: the address of * will never be NULL [-Werror=address]
-#pragma GCC diagnostic ignored "-Waddress"
-#endif
-    return Policy::template get_hash_slot_fn<Hash, kIsAbsl, kSeedShift>() ==
-                   nullptr
-               ? &hash_slot_fn_non_type_erased<Hash, kIsAbsl, kSeedShift>
-               : Policy::template get_hash_slot_fn<Hash, kIsAbsl,
-                                                   kSeedShift>();
-#if defined(__GNUC__) && !defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
+    // A null policy function selects the non-type-erased fallback.
+    return HashSlotFnSelector<
+        Policy::template get_hash_slot_fn<Hash, kIsAbsl, kSeedShift>(),
+        &hash_slot_fn_non_type_erased<Hash, kIsAbsl, kSeedShift>>::kValue;
   }
 
  private:
diff --git a/absl/container/internal/hash_policy_traits_test.cc b/absl/container/internal/hash_policy_traits_test.cc
index ebe8074..0646923 100644
--- a/absl/container/internal/hash_policy_traits_test.cc
+++ b/absl/container/internal/hash_policy_traits_test.cc
@@ -121,7 +121,7 @@
 
   Hash hasher;
   Slot value = 7;
-  auto* fn = hash_policy_traits<PolicyNoHashFn>::get_hash_slot_fn<
+  constexpr auto* fn = hash_policy_traits<PolicyNoHashFn>::get_hash_slot_fn<
       Hash, /*kIsAbsl=*/false, /*kSeedShift=*/6>();
   EXPECT_NE(fn, nullptr);
   EXPECT_EQ(fn(&hasher, &value, 100),
@@ -136,7 +136,7 @@
 
   Hash hasher;
   Slot value = 7;
-  auto* fn = hash_policy_traits<PolicyCustomHashFn>::get_hash_slot_fn<
+  constexpr auto* fn = hash_policy_traits<PolicyCustomHashFn>::get_hash_slot_fn<
       Hash, /*kIsAbsl=*/false, /*kSeedShift=*/6>();
   EXPECT_EQ(fn,
             (PolicyCustomHashFn::get_hash_slot_fn<Hash, /*kIsAbsl=*/false,