Ticket #372 (closed bug: Fixed)

Opened 8 years ago

Last modified 43 years ago

Template panics the compiler

Reported by: nobody Owned by: simonpj
Priority: normal Milestone:
Component: Template Haskell Version: 6.4
Keywords: Cc:
Operating System: Architecture:
Type of failure: Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Experimenting a little with the template example from 
http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html
i tried to put a small parser into it, and ran into this:

ghc --make -fth template.hs -o template
Chasing modules from: template.hs
Skipping  Printf           ( ./Printf.hs, ./Printf.o )
Compiling Main             ( template.hs, template.o )
Loading package base-1.0 ... linking ... done.
Loading package haskell98-1.0 ... linking ... done.
Loading package parsec-1.0 ... linking ... done.
Loading package template-haskell-1.0 ... linking ... done.
ghc: panic! (the `impossible' happened, GHC version 6.4):
	./Printf.hs:(40,0)-(42,20): Non-exhaustive patterns in
function gen


Please report it as a compiler bug to
glasgow-haskell-bugs@haskell.org,
or http://sourceforge.net/projects/ghc/.


Compilation exited abnormally with code 1 at Wed May 04
00:50:37

I was running on winxp

The code was:

--template.hs:
module Main where
import Printf (pr)
    
main = putStrLn ( $(pr "hello"))
       
--Printf.hs:

module Printf where

-- Skeletal printf from the paper.
-- It needs to be in a separate module to the one where
-- you intend to use it.

-- Import some Template Haskell syntax
import Language.Haskell.TH
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Char
import Text.ParserCombinators.Parsec.Prim
import Text.ParserCombinators.Parsec.Combinator
    

-- Describe a format string
data Format = D | S | L String

parsePercent = do char '%'
                  k <- oneOf "sd"
                  return (case k of
                          's' -> S
                          'd' -> D)
parseString = do s <- many (satisfy (/= '%'))
                 return (L s)

-- Is not used yet since it doesn't work              
         
-- parseFormat' = return many (parsePercent <|>
parseString)
                
-- Parse a format string.  This is left largely to you
-- as we are here interested in building our first ever
-- Template Haskell program and not in building printf.
parseFormat :: String -> [Format]
parseFormat s   = case (parse (many parsePercent) [] s) of
                        Left err -> fail "Printf parser
error"
                        Right x -> x

-- Generate Haskell source code from a parsed
representation
-- of the format string.  This code will be spliced into
-- the module which calls "pr", at compile time.
gen :: [Format] -> ExpQ
gen [D]   = [| \n -> show n |]
gen [S]   = [| \s -> s |]
gen [L s] = stringE s

-- Here we generate the Haskell code for the splice
-- from an input format string.
pr :: String -> ExpQ
pr s      = gen (parseFormat s)


Change History

Changed 8 years ago by wthaller

Logged In: YES 
user_id=566359

This is actually a failure in your Printf module - function gen was called 
with something that is not a one-element list. 
There is a GHC bug here, though: GHC shouldn't report a pattern-match 
failure in user-defined code as a compiler bug.

Changed 8 years ago by simonpj

  • status changed from assigned to closed
Logged In: YES 
user_id=50165

Wolfgang is right: the bug is in your code, but GHC's error 
report was bad.

It's now much improved.  The fix will be in 6.4.1

Note: See TracTickets for help on using tickets.