Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#8570 closed bug (fixed)

In a pattern binding:ghc: panic! (the 'impossible' happened): Bogus selector Id

Reported by: rzetterberg Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.6.3
Keywords: Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Compile-time crash Difficulty: Unknown
Test Case: typecheck/should_fail/T8570 Blocked By:
Blocking: Related Tickets:

Description

Hello,

This is my first bug report for GHC. I encountered a type error when compiling my program that said to report the error as a bug here. I tried searching for similar bugs, but could not find any bugs with the same problem.

I'm sorry in advance if I have misunderstood anything or if there indeed are any duplicates of this problem!

The error output

src/app/Templates/Pages.hs:214:17:
    Constructor `Image' does not have field `filepath'
    In the pattern: Image {filepath = fp}
    In a pattern binding: Image {filepath = fp} = logo
    In an equation for `renderLogo':
        renderLogo (Design {mlogo = (Just logo)})
          = H.a ! A.href "/" ! A.class_ "navbar-brand"
            $ H.img ! A.src (toValue fp)
          where
              Image {filepath = fp} = logo

src/app/Templates/Pages.hs:214:34:
    Couldn't match expected type `Field' with actual type `Image'
    In the expression: logo
    In a pattern binding:ghc: panic! (the 'impossible' happened)
  (GHC version 7.6.3 for x86_64-unknown-linux):
        Bogus selector Id

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

When/why does this happen?

I encountered this problem when I accidentally imported a field in a data named "Image" from module A, and imported the constructor of a different data structure from a different module also named "Image".

I guess the type error itself is valid since it doesn't make sense to do what I did, but the way GHC handles this corner case internally is the source of the panic.

Here are my imports:

import Data.Image (Image(filepath))
import Data.Form (Field(..), FieldResult(..))

Data.Image contains the data structure Image, whereas Data.Form contains the data structure Field which has a constructor named Image.

Here is the the data structure in Data.Image:

data Image = Image
    { id :: Integer
    , categoryId :: Integer
    , name :: String
    , filepath :: String
    , createdAt :: LocalTimestamp
    } deriving (Show)

And here is the data structure in Data.Form:

data Field
    = Password   Name Label
    | Text       Name Label Value
    | Email      Name Label Value
    | InlineArea Name Label Html
    | Hidden     Name Value
    | Image      Name Label
    | Select     Name Label [SelectItem] SelectedId
    | CheckBox   Name Label Selected

And here is the line I use the filepath field in my template file:

    where Image{filepath = fp} = logo

Can this GHC panic be avoided?

Yes, by specifically importing from the same module. Like so:

import Data.Image (Image(Image, filepath)
import Data.Form (Field(InlineArea), FieldResult(..))

If there is anything more I can do to help, just let me know!

Thank you
Richard Zetterberg

Change History (9)

comment:1 Changed 5 months ago by simonpj

I can't reproduce this. Here are the modules I used. I used these modules:

module T8570a where
  data Image = Image
    { id :: Integer
    , categoryId :: Integer
    , name :: String
    , filepath :: String
    } deriving (Show)

module T8570b where
  data Field
    = Password   String
    | Image      Int

module T8570 where
  import T8570a (Image(filepath))
  import T8570b (Field(..))

  foo logo = fp where Image{filepath = fp} = logo

I compiled thus:

bash$ ghc -c T8570a.hs
bash$ ghc -c T8570b.hs
bash$ ghc -c T8570.hs

T8570.hs:6:27:
    Constructor `Image' does not have field `filepath'
    In the pattern: Image {filepath = fp}
    In a pattern binding: Image {filepath = fp} = logo
    In an equation for `foo':
        foo logo
          = fp
          where
              Image {filepath = fp} = logo

I tried with ghc-7.6.3 and HEAD. They all work fine.

Can you offer a precise, reproducible test case? Thanks

Simon

comment:2 Changed 5 months ago by parcs

I can reproduce the error if logo is defined and imported from the module that defines data Image.

comment:3 Changed 5 months ago by simonpj

Can you give me the code?

comment:4 Changed 5 months ago by parcs

This code reproduces the error:

module A where

import B (Image(filepath), logo)
import C (Field(Image))

foo = let Image {filepath = ()} = logo in ()
module B where

data Image = Image { filepath :: () }

logo = Image ()
module C where

data Field = Image
$ ghc-stage2 A
[1 of 3] Compiling C                ( C.hs, C.o )
[2 of 3] Compiling B                ( B.hs, B.o )
[3 of 3] Compiling A                ( A.hs, A.o )

A.hs:6:18:
    Constructor ‛Image’ does not have field ‛filepath’
    In the pattern: Image {filepath = ()}
    In a pattern binding: Image {filepath = ()} = logo
    In the expression: let Image {filepath = ()} = logo in ()

A.hs:6:35:
    Couldn't match expected type ‛Field’ with actual type ‛Image’
    In the expression: logo
    In a pattern binding:ghc-stage2: panic! (the 'impossible' happened)
  (GHC version 7.7.20131127 for x86_64-unknown-linux):
        Bogus selector Id

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

One-shot mode behaves similarly.

comment:5 Changed 5 months ago by Simon Peyton Jones <simonpj@…>

In 59e17d6ef71c29f4e114e55be83f1b3722f04139/ghc:

Fail (rather than addErr) if you use a bogus field in a pattern

This fixes Trac #8570

comment:6 Changed 5 months ago by Simon Peyton Jones <simonpj@…>

In 5f3c989258205158c523fbe218d52394f6a28cb8/testsuite:

Wibbles when fixing Trac #8570

comment:7 Changed 5 months ago by Simon Peyton Jones <simonpj@…>

comment:8 Changed 5 months ago by simonpj

  • Resolution set to fixed
  • Status changed from new to closed

Thank you!

comment:9 Changed 5 months ago by simonpj

  • Test Case set to typecheck/should_fail/T8570
Note: See TracTickets for help on using tickets.