Skip to content

Commit cda514b

Browse files
committed
Add GHC-18157
1 parent 2c280d5 commit cda514b

File tree

18 files changed

+197
-0
lines changed

18 files changed

+197
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: GHC stage restriction
3+
summary: A local name is used in a top-level splice, quasi-quote or annotation.
4+
severity: error
5+
introduced: 9.8.1
6+
---
7+
8+
A top-level
9+
[Template Haskell splice](https://downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/template_haskell.html#syntax),
10+
[quasi-quote](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/template_haskell.html#template-haskell-quasi-quotation)
11+
or
12+
[source annotation](https://downloads.haskell.org/ghc/latest/docs/users_guide/extending_ghc.html#source-annotations).
13+
cannot directly call a variable bound in the same module.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Bar (bar) where
2+
3+
bar :: Integer
4+
bar = 2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Example where
2+
3+
import Bar (bar)
4+
5+
{-# ANN foo bar #-}
6+
foo :: Integer
7+
foo = 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Example where
2+
3+
{-# ANN foo bar #-}
4+
foo :: Integer
5+
foo = 1
6+
7+
bar :: Integer
8+
bar = 2
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: A local name is used in an annotation.
3+
---
4+
5+
`foo` and `bar` are both defined in the same module, causing the error.
6+
7+
# Error Message
8+
```
9+
Example.hs:3:13: error: [GHC-18157]
10+
• GHC stage restriction:
11+
‘bar’ is used in a top-level splice, quasi-quote, or annotation,
12+
and must be imported, not defined locally
13+
• In the annotation: {-# ANN foo bar #-}
14+
|
15+
3 | {-# ANN foo bar #-}
16+
| ^^^
17+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE QuasiQuotes #-}
2+
module Example where
3+
4+
import QQ (qq)
5+
6+
foo :: String
7+
foo = [qq|1|]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module QQ (qq) where
2+
3+
import Language.Haskell.TH.Quote
4+
import Language.Haskell.TH
5+
6+
qq :: QuasiQuoter
7+
qq = QuasiQuoter
8+
{ quoteExp = pure . LitE . StringL
9+
, quoteDec = undefined
10+
, quotePat = undefined
11+
, quoteType = undefined
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-# LANGUAGE QuasiQuotes #-}
2+
module Example where
3+
4+
import Language.Haskell.TH.Quote
5+
import Language.Haskell.TH
6+
7+
qq :: QuasiQuoter
8+
qq = QuasiQuoter
9+
{ quoteExp = pure . LitE . StringL
10+
, quoteDec = undefined
11+
, quotePat = undefined
12+
, quoteType = undefined
13+
}
14+
15+
foo :: String
16+
foo = [qq|bar|]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: A local name is used in a quasi-quote.
3+
---
4+
5+
`foo` and `qq` are both defined in the same module, causing the error.
6+
7+
# Error Message
8+
```
9+
Example.hs:16:7: error: [GHC-18157]
10+
• GHC stage restriction:
11+
‘qq’ is used in a top-level splice, quasi-quote, or annotation,
12+
and must be imported, not defined locally
13+
• In the quasi-quotation: [qq|bar|]
14+
|
15+
16 | foo = [qq|bar|]
16+
| ^^^^^^^^^
17+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module Example where
3+
4+
import Foo (foo)
5+
import Language.Haskell.TH
6+
7+
x :: Integer
8+
x = $foo
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Foo (foo) where
2+
3+
import Language.Haskell.TH
4+
5+
foo :: Q Exp
6+
foo = pure . LitE $ IntegerL 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module Example where
3+
4+
import Language.Haskell.TH
5+
6+
foo :: Q Exp
7+
foo = pure . LitE $ IntegerL 1
8+
9+
x :: Integer
10+
x = $foo
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: A local name is used in a top-level splice.
3+
---
4+
5+
`x` and `foo` are both defined in the same module, causing the error.
6+
7+
# Error Message
8+
```
9+
Example.hs:10:6: error: [GHC-18157]
10+
• GHC stage restriction:
11+
‘foo’ is used in a top-level splice, quasi-quote, or annotation,
12+
and must be imported, not defined locally
13+
• In the untyped splice: $foo
14+
|
15+
10 | x = $foo
16+
| ^^^
17+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module Example where
3+
4+
import TH (doSomethingWithName)
5+
6+
foo :: Int
7+
foo = 1
8+
9+
doSomethingWithName 'foo
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module TH (doSomethingWithName) where
2+
3+
import Language.Haskell.TH
4+
5+
doSomethingWithName :: Name -> DecsQ
6+
doSomethingWithName x = pure []
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module Example where
3+
4+
import TH (doSomethingWith)
5+
6+
foo :: Int
7+
foo = 1
8+
9+
doSomethingWith foo
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module TH (doSomethingWith) where
2+
3+
import Language.Haskell.TH
4+
5+
doSomethingWith :: Int -> DecsQ
6+
doSomethingWith x = pure []
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: A local name is used in a top-level splice as a function argument.
3+
---
4+
5+
The error message given by GHC is slightly misleading: you cannot pass a
6+
_variable_ bound in the same module to a top-level splice, but you can pass in
7+
just the `Name`. The name must be bound above the splice in the module file.
8+
9+
Depending on the information you need from the variable, this can be an
10+
alternative to moving the variable binding to a different module.
11+
12+
The syntax for the `Name` literal of a function `f` is `'f`, and the `Name` of a
13+
type `T` is `''T`. To extract information from a `Name`, see `reify` in
14+
`Language.Haskell.TH`.
15+
16+
# Error Message
17+
```
18+
Example.hs:9:17: error: [GHC-18157]
19+
GHC stage restriction:
20+
‘foo’ is used in a top-level splice, quasi-quote, or annotation,
21+
and must be imported, not defined locally
22+
|
23+
9 | doSomethingWith foo
24+
| ^^^
25+
```

0 commit comments

Comments
 (0)