Skip to content

Commit 2f33cf2

Browse files
Fix partial function warnings in PacketStream
1 parent 390e056 commit 2f33cf2

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

clash-protocols/src/Protocols/PacketStream/Hedgehog.hs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ depacketizerModel toMetaOut ps = L.concat dataWidthPackets
216216
_ -> fwdF
217217

218218
hdr = bitCoerce $ Vec.unsafeFromList @headerBytes $ _data <$> hdrF
219-
metaOut = toMetaOut hdr (_meta $ L.head hdrF)
219+
metaIn = case hdrF of
220+
[] ->
221+
-- There are @headerBytes@ packets in this list, and (1 <= headerBytes)
222+
error "depacketizerModel: absurd"
223+
(hdrF0 : _) -> _meta hdrF0
224+
metaOut = toMetaOut hdr metaIn
220225

221226
bytePackets :: [[PacketStreamM2S 1 metaIn]]
222227
bytePackets =
@@ -257,10 +262,14 @@ depacketizeToDfModel ::
257262
depacketizeToDfModel toOut ps = L.map parseHdr bytePackets
258263
where
259264
parseHdr :: [PacketStreamM2S 1 metaIn] -> a
260-
parseHdr hdrF =
265+
parseHdr [] =
266+
-- There are at least @headerBytes@ packets in this list, and
267+
-- (1 <= headerBytes)
268+
error "depacketizeToDfModel: absurd"
269+
parseHdr hdrF@(hdrF0 : _) =
261270
toOut
262271
(bitCoerce $ Vec.unsafeFromList $ L.map _data hdrF)
263-
(_meta $ L.head hdrF)
272+
(_meta hdrF0)
264273

265274
bytePackets :: [[PacketStreamM2S 1 metaIn]]
266275
bytePackets =
@@ -313,9 +322,13 @@ packetizerModel ::
313322
packetizerModel toMetaOut toHeader ps = L.concatMap (upConvert . prependHdr) bytePackets
314323
where
315324
prependHdr :: [PacketStreamM2S 1 metaIn] -> [PacketStreamM2S 1 metaOut]
316-
prependHdr fragments = hdr L.++ L.map (\f -> f{_meta = metaOut}) fragments
325+
prependHdr [] =
326+
-- 'chunkBy' filters empty lists, so all elements of bytePackets are
327+
-- guaranteed to be non-null
328+
error "packetizerModel: Unreachable code"
329+
prependHdr fragments@(h : _) =
330+
hdr L.++ L.map (\f -> f{_meta = metaOut}) fragments
317331
where
318-
h = L.head fragments
319332
metaOut = toMetaOut (_meta h)
320333
hdr = L.map go (toList $ bitCoerce (toHeader (_meta h)))
321334
go byte = PacketStreamM2S (singleton byte) Nothing metaOut (_abort h)

clash-protocols/tests/Tests/Protocols/PacketStream/Padding.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ stripPaddingModel packets = L.concatMap go (chunkByPacket packets)
3333
go packet
3434
| packetBytes == expectedSize = packet
3535
| packetBytes > expectedSize =
36-
x L.++ [(L.head padding){_last = Just 0, _abort = any _abort padding}]
36+
case padding of
37+
[] ->
38+
-- There are (packetBytes - expectedSize) bytes, so more than 0
39+
error "stripPaddingModel: absurd"
40+
(padding0 : _) ->
41+
x L.++ [padding0{_last = Just 0, _abort = any _abort padding}]
3742
| otherwise = a L.++ [b{_abort = True}]
3843
where
3944
(a, b) = case L.unsnoc packet of

clash-protocols/tests/Tests/Protocols/PacketStream/Routing.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ makePropPacketArbiter SNat SNat mode =
6262
pure $ L.map (\pkt -> pkt{_meta = j}) pkts
6363

6464
partitionPackets packets =
65-
L.sortOn (_meta . L.head . L.head)
65+
L.sortOn getMeta
6666
$ L.groupBy (\a b -> _meta a == _meta b)
6767
<$> chunkByPacket packets
6868

69+
getMeta ((pkt : _) : _) = _meta pkt
70+
getMeta _ = error "makePropPacketArbiter: empty partition"
71+
6972
{- |
7073
Generic test function for the packet dispatcher, testing for all data widths,
7174
dispatch functions, and some meta types.

0 commit comments

Comments
 (0)