diff --git a/haskell-gi-base.cabal b/haskell-gi-base.cabal
--- a/haskell-gi-base.cabal
+++ b/haskell-gi-base.cabal
@@ -1,5 +1,5 @@
 name:                haskell-gi-base
-version:             0.18.2
+version:             0.18.3
 synopsis:            Foundation for libraries generated by haskell-gi
 description:         Foundation for libraries generated by haskell-gi
 homepage:            https://github.com/haskell-gi/haskell-gi-base
diff --git a/src/Data/GI/Base/ManagedPtr.hs b/src/Data/GI/Base/ManagedPtr.hs
--- a/src/Data/GI/Base/ManagedPtr.hs
+++ b/src/Data/GI/Base/ManagedPtr.hs
@@ -1,4 +1,7 @@
 {-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-}
+-- For HasCallStack compatibility
+{-# LANGUAGE ImplicitParams, KindSignatures, ConstraintKinds #-}
+
 -- | We wrap most objects in a "managed pointer", which is simply a
 -- newtype for a 'ForeignPtr' of the appropriate type:
 --
@@ -55,6 +58,17 @@
 import Data.GI.Base.BasicTypes
 import Data.GI.Base.Utils
 
+#if MIN_VERSION_base(4,9,0)
+import GHC.Stack (HasCallStack)
+#elif MIN_VERSION_base(4,8,0)
+import GHC.Stack (CallStack)
+import GHC.Exts (Constraint)
+type HasCallStack = ((?callStack :: CallStack) :: Constraint)
+#else
+import GHC.Exts (Constraint)
+type HasCallStack = (() :: Constraint)
+#endif
+
 -- | Perform an IO action on the 'Ptr' inside a managed pointer.
 withManagedPtr :: ForeignPtrNewtype a => a -> (Ptr a -> IO c) -> IO c
 withManagedPtr managed action = do
@@ -123,7 +137,7 @@
 
 -- | Cast to the given type, assuming that the cast will succeed. This
 -- function will call `error` if the cast is illegal.
-unsafeCastTo :: forall o o'. (GObject o, GObject o') =>
+unsafeCastTo :: forall o o'. (HasCallStack, GObject o, GObject o') =>
                 (ForeignPtr o' -> o') -> o -> IO o'
 unsafeCastTo constructor obj =
   withManagedPtr obj $ \objPtr -> do
