summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsheaf <sam.derbyshire@gmail.com>2021-05-02 04:03:38 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-05-03 19:52:34 -0400
commit24a9b1708cee95670e7ec2a6ceb68e29fc376cf7 (patch)
treeb6c5ffadf9d3bb8c999070886103d7fffbd0fe19
parent7f5ee7194e18cc2c922839260817c16db8e95832 (diff)
downloadhaskell-24a9b1708cee95670e7ec2a6ceb68e29fc376cf7.tar.gz
Improve hs-boot binds error (#19781)
-rw-r--r--compiler/GHC/Rename/Bind.hs27
-rw-r--r--compiler/GHC/Rename/Module.hs8
-rw-r--r--testsuite/tests/rename/should_fail/T19781.hs6
-rw-r--r--testsuite/tests/rename/should_fail/T19781.stderr6
-rw-r--r--testsuite/tests/rename/should_fail/T19781_A.hs7
-rw-r--r--testsuite/tests/rename/should_fail/T19781_A.hs-boot7
-rw-r--r--testsuite/tests/rename/should_fail/all.T1
7 files changed, 52 insertions, 10 deletions
diff --git a/compiler/GHC/Rename/Bind.hs b/compiler/GHC/Rename/Bind.hs
index a37f88bc83..0dcd51637b 100644
--- a/compiler/GHC/Rename/Bind.hs
+++ b/compiler/GHC/Rename/Bind.hs
@@ -18,7 +18,7 @@ they may be affected by renaming (which isn't fully worked out yet).
module GHC.Rename.Bind (
-- Renaming top-level bindings
- rnTopBindsLHS, rnTopBindsBoot, rnValBindsRHS,
+ rnTopBindsLHS, rnTopBindsLHSBoot, rnTopBindsBoot, rnValBindsRHS,
-- Renaming local bindings
rnLocalBindsAndThen, rnLocalValBindsLHS, rnLocalValBindsRHS,
@@ -187,13 +187,24 @@ rnTopBindsLHS :: MiniFixityEnv
rnTopBindsLHS fix_env binds
= rnValBindsLHS (topRecNameMaker fix_env) binds
+-- Ensure that a hs-boot file has no top-level bindings.
+rnTopBindsLHSBoot :: MiniFixityEnv
+ -> HsValBinds GhcPs
+ -> RnM (HsValBindsLR GhcRn GhcPs)
+rnTopBindsLHSBoot fix_env binds
+ = do { topBinds <- rnTopBindsLHS fix_env binds
+ ; case topBinds of
+ ValBinds x mbinds sigs ->
+ do { mapM_ bindInHsBootFileErr mbinds
+ ; pure (ValBinds x emptyBag sigs) }
+ _ -> pprPanic "rnTopBindsLHSBoot" (ppr topBinds) }
+
rnTopBindsBoot :: NameSet -> HsValBindsLR GhcRn GhcPs
-> RnM (HsValBinds GhcRn, DefUses)
-- A hs-boot file has no bindings.
-- Return a single HsBindGroup with empty binds and renamed signatures
-rnTopBindsBoot bound_names (ValBinds _ mbinds sigs)
- = do { checkErr (isEmptyLHsBinds mbinds) (bindsInHsBootFile mbinds)
- ; (sigs', fvs) <- renameSigs (HsBootCtxt bound_names) sigs
+rnTopBindsBoot bound_names (ValBinds _ _ sigs)
+ = do { (sigs', fvs) <- renameSigs (HsBootCtxt bound_names) sigs
; return (XValBindsLR (NValBinds [] sigs'), usesOnly fvs) }
rnTopBindsBoot _ b = pprPanic "rnTopBindsBoot" (ppr b)
@@ -1322,10 +1333,10 @@ defaultSigErr sig = vcat [ hang (text "Unexpected default signature:")
2 (ppr sig)
, text "Use DefaultSignatures to enable default signatures" ]
-bindsInHsBootFile :: LHsBindsLR GhcRn GhcPs -> SDoc
-bindsInHsBootFile mbinds
- = hang (text "Bindings in hs-boot files are not allowed")
- 2 (ppr mbinds)
+bindInHsBootFileErr :: LHsBindLR GhcRn GhcPs -> RnM ()
+bindInHsBootFileErr (L loc _)
+ = addErrAt (locA loc) $
+ vcat [ text "Bindings in hs-boot files are not allowed" ]
nonStdGuardErr :: (Outputable body,
Anno (Stmt GhcRn body) ~ SrcSpanAnnA)
diff --git a/compiler/GHC/Rename/Module.hs b/compiler/GHC/Rename/Module.hs
index 8de0c4a34f..1b54b53716 100644
--- a/compiler/GHC/Rename/Module.hs
+++ b/compiler/GHC/Rename/Module.hs
@@ -140,7 +140,12 @@ rnSrcDecls group@(HsGroup { hs_valds = val_decls,
-- (D2) Rename the left-hand sides of the value bindings.
-- This depends on everything from (B) being in scope.
-- It uses the fixity env from (A) to bind fixities for view patterns.
- new_lhs <- rnTopBindsLHS local_fix_env val_decls ;
+
+ -- We need to throw an error on such value bindings when in a boot file.
+ is_boot <- tcIsHsBootOrSig ;
+ new_lhs <- if is_boot
+ then rnTopBindsLHSBoot local_fix_env val_decls
+ else rnTopBindsLHS local_fix_env val_decls ;
-- Bind the LHSes (and their fixities) in the global rdr environment
let { id_bndrs = collectHsIdBinders CollNoDictBinders new_lhs } ;
@@ -168,7 +173,6 @@ rnSrcDecls group@(HsGroup { hs_valds = val_decls,
-- (F) Rename Value declarations right-hand sides
traceRn "Start rnmono" empty ;
let { val_bndr_set = mkNameSet id_bndrs `unionNameSet` mkNameSet pat_syn_bndrs } ;
- is_boot <- tcIsHsBootOrSig ;
(rn_val_decls, bind_dus) <- if is_boot
-- For an hs-boot, use tc_bndrs (which collects how we're renamed
-- signatures), since val_bndr_set is empty (there are no x = ...
diff --git a/testsuite/tests/rename/should_fail/T19781.hs b/testsuite/tests/rename/should_fail/T19781.hs
new file mode 100644
index 0000000000..1fbf64f24d
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T19781.hs
@@ -0,0 +1,6 @@
+module T19781 where
+
+import {-# SOURCE #-} T19781_A
+
+z :: Int
+z = x + y
diff --git a/testsuite/tests/rename/should_fail/T19781.stderr b/testsuite/tests/rename/should_fail/T19781.stderr
new file mode 100644
index 0000000000..1e468715aa
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T19781.stderr
@@ -0,0 +1,6 @@
+
+T19781_A.hs-boot:4:1: error:
+ Bindings in hs-boot files are not allowed
+
+T19781_A.hs-boot:7:1: error:
+ Bindings in hs-boot files are not allowed
diff --git a/testsuite/tests/rename/should_fail/T19781_A.hs b/testsuite/tests/rename/should_fail/T19781_A.hs
new file mode 100644
index 0000000000..9cde0c39ce
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T19781_A.hs
@@ -0,0 +1,7 @@
+module T19781_A where
+
+x :: Int
+x = 3
+
+y :: Int
+y = 4
diff --git a/testsuite/tests/rename/should_fail/T19781_A.hs-boot b/testsuite/tests/rename/should_fail/T19781_A.hs-boot
new file mode 100644
index 0000000000..9cde0c39ce
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T19781_A.hs-boot
@@ -0,0 +1,7 @@
+module T19781_A where
+
+x :: Int
+x = 3
+
+y :: Int
+y = 4
diff --git a/testsuite/tests/rename/should_fail/all.T b/testsuite/tests/rename/should_fail/all.T
index 475aef9c6c..a5dd61e575 100644
--- a/testsuite/tests/rename/should_fail/all.T
+++ b/testsuite/tests/rename/should_fail/all.T
@@ -164,3 +164,4 @@ test('T18240a', normal, compile_fail, [''])
test('T18240b', normal, compile_fail, [''])
test('T18740a', normal, compile_fail, [''])
test('T18740b', normal, compile_fail, [''])
+test('T19781', [extra_files(['T19781_A.hs', 'T19781_A.hs-boot'])], multimod_compile_fail, ['T19781', '-v0'])