Skip to content

Commit

Permalink
allow getting type errors without having to kill the interpreter
Browse files Browse the repository at this point in the history
Added typeChecksWithDetails and exported onCompilationError through Hint.Internal.
Exporting the latter allows others to catch errors on their own. Also added a relevant
note on -fdefer-type-errors.

Fixes #24.
  • Loading branch information
Wizek authored and mvdan committed Jan 29, 2018
1 parent c6fdd3c commit fa57ce8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions hint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ library
exposed-modules: Language.Haskell.Interpreter
Language.Haskell.Interpreter.Extension
Language.Haskell.Interpreter.Unsafe
Hint.Internal
other-modules: Hint.GHC
Hint.Base
Hint.InterpreterT
Expand Down
33 changes: 33 additions & 0 deletions src/Hint/Internal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- | In this module we intend to export some internal functions.
--
-- __Important note__: the authors of this library imply no assurance whatsoever
-- of the stability or functionality of the API exposed here, and compatibility
-- may break even by minor version changes. Rely on these at your
-- own risk.
--
-- The reason for showing them here is to aid discoverability
-- of already written code and prevent having to reinvent the wheel from
-- scratch if said wheel is already invented.
--
-- In case you find something here especially useful, please submit
-- an issue or a pull request at https://github.com/mvdan/hint so
-- we can discuss making it part of the official public API.
--
-- Some further context can be found here:
-- https://github.com/mvdan/hint/pull/48#issuecomment-358722638



module Hint.Internal (
onCompilationError
) where

import Hint.Typecheck (onCompilationError)



-- todo: Consider refactoring like the following when
-- https://github.com/haskell/haddock/issues/563 is fixed
--
-- module Hint.Internal (module ReExport) where
-- import Hint.Typecheck as ReExport (onCompilationError)
12 changes: 11 additions & 1 deletion src/Hint/Typecheck.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Hint.Typecheck (
typeOf, typeChecks, kindOf, normalizeType
typeOf, typeChecks, kindOf, normalizeType, onCompilationError, typeChecksWithDetails
) where

import Control.Monad.Catch
Expand All @@ -23,11 +23,21 @@ typeOf expr =
typeToString ty

-- | Tests if the expression type checks.
--
-- NB. Be careful if there is `-fdefer-type-errors` involved.
-- Perhaps unsurprisingly, that can falsely make @typeChecks@ and @getType@
-- return @True@ and @Right _@ respectively.
typeChecks :: MonadInterpreter m => String -> m Bool
typeChecks expr = (typeOf expr >> return True)
`catchIE`
onCompilationError (\_ -> return False)

-- | Similar to @typeChecks@, but gives more information, e.g. the type errors.
typeChecksWithDetails :: MonadInterpreter m => String -> m (Either [GhcError] String)
typeChecksWithDetails expr = (typeOf expr >>= return . Right)
`catchIE`
onCompilationError (\a -> return (Left a))

-- | Returns a string representation of the kind of the type expression.
kindOf :: MonadInterpreter m => String -> m String
kindOf type_expr =
Expand Down
1 change: 1 addition & 0 deletions src/Language/Haskell/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Language.Haskell.Interpreter(
-- pragmas inline in the code since GHC scarfs them up.
getModuleAnnotations, getValAnnotations,
-- ** Type inference
typeChecksWithDetails,
typeOf, typeChecks, kindOf, normalizeType,
-- ** Evaluation
interpret, as, infer, eval, runStmt,
Expand Down

0 comments on commit fa57ce8

Please sign in to comment.