Skip to content

ghc-option -fspecialise-aggressively could lead to wrong callstack information

Summary

As it seems GHC with option -fspecialise-aggressively inlines message with "indirect" HasCallStack constraint (in the example below showcs2). This leads to incorrect callstack information.

Steps to reproduce

{-# LANGUAGE ConstraintKinds #-}
module Main2 where

import GHC.Stack.Types (getCallStack, HasCallStack)
import GHC.Stack (callStack)

main :: IO ()
main = 
    do 
        showcs
        showcs
        showcs2
        showcs2

showcs :: HasCallStack => IO ()
showcs = putStrLn $ show $ getCallStack callStack

showcs2 :: MyCS IO => IO ()
showcs2 = putStrLn $ show $ getCallStack callStack

type MyCS m = (HasCallStack, Monad m)

reports

[("showcs",SrcLoc {srcLocPackage = "main", srcLocModule = "Main2", srcLocFile = "app\\Main2.hs", srcLocStartLine = 10, srcLocStartCol = 9, srcLocEndLine = 10, srcLocEndCol = 15})]
[("showcs",SrcLoc {srcLocPackage = "main", srcLocModule = "Main2", srcLocFile = "app\\Main2.hs", srcLocStartLine = 11, srcLocStartCol = 9, srcLocEndLine = 11, srcLocEndCol = 15})]
[("showcs2",SrcLoc {srcLocPackage = "main", srcLocModule = "Main2", srcLocFile = "app\\Main2.hs", srcLocStartLine = 13, srcLocStartCol = 9, srcLocEndLine = 13, srcLocEndCol = 16})]
[("showcs2",SrcLoc {srcLocPackage = "main", srcLocModule = "Main2", srcLocFile = "app\\Main2.hs", srcLocStartLine = 13, srcLocStartCol = 9, srcLocEndLine = 13, srcLocEndCol = 16})]

Please note the identical line number for the last two log lines.

Expected behavior

Line 12 should be reported in the callstack for the first showcs2 call.

Workaround to enforce the expected behaviour

Simple add {-# NOINLINE showcs2 #-} to forbid the problematic inlining.

Environment

  • GHC version used: The Glorious Glasgow Haskell Compilation System, version 8.10.2

Optional:

  • Operating System: Windows 10 1909
  • System Architecture: x64

Please let me know of I was unclear or if I could provide additional information.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information