summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Gröber <dxld@darkboxed.org>2020-03-18 22:50:47 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-07-22 20:18:11 -0400
commit7484a9a47ad277bb7e51c6357817f7e7c59e744a (patch)
tree0cbc0b8551b7cebf4229e5da6c26ef92da2954f7
parent8336ba78e00ec42521ba8314bc65ec766e6bcc7d (diff)
downloadhaskell-7484a9a47ad277bb7e51c6357817f7e7c59e744a.tar.gz
Encoding: Add comment about tricky ForeignPtr lifetime
-rw-r--r--compiler/GHC/Utils/Encoding.hs4
1 files changed, 4 insertions, 0 deletions
diff --git a/compiler/GHC/Utils/Encoding.hs b/compiler/GHC/Utils/Encoding.hs
index 96531e7cd1..113620e895 100644
--- a/compiler/GHC/Utils/Encoding.hs
+++ b/compiler/GHC/Utils/Encoding.hs
@@ -158,6 +158,10 @@ utf8DecodeStringLazy fp offset (I# len#)
= unsafeDupablePerformIO $ withForeignPtr fp $ \ptr ->
let !(Ptr a#) = ptr `plusPtr` offset in
utf8DecodeLazy# (touchForeignPtr fp) (utf8DecodeCharAddr# a#) len#
+-- Note that since utf8DecodeLazy# returns a thunk the lifetime of the
+-- ForeignPtr actually needs to be longer than the lexical lifetime
+-- withForeignPtr would provide here. That's why we use touchForeignPtr to
+-- keep the fp alive until the last character has actually been decoded.
utf8DecodeShortByteString :: ShortByteString -> [Char]
utf8DecodeShortByteString (SBS ba#)