Immediately, the alternative of introducing bound variables in the environment that is available to rewrite rules comes to mind as a more general way of doing this.
So this example from the GHC docs:
{-# RULES
"map/map" forall f g xs. map f (map g xs) = map (f.g) xs
"map/append" forall f xs ys. map f (xs ++ ys) = map f xs ++ map f ys
#-}
For some source:
map f (map g xs)
it is translated into:
let location = "somefile.hs:234"
in map (f.g) xs
So for error:
{-# RULES
"error/location" error s = errorLoc location s
#-}
is translated into:
let location = "somefile.hs:345"
in errorLoc location s
Alexander