Opened 3 years ago

Closed 3 years ago

#9175 closed bug (fixed)

Bad interaction between Pattern Synonyms and Text

Reported by: emertens Owned by:
Priority: normal Milestone: 7.8.3
Component: Compiler Version: 7.8.2
Keywords: Cc: diatchki@…, bos@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Compile-time crash Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Compiling this file causes the impossible to happen. It is necessary to enable optimizations. The bug did not occur if the Text parameter was replaced with ByteString or String.

{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE OverloadedStrings #-}
module Bug where

 -  Must compile with optimizations
 -  text-
 -  ghc-7.8.2
 -  Both Darwin and Linux platforms

import Data.Text(Text)

data T = C Text Bool

f :: a -> b
f _ = undefined
{-# NOINLINE f #-} -- important

pattern P1 a = C "sh" a -- at least two characters
pattern P2    = C "x" True -- this pattern has to come last

g :: Text -> T
g x = case x of
       "" -> f (P1 undefined) -- this has to be a pattern synonym
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )
ghc: panic! (the 'impossible' happened)
  (GHC version 7.8.2 for x86_64-apple-darwin):
	StgCmmEnv: variable not found
    lvl_s2Jr{v} [lid]
    local binds for:
    main:Bug.f{v rBS} [gid]
    main:Bug.rewriteRule{v rBV} [gid]
    main:Bug.$mP2{v r17q} [gid]
    main:Bug.$WP2{v r17A} [gid]
    main:Bug.$mP1{v r17E} [gid]
    main:Bug.$WP1{v r17R} [gid]
    main:Bug.$WP3{v r3bq} [gid]
    main:Bug.$WP4{v r3ua} [gid]
    main:Bug.$WP5{v r3ub} [gid]
    main:Bug.$WP2_dt{v r3uc} [gid]
    main:Bug.$WP6{v r3ud} [gid]
    main:Bug.$WP7{v r3ue} [gid]
    main:Bug.$WP8{v r3uf} [gid]
    main:Bug.$w$mP1{v r3ug} [gid]
    main:Bug.$w$mP2{v r3uh} [gid]
    main:Bug.$mP3{v r3ui} [gid]
    main:Bug.$mP4{v r3uj} [gid]
    main:Bug.$mP5{v r3uk} [gid]
    main:Bug.$wrewriteRule{v r3ul} [gid]
    main:Bug.rewriteRule1{v r3um} [gid]
    main:Bug.rewriteRule2{v r3un} [gid]
    main:Bug.rewriteRule3{v r3uo} [gid]
    main:Bug.rewriteRule4{v r3up} [gid]
    main:Bug.rewriteRule5{v r3uq} [gid]
    lvl{v r3ur} [gid]

Please report this as a GHC bug:

Change History (4)

comment:1 Changed 3 years ago by Simon Peyton Jones <simonpj@…>

In 7ac600d5fcd74db1f991555de6e415030970d5f3/ghc:

Make the matcher and wrapper Ids in PatSyn into LocalIds, not GlobalIds

This was a serious bug, exposed by Trac #9175.  The matcher and wrapper
must be LocalIds, like record selectors and dictionary functions, for
the reasons now documented in Note [Exported LocalIds] in Id.lhs

In fixing this I found
 - PatSyn should have an Id inside it (apart from the wrapper and matcher)
   It should be a Name.  Hence psId --> psName, with knock-on consequences

 - Tidying of PatSyns in TidyPgm was wrong

 - The keep-alive set in Desugar.deSugar (now) doesn't need pattern synonyms
   in it

I also cleaned up the interface to PatSyn a little, so there's a tiny knock-on
effect in Haddock; hence the haddock submodule update.

It's very hard to make a test for this bug, so I haven't.

comment:2 Changed 3 years ago by simonpj

Status: newmerge

Thank you for reporting this bug. It exposed a nasty bug in the pattern-synonym implementation, which I have now fixed. Nasty in that it's not easy to provoke... but I'm sure it would have bitten us soon enough. Indeed I'm not even going to add a test case.

Please merge to 7.8.3 though (along with the corresponding Haddock patch.



comment:3 Changed 3 years ago by thoughtpolice

Milestone: 7.8.3

comment:4 Changed 3 years ago by thoughtpolice

Resolution: fixed
Status: mergeclosed


Note: See TracTickets for help on using tickets.