Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GHC-18157 #509

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions message-index/messages/GHC-18157/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: GHC stage restriction
summary: A local name is used in a top-level splice, quasi-quote or annotation.
severity: error
introduced: 9.8.1
---

A top-level
[Template Haskell splice](https://downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/template_haskell.html#syntax),
[quasi-quote](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/template_haskell.html#template-haskell-quasi-quotation)
or
[source annotation](https://downloads.haskell.org/ghc/latest/docs/users_guide/extending_ghc.html#source-annotations).
cannot directly call a variable bound in the same module.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Bar (bar) where

bar :: Integer
bar = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Example where

import Bar (bar)

{-# ANN foo bar #-}
foo :: Integer
foo = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Example where

{-# ANN foo bar #-}
foo :: Integer
foo = 1

bar :: Integer
bar = 2
17 changes: 17 additions & 0 deletions message-index/messages/GHC-18157/stage-restriction-anns/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: A local name is used in an annotation.
---

`foo` and `bar` are both defined in the same module, causing the error.

# Error Message
```
Example.hs:3:13: error: [GHC-18157]
• GHC stage restriction:
‘bar’ is used in a top-level splice, quasi-quote, or annotation,
and must be imported, not defined locally
• In the annotation: {-# ANN foo bar #-}
|
3 | {-# ANN foo bar #-}
| ^^^
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE QuasiQuotes #-}
module Example where

import QQ (qq)

foo :: String
foo = [qq|1|]
12 changes: 12 additions & 0 deletions message-index/messages/GHC-18157/stage-restriction-qq/after/QQ.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module QQ (qq) where

import Language.Haskell.TH.Quote
import Language.Haskell.TH

qq :: QuasiQuoter
qq = QuasiQuoter
{ quoteExp = pure . LitE . StringL
, quoteDec = undefined
, quotePat = undefined
, quoteType = undefined
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{-# LANGUAGE QuasiQuotes #-}
module Example where

import Language.Haskell.TH.Quote
import Language.Haskell.TH

qq :: QuasiQuoter
qq = QuasiQuoter
{ quoteExp = pure . LitE . StringL
, quoteDec = undefined
, quotePat = undefined
, quoteType = undefined
}

foo :: String
foo = [qq|bar|]
17 changes: 17 additions & 0 deletions message-index/messages/GHC-18157/stage-restriction-qq/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: A local name is used in a quasi-quote.
---

`foo` and `qq` are both defined in the same module, causing the error.

# Error Message
```
Example.hs:16:7: error: [GHC-18157]
• GHC stage restriction:
‘qq’ is used in a top-level splice, quasi-quote, or annotation,
and must be imported, not defined locally
• In the quasi-quotation: [qq|bar|]
|
16 | foo = [qq|bar|]
| ^^^^^^^^^
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE TemplateHaskell #-}
module Example where

import Foo (foo)
import Language.Haskell.TH

x :: Integer
x = $foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Foo (foo) where

import Language.Haskell.TH

foo :: Q Exp
foo = pure . LitE $ IntegerL 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{-# LANGUAGE TemplateHaskell #-}
module Example where

import Language.Haskell.TH

foo :: Q Exp
foo = pure . LitE $ IntegerL 1

x :: Integer
x = $foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: A local name is used in a top-level splice.
---

`x` and `foo` are both defined in the same module, causing the error.

# Error Message
```
Example.hs:10:6: error: [GHC-18157]
• GHC stage restriction:
‘foo’ is used in a top-level splice, quasi-quote, or annotation,
and must be imported, not defined locally
• In the untyped splice: $foo
|
10 | x = $foo
| ^^^
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE TemplateHaskell #-}
module Example where

import TH (doSomethingWithName)

foo :: Int
foo = 1

doSomethingWithName 'foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module TH (doSomethingWithName) where

import Language.Haskell.TH

doSomethingWithName :: Name -> DecsQ
doSomethingWithName x = pure []
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE TemplateHaskell #-}
module Example where

import TH (doSomethingWith)

foo :: Int
foo = 1

doSomethingWith foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module TH (doSomethingWith) where

import Language.Haskell.TH

doSomethingWith :: Int -> DecsQ
doSomethingWith x = pure []
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: A local name is used in a top-level splice as a function argument.
---

The error message given by GHC is slightly misleading: you cannot pass a
_variable_ bound in the same module to a top-level splice, but you can pass in
just the `Name`. The name must be bound above the splice in the module file.

Depending on the information you need from the variable, this can be an
alternative to moving the variable binding to a different module.

The syntax for the `Name` literal of a function `f` is `'f`, and the `Name` of a
type `T` is `''T`. To extract information from a `Name`, see `reify` in
`Language.Haskell.TH`.

# Error Message
```
Example.hs:9:17: error: [GHC-18157]
GHC stage restriction:
‘foo’ is used in a top-level splice, quasi-quote, or annotation,
and must be imported, not defined locally
|
9 | doSomethingWith foo
| ^^^
```
Loading