clash-compiler icon indicating copy to clipboard operation
clash-compiler copied to clipboard

Allow rich error messages in `clashCompileError` for simulation

Open gergoerdi opened this issue 6 months ago • 1 comments

Clash.Magic.clashCompileError only works if the argument is a literal string. But sometimes I want to provide a richer error message, for example I want to write this function:

ascii :: Char -> Word8
ascii c
    | code <= 0x7f = fromIntegral code
    | otherwise = clashCompileError (printf "Not an ASCII code point: %s (0x%08x)" (show c) code)
  where
    code = ord c

Now, of course this doesn't work at compile time. But it could work in simulation! So I propose a "two-level" clashCompileError: taking a static string (as a type parameter, perhaps?) and a runtime string, and using the former during compilation and the latter during simulation:

    | otherwise = clashCompileError @"Not an ASCII code point" $ printf "Not an ASCII code point: %s (0x%08x)" (show c) code

gergoerdi avatar Aug 11 '24 13:08 gergoerdi