Skip to content

Commit 3fdf075

Browse files
sjakobimergify[bot]
authored andcommitted
Output the context of a type error with syntax highlighting (#1556)
1 parent e12dd9a commit 3fdf075

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

dhall/src/Dhall/TypeCheck.hs

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,21 +4252,23 @@ data TypeError s a = TypeError
42524252
}
42534253

42544254
instance (Eq a, Pretty s, Pretty a) => Show (TypeError s a) where
4255-
show = Pretty.renderString . Dhall.Pretty.layout . Pretty.pretty
4255+
show = Pretty.renderString . Dhall.Pretty.layout . prettyTypeError
42564256

42574257
instance (Eq a, Pretty s, Pretty a, Typeable s, Typeable a) => Exception (TypeError s a)
42584258

42594259
instance (Eq a, Pretty s, Pretty a) => Pretty (TypeError s a) where
4260-
pretty (TypeError _ expr msg)
4261-
= Pretty.unAnnotate
4262-
( "\n"
4263-
<> shortTypeMessage msg <> "\n"
4264-
<> source
4265-
)
4266-
where
4267-
source = case expr of
4268-
Note s _ -> pretty s
4269-
_ -> mempty
4260+
pretty = Pretty.unAnnotate . prettyTypeError
4261+
4262+
prettyTypeError :: (Eq a, Pretty s, Pretty a) => TypeError s a -> Doc Ann
4263+
prettyTypeError (TypeError _ expr msg) =
4264+
( "\n"
4265+
<> shortTypeMessage msg <> "\n"
4266+
<> source
4267+
)
4268+
where
4269+
source = case expr of
4270+
Note s _ -> pretty s
4271+
_ -> mempty
42704272

42714273
{-| Wrap a type error in this exception type to censor source code and
42724274
`Text` literals from the error message
@@ -4425,36 +4427,39 @@ newtype DetailedTypeError s a = DetailedTypeError (TypeError s a)
44254427
deriving (Typeable)
44264428

44274429
instance (Eq a, Pretty s, Pretty a) => Show (DetailedTypeError s a) where
4428-
show = Pretty.renderString . Dhall.Pretty.layout . Pretty.pretty
4430+
show = Pretty.renderString . Dhall.Pretty.layout . prettyDetailedTypeError
44294431

44304432
instance (Eq a, Pretty s, Pretty a, Typeable s, Typeable a) => Exception (DetailedTypeError s a)
44314433

44324434
instance (Eq a, Pretty s, Pretty a) => Pretty (DetailedTypeError s a) where
4433-
pretty (DetailedTypeError (TypeError ctx expr msg))
4434-
= Pretty.unAnnotate
4435-
( "\n"
4436-
<> ( if null (Dhall.Context.toList ctx)
4437-
then ""
4438-
else prettyContext ctx <> "\n\n"
4439-
)
4440-
<> longTypeMessage msg <> "\n"
4441-
<> "────────────────────────────────────────────────────────────────────────────────\n"
4442-
<> "\n"
4443-
<> source
4444-
)
4445-
where
4446-
prettyKV (key, val) =
4447-
pretty key <> " : " <> Dhall.Util.snipDoc (pretty val)
4448-
4449-
prettyContext =
4450-
Pretty.vsep
4451-
. map prettyKV
4452-
. reverse
4453-
. Dhall.Context.toList
4454-
4455-
source = case expr of
4456-
Note s _ -> pretty s
4457-
_ -> mempty
4435+
pretty = Pretty.unAnnotate . prettyDetailedTypeError
4436+
4437+
prettyDetailedTypeError :: (Eq a, Pretty s, Pretty a) => DetailedTypeError s a -> Doc Ann
4438+
prettyDetailedTypeError (DetailedTypeError (TypeError ctx expr msg)) =
4439+
( "\n"
4440+
<> ( if null (Dhall.Context.toList ctx)
4441+
then ""
4442+
else prettyContext ctx <> "\n\n"
4443+
)
4444+
<> longTypeMessage msg <> "\n"
4445+
<> "────────────────────────────────────────────────────────────────────────────────\n"
4446+
<> "\n"
4447+
<> source
4448+
)
4449+
where
4450+
prettyKV (key, val) =
4451+
Dhall.Util.snipDoc
4452+
(Dhall.Pretty.Internal.prettyLabel key <> " : " <> Dhall.Pretty.prettyExpr val)
4453+
4454+
prettyContext =
4455+
Pretty.vsep
4456+
. map prettyKV
4457+
. reverse
4458+
. Dhall.Context.toList
4459+
4460+
source = case expr of
4461+
Note s _ -> pretty s
4462+
_ -> mempty
44584463

44594464
{-| This function verifies that a custom context is well-formed so that
44604465
type-checking will not loop

dhall/src/Dhall/Util.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import Dhall.Src (Src)
3131
import qualified Control.Exception
3232
import qualified Data.Text
3333
import qualified Data.Text.IO
34-
import qualified Data.Text.Prettyprint.Doc as Pretty
35-
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty
34+
import qualified Data.Text.Prettyprint.Doc as Pretty
35+
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
3636
import qualified Dhall.Parser
3737
import qualified Dhall.Pretty
3838

@@ -81,7 +81,7 @@ snipDoc doc = Pretty.align (Pretty.pretty (snip text))
8181

8282
ansiStream = fmap Dhall.Pretty.annToAnsiStyle stream
8383

84-
text = Pretty.renderStrict ansiStream
84+
text = Pretty.Terminal.renderStrict ansiStream
8585

8686
takeEnd :: Int -> [a] -> [a]
8787
takeEnd n l = go (drop n l) l

0 commit comments

Comments
 (0)