On 01.06.26 02:55, Tom Lane wrote:
I wrote:
Here's a patch to do $SUBJECT.

cfbot didn't like v1 too much, here's a v2.

I know you have already reported this upstream, but here are some patches to silence the resulting warnings on MSVC.

Patch 0001 just silences the warnings using pragmas.

Patch 0002 is to silence the "unary minus operator applied to unsigned type" warning globally for PostgreSQL. AFAICT, there is no equivalent warning available on gcc or clang, so this would remain a trap on MSVC.

Patch 0003 is to silence the cross-enum comparison warnings using casts (as you had suggested to upstream). I confirmed that this works in a small test case on Compiler Explorer (godbolt.org), but if I push this branch to GitHub Actions CI, the warning remains, so I don't know about this one.
From daf36e0df73a817cbe84f0083b89ac4d6868a935 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Sat, 11 Jul 2026 13:56:51 +0200
Subject: [PATCH 1/3] Fix MSVC warnings from new timezone code

The new timezone code from commit aeb07c55fab introduced a few new
compiler warnings from MSVC:

../src/timezone/zic.c(353): warning C5287: operands are different enum types 
'<unnamed-enum-RF_NAME>' and '<unnamed-enum-LF_TARGET>'; use an explicit cast 
to silence this warning
../src/timezone/zic.c(353): warning C5287: operands are different enum types 
'<unnamed-enum-RF_NAME>' and '<unnamed-enum-LP_YEAR>'; use an explicit cast to 
silence this warning
../src/timezone/zic.c(1654): warning C4146: unary minus operator applied to 
unsigned type, result still unsigned

Silence these warnings with pragmas.
---
 src/timezone/zic.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/timezone/zic.c b/src/timezone/zic.c
index 1f89e220730..38411220e01 100644
--- a/src/timezone/zic.c
+++ b/src/timezone/zic.c
@@ -350,8 +350,15 @@ enum
  */
 enum
 {
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable: 5287)
+#endif
        MAX_FIELDS = max(max(+RULE_FIELDS, +LINK_FIELDS),
                                         max(+LEAP_FIELDS, +EXPIRES_FIELDS))
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
 };
 
 /*
@@ -1651,7 +1658,14 @@ random_dirent(char const **name, char **namealloc)
         * value of ((UINTMAX_MAX + 1) - (UINTMAX_MAX + 1) % BASE**6) computed
         * without overflow.
         */
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable: 4146)
+#endif
        uint_fast64_t unfair_min = -((UINTMAX_MAX % base__6 + 1) % base__6);
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
 
        if (!dst)
        {
-- 
2.55.0

From e7b1cc6a5b3b1446b5aaf692a80111982f0f88e3 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Sat, 11 Jul 2026 14:11:45 +0200
Subject: [PATCH 2/3] Disable MSVC warning C4146 globally

---
 meson.build         | 1 +
 src/port/snprintf.c | 8 --------
 src/timezone/zic.c  | 7 -------
 3 files changed, 1 insertion(+), 15 deletions(-)

diff --git a/meson.build b/meson.build
index d88a7a70308..8fe2307c1df 100644
--- a/meson.build
+++ b/meson.build
@@ -2317,6 +2317,7 @@ if cc.get_id() == 'msvc'
 
     # Warnings to disable:
     # from /W2:
+    '/wd4146', # unary minus operator applied to unsigned type, result still 
unsigned
     '/wd4244', # conversion from 'type1' to 'type2', possible loss of data
 
     # Additional warnings to enable:
diff --git a/src/port/snprintf.c b/src/port/snprintf.c
index 2189ebfe399..c4611f6ab0b 100644
--- a/src/port/snprintf.c
+++ b/src/port/snprintf.c
@@ -1056,19 +1056,11 @@ fmtint(long long value, char type, int forcesign, int 
leftjust,
                        return;                         /* keep compiler quiet 
*/
        }
 
-       /* disable MSVC warning about applying unary minus to an unsigned value 
*/
-#ifdef _MSC_VER
-#pragma warning(push)
-#pragma warning(disable: 4146)
-#endif
        /* Handle +/- */
        if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
                uvalue = -(unsigned long long) value;
        else
                uvalue = (unsigned long long) value;
-#ifdef _MSC_VER
-#pragma warning(pop)
-#endif
 
        /*
         * SUS: the result of converting 0 with an explicit precision of 0 is no
diff --git a/src/timezone/zic.c b/src/timezone/zic.c
index 38411220e01..b61082dfdf1 100644
--- a/src/timezone/zic.c
+++ b/src/timezone/zic.c
@@ -1658,14 +1658,7 @@ random_dirent(char const **name, char **namealloc)
         * value of ((UINTMAX_MAX + 1) - (UINTMAX_MAX + 1) % BASE**6) computed
         * without overflow.
         */
-#ifdef _MSC_VER
-#pragma warning(push)
-#pragma warning(disable: 4146)
-#endif
        uint_fast64_t unfair_min = -((UINTMAX_MAX % base__6 + 1) % base__6);
-#ifdef _MSC_VER
-#pragma warning(pop)
-#endif
 
        if (!dst)
        {
-- 
2.55.0

From 8ffc496b1144a78056c3c3b0cd36e9c94db3208d Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Sat, 11 Jul 2026 14:15:31 +0200
Subject: [PATCH 3/3] Use a different trick to silence warnings about
 cross-enum comparisons

---
 src/timezone/zic.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/src/timezone/zic.c b/src/timezone/zic.c
index b61082dfdf1..31fe10c0280 100644
--- a/src/timezone/zic.c
+++ b/src/timezone/zic.c
@@ -346,19 +346,12 @@ enum
 
 /*
  * The maximum number of fields on any of the above lines.
- * (The "+"s pacify gcc -Wenum-compare.)
+ * (The casts pacify gcc -Wenum-compare.)
  */
 enum
 {
-#ifdef _MSC_VER
-#pragma warning(push)
-#pragma warning(disable: 5287)
-#endif
-       MAX_FIELDS = max(max(+RULE_FIELDS, +LINK_FIELDS),
-                                        max(+LEAP_FIELDS, +EXPIRES_FIELDS))
-#ifdef _MSC_VER
-#pragma warning(pop)
-#endif
+       MAX_FIELDS = max(max((int) RULE_FIELDS, (int) LINK_FIELDS),
+                                        max((int) LEAP_FIELDS, (int) 
EXPIRES_FIELDS))
 };
 
 /*
-- 
2.55.0

Reply via email to