summaryrefslogtreecommitdiff
path: root/compiler/GHC/Utils/Misc.hs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/GHC/Utils/Misc.hs')
-rw-r--r--compiler/GHC/Utils/Misc.hs14
1 files changed, 13 insertions, 1 deletions
diff --git a/compiler/GHC/Utils/Misc.hs b/compiler/GHC/Utils/Misc.hs
index f7168190e4..abd85b6b66 100644
--- a/compiler/GHC/Utils/Misc.hs
+++ b/compiler/GHC/Utils/Misc.hs
@@ -43,7 +43,7 @@ module GHC.Utils.Misc (
listLengthCmp, atLength,
equalLength, compareLength, leLength, ltLength,
- isSingleton, only, GHC.Utils.Misc.singleton,
+ isSingleton, only, expectOnly, GHC.Utils.Misc.singleton,
notNull, snocView,
isIn, isn'tIn,
@@ -563,6 +563,18 @@ only (a:_) = a
#endif
only _ = panic "Util: only"
+-- | Extract the single element of a list and panic with the given message if
+-- there are more elements or the list was empty.
+-- Like 'expectJust', but for lists.
+expectOnly :: HasCallStack => String -> [a] -> a
+{-# INLINE expectOnly #-}
+#if defined(DEBUG)
+expectOnly _ [a] = a
+#else
+expectOnly _ (a:_) = a
+#endif
+expectOnly msg _ = panic ("expectOnly: " ++ msg)
+
-- Debugging/specialising versions of \tr{elem} and \tr{notElem}
# if !defined(DEBUG)