From 8a8f67816c3f8423be566b7885745f3b45b43505 Mon Sep 17 00:00:00 2001 From: Simon Hengel Date: Sat, 25 Jan 2025 20:24:01 +0700 Subject: [PATCH] Add tests for more GHC errors --- src/GHC/Diagnostic.hs | 14 ++++++++++---- test/GHC/DiagnosticSpec.hs | 1 + test/assets/use-BlockArguments/Foo.hs | 2 +- test/assets/use-TemplateHaskellQuotes/Foo.hs | 2 ++ test/assets/use-TemplateHaskellQuotes/err.json | 1 + test/assets/use-TemplateHaskellQuotes/err.out | 6 ++++++ 6 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 test/assets/use-TemplateHaskellQuotes/Foo.hs create mode 100644 test/assets/use-TemplateHaskellQuotes/err.json create mode 100644 test/assets/use-TemplateHaskellQuotes/err.out diff --git a/src/GHC/Diagnostic.hs b/src/GHC/Diagnostic.hs index 5caebf6c..af91e190 100644 --- a/src/GHC/Diagnostic.hs +++ b/src/GHC/Diagnostic.hs @@ -92,9 +92,15 @@ format diagnostic = encodeUtf8 . render $ unlines [ unlines = foldr ($+$) empty removeGhciSpecificHints :: Diagnostic -> Diagnostic -removeGhciSpecificHints diagnostic = diagnostic { hints = map f diagnostic.hints } +removeGhciSpecificHints diagnostic = diagnostic { hints = map processHint diagnostic.hints } where - f :: String -> String - f input = case lines input of - [hint, "You may enable this language extension in GHCi with:", ghciHint] | " :set -X" `isPrefixOf` ghciHint -> hint + isSetLanguageExtension :: String -> Bool + isSetLanguageExtension = isPrefixOf " :set -X" + + processHint :: String -> String + processHint input = case lines input of + [hint, "You may enable this language extension in GHCi with:", ghciHint] + | isSetLanguageExtension ghciHint -> hint + hint : "You may enable these language extensions in GHCi with:" : ghciHints + | all isSetLanguageExtension ghciHints -> hint _ -> input diff --git a/test/GHC/DiagnosticSpec.hs b/test/GHC/DiagnosticSpec.hs index 68813449..2e160966 100644 --- a/test/GHC/DiagnosticSpec.hs +++ b/test/GHC/DiagnosticSpec.hs @@ -50,6 +50,7 @@ spec = do test "variable-not-in-scope" test "variable-not-in-scope-perhaps-use" test "use-BlockArguments" + test "use-TemplateHaskellQuotes" test "non-existing" test "parse-error" test "lex-error" diff --git a/test/assets/use-BlockArguments/Foo.hs b/test/assets/use-BlockArguments/Foo.hs index de194c98..b8c2caa4 100644 --- a/test/assets/use-BlockArguments/Foo.hs +++ b/test/assets/use-BlockArguments/Foo.hs @@ -1,5 +1,5 @@ {-# LANGUAGE NoBlockArguments #-} -module BlockArguments.Foo where +module Foo where foo :: IO () foo = id do return () diff --git a/test/assets/use-TemplateHaskellQuotes/Foo.hs b/test/assets/use-TemplateHaskellQuotes/Foo.hs new file mode 100644 index 00000000..03b2235b --- /dev/null +++ b/test/assets/use-TemplateHaskellQuotes/Foo.hs @@ -0,0 +1,2 @@ +module Foo where +foo = [|23|] diff --git a/test/assets/use-TemplateHaskellQuotes/err.json b/test/assets/use-TemplateHaskellQuotes/err.json new file mode 100644 index 00000000..ef59c116 --- /dev/null +++ b/test/assets/use-TemplateHaskellQuotes/err.json @@ -0,0 +1 @@ +{"version":"1.0","ghcVersion":"ghc-9.10.1","span":{"file":"test/assets/use-TemplateHaskellQuotes/Foo.hs","start":{"line":2,"column":7},"end":{"line":2,"column":13}},"severity":"Error","code":62558,"message":["Syntax error on [| 23 |]","In the Template Haskell quotation [| 23 |]"],"hints":["Enable any of the following extensions: TemplateHaskell, TemplateHaskellQuotes\nYou may enable these language extensions in GHCi with:\n :set -XTemplateHaskell\n :set -XTemplateHaskellQuotes"]} diff --git a/test/assets/use-TemplateHaskellQuotes/err.out b/test/assets/use-TemplateHaskellQuotes/err.out new file mode 100644 index 00000000..a794148d --- /dev/null +++ b/test/assets/use-TemplateHaskellQuotes/err.out @@ -0,0 +1,6 @@ +test/assets/use-TemplateHaskellQuotes/Foo.hs:2:7: error: [GHC-62558] + • Syntax error on [| 23 |] + • In the Template Haskell quotation [| 23 |] + Suggested fix: + Enable any of the following extensions: TemplateHaskell, TemplateHaskellQuotes +