Opened 9 months ago

Closed 9 months ago

#16377 closed bug (duplicate)

`TypeError` in a pattern should flag inaccessible code

Reported by: diatchki Owned by:
Priority: normal Milestone:
Component: Compiler (Type checker) Version: 8.6.3
Keywords: PatternMatchWarnings, CustomTypeErrors Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #11503 Differential Rev(s):
Wiki Page:

Description

When we pattern match, if the context ends up having a custom TypeError, we should probably report it, as we've essentially found some inaccessible code. At present this doesn't happen and the custom error is only triggered if the function is applied to an invalid argument. Here is an example to illustrate the issue:

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GADTs #-}

module Test where

import GHC.TypeLits
import Data.Kind(Constraint)

type family F a :: Constraint
type instance F Int  = ()
type instance F Char = TypeError ('Text "Nope")
type instance F Bool = (Int ~ Char)

data T where
  A :: F Int  => T
  B :: F Char => T
  C :: F Bool => T

exhaustive :: T -> ()
exhaustive A = ()
exhaustive B = ()   -- this is not flagged as inaccessible, but it should
-- exhaustive C = ()   -- this is flagged as inaccessible, as expected

If the match with constructor C is added to the program, GHC detects that this equation is inaccessible because Int will never match Char. I think that we'd like the same to happen in the second equation when we match on B, except we should report the custom type error, instead of GHC's message about unreachable code.

Change History (2)

comment:1 Changed 9 months ago by RyanGlScott

Keywords: PatternMatchWarnings CustomTypeErrors added

Is this the same issue as in #11503?

comment:2 Changed 9 months ago by diatchki

Resolution: duplicate
Status: newclosed

Ah, yes. I am marking it as a duplicate of #11503.

Note: See TracTickets for help on using tickets.