Opened 14 months ago

Last modified 8 months ago

#15437 new bug

Internal error when applying a scoped type variable inside a typed expression quotation

Reported by: dminuoso Owned by:
Priority: normal Milestone: 8.10.1
Component: Template Haskell Version: 8.4.3
Keywords: TypedTemplateHaskell Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: T15437
Blocked By: Blocking:
Related Tickets: #13587, #15835 Differential Rev(s):
Wiki Page:

Description

{-# LANGUAGE TemplateHaskell #-}

import TestMod

f :: Int
f = $$(foo)

main :: IO ()
main = main
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}

module TestMod where

import Language.Haskell.TH.Syntax (Q, TExp)

get :: forall a. Int
get = 1

foo :: forall a. Q (TExp Int)
foo = [|| get @a ||]
Test.hs:6:8: error:
    • The exact Name ‘a’ 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 result of the splice:
        $foo
      To see what the splice expanded to, use -ddump-splices
      In the Template Haskell splice $$(foo)
      In the expression: $$(foo)
  |
6 | f = $$(foo)
  |        ^^^

Test.hs:6:8: error:
    • GHC internal error: ‘a’ is not in scope during type checking, but it passed the renamer
      tcl_env of environment: [r3Kl :-> Identifier[f::Int, TopLevelLet [] True],
                               r3PI :-> Identifier[main::IO (), TopLevelLet [r3PI :-> main] True]]
    • In the type ‘a’
      In the expression: get @a
      In the result of the splice:
        $foo
      To see what the splice expanded to, use -ddump-splices
  |
6 | f = $$(foo)
  |

Change History (12)

comment:1 Changed 14 months ago by simonpj

Component: CompilerTemplate Haskell

comment:2 Changed 14 months ago by RyanGlScott

comment:3 Changed 14 months ago by bgamari

Milestone: 8.6.18.8.1

These won't be fixed for in GHC 8.6.

comment:4 Changed 11 months ago by mpickering

Also #15835

comment:5 Changed 11 months ago by mpickering

I don't think types are really dealt with properly in typed template haskell. Your program is a lot like

foo :: Int -> Q (TExp Int)
foo x = [|| x ||]

In this case, we have to interpret the x as $(lift x) and we should do the same thing for types here.

To go back to your program and some pseudo syntax:

foo :: forall a. Q (TExp Int)
foo = [|| get @$(lift a) ||]

We really mean something like this as the value of a is bound at compile-time but not at run-time so if we want to use it we have to use it in a splice.

Does that sound sensible Simon?

comment:6 Changed 11 months ago by mpickering

I put a patch up which contains the test for now.

https://phabricator.haskell.org/D5291

comment:7 Changed 10 months ago by Ben Gamari <ben@…>

In ea015172/ghc:

Add test for #15437

Reviewers: bgamari

Subscribers: rwbarton, carter

GHC Trac Issues: #15437

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

comment:8 Changed 10 months ago by bgamari

Test Case: T15437

comment:9 Changed 9 months ago by osa1

Milestone: 8.8.18.10.1

Bumping milestones of low-priority tickets.

comment:10 Changed 8 months ago by mpickering

Keywords: TypedTemplateHaskell added

comment:11 Changed 8 months ago by mpickering

I have a patch which fixes the issue presented in this ticket but there are some rough edges and design considerations which need to be dealt with.

https://gitlab.haskell.org/ghc/ghc/merge_requests/166

comment:12 Changed 8 months ago by mpickering

Patch is now ready for preliminary review:

https://gitlab.haskell.org/ghc/ghc/merge_requests/166

Note: See TracTickets for help on using tickets.