From 0c002200bbee27acc8a663075da69120f666b4e5 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 18 May 2023 18:32:41 -0700 Subject: [PATCH] [CollectionUtilities] Silence a warning on 32 bit platforms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out `UInt.bitWidth` is getting too eagerly inlined, so conditions based on it trigger spurious “code will never be executed” warnings. Silence those by switching to looking at `MemoryLayout`, which doesn’t get turned into a constant *that* early. --- .../IntegerTricks/Integer rank.swift.gyb | 5 ++++- .../IntegerTricks/autogenerated/Integer rank.swift | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/_CollectionsUtilities/IntegerTricks/Integer rank.swift.gyb b/Sources/_CollectionsUtilities/IntegerTricks/Integer rank.swift.gyb index de75e1f7..59ad49ed 100644 --- a/Sources/_CollectionsUtilities/IntegerTricks/Integer rank.swift.gyb +++ b/Sources/_CollectionsUtilities/IntegerTricks/Integer rank.swift.gyb @@ -41,13 +41,16 @@ extension UInt { var shift: Self = 0 var n = Self(truncatingIfNeeded: n) - if Self.bitWidth == 64 { + if MemoryLayout.size == 8 { let c32 = (self & 0xFFFFFFFF)._nonzeroBitCount if n >= c32 { shift &+= 32 n &-= c32 } + } else { + assert(MemoryLayout.size == 4, "Unknown platform") } + let c16 = ((self &>> shift) & 0xFFFF)._nonzeroBitCount if n >= c16 { shift &+= 16 diff --git a/Sources/_CollectionsUtilities/IntegerTricks/autogenerated/Integer rank.swift b/Sources/_CollectionsUtilities/IntegerTricks/autogenerated/Integer rank.swift index 02790d35..07ede453 100644 --- a/Sources/_CollectionsUtilities/IntegerTricks/autogenerated/Integer rank.swift +++ b/Sources/_CollectionsUtilities/IntegerTricks/autogenerated/Integer rank.swift @@ -47,13 +47,16 @@ extension UInt { var shift: Self = 0 var n = Self(truncatingIfNeeded: n) - if Self.bitWidth == 64 { + if MemoryLayout.size == 8 { let c32 = (self & 0xFFFFFFFF)._nonzeroBitCount if n >= c32 { shift &+= 32 n &-= c32 } + } else { + assert(MemoryLayout.size == 4, "Unknown platform") } + let c16 = ((self &>> shift) & 0xFFFF)._nonzeroBitCount if n >= c16 { shift &+= 16 @@ -184,13 +187,16 @@ extension UInt { var shift: Self = 0 var n = Self(truncatingIfNeeded: n) - if Self.bitWidth == 64 { + if MemoryLayout.size == 8 { let c32 = (self & 0xFFFFFFFF)._nonzeroBitCount if n >= c32 { shift &+= 32 n &-= c32 } + } else { + assert(MemoryLayout.size == 4, "Unknown platform") } + let c16 = ((self &>> shift) & 0xFFFF)._nonzeroBitCount if n >= c16 { shift &+= 16