Error eagerly after renaming failures in reifyInstances
authorRyan Scott <ryan.gl.scott@gmail.com>
Fri, 28 Jul 2017 15:35:22 +0000 (11:35 -0400)
committerRyan Scott <ryan.gl.scott@gmail.com>
Fri, 28 Jul 2017 15:35:22 +0000 (11:35 -0400)
Summary:
Previously, if `reifyInstances` failed to discover a `Name` during
renaming, it would blindy charge into typechecking, at which point GHC would
become very confused at the absence of that `Name` and throw an internal error.
A simple workaround is to fail eagerly after renaming errors.

Test Plan: make test TEST=T13837

Reviewers: goldfire, austin, bgamari, simonpj

Reviewed By: simonpj

Subscribers: simonpj, rwbarton, thomie

GHC Trac Issues: #13837

Differential Revision: https://phabricator.haskell.org/D3793

compiler/typecheck/TcSplice.hs
testsuite/tests/th/T13837.hs [new file with mode: 0644]
testsuite/tests/th/T13837.stderr [new file with mode: 0644]
testsuite/tests/th/all.T

index 824227a..266a4df 100644 (file)
@@ -1138,7 +1138,11 @@ reifyInstances th_nm th_tys
         ; let tv_rdrs = freeKiTyVarsAllVars free_vars
           -- Rename  to HsType Name
         ; ((tv_names, rn_ty), _fvs)
-            <- bindLRdrNames tv_rdrs $ \ tv_names ->
+            <- checkNoErrs $ -- If there are out-of-scope Names here, then we
+                             -- must error before proceeding to typecheck the
+                             -- renamed type, as that will result in GHC
+                             -- internal errors (#13837).
+               bindLRdrNames tv_rdrs $ \ tv_names ->
                do { (rn_ty, fvs) <- rnLHsType doc rdr_ty
                   ; return ((tv_names, rn_ty), fvs) }
         ; (_tvs, ty)
diff --git a/testsuite/tests/th/T13837.hs b/testsuite/tests/th/T13837.hs
new file mode 100644 (file)
index 0000000..3d33341
--- /dev/null
@@ -0,0 +1,10 @@
+{-# LANGUAGE TemplateHaskell #-}
+module T13837 where
+
+import Language.Haskell.TH.Lib
+import Language.Haskell.TH.Syntax
+
+test_local_tyfam_expansion :: String
+test_local_tyfam_expansion =
+  $(do fam_name <- newName "Fam"
+       stringE . show =<< qReifyInstances fam_name [])
diff --git a/testsuite/tests/th/T13837.stderr b/testsuite/tests/th/T13837.stderr
new file mode 100644 (file)
index 0000000..53700b5
--- /dev/null
@@ -0,0 +1,10 @@
+
+T13837.hs:9:5: error:
+    • The exact Name ‘Fam’ is not in scope
+        Probable cause: you used a unique Template Haskell name (NameU), 
+        perhaps via newName, but did not bind it
+        If that's it, then -ddump-splices might be useful
+    • In the argument of reifyInstances: Fam_0
+      In the untyped splice:
+        $(do fam_name <- newName "Fam"
+             stringE . show =<< qReifyInstances fam_name [])
index df31162..b52042b 100644 (file)
@@ -389,5 +389,6 @@ test('T13618', normal, compile_and_run, ['-v0'])
 test('T13642', normal, compile_fail, ['-v0'])
 test('T13781', normal, compile, ['-v0'])
 test('T13782', normal, compile, [''])
+test('T13837', normal, compile_fail, ['-v0 -dsuppress-uniques'])
 test('T13856', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
 test('T13968', normal, compile_fail, ['-v0'])