Skip to content

Missing 'Pattern match has inaccessible right hand side' warnings with -XStrict

Summary

With -XStrict enabled, a non-strict, non-lazy match on a variable of an uninhabited type should produce a warning 'Pattern match has inaccessible RHS', but that's not the case.

The User's Guide says

6.14.3. Strict-by-default pattern bindings

Strict

[...]

  • Function definitions

    When the user writes

    f x = ...

    we interpret it as if they had written

    f !x = ...

[...]

  • Pattern matching in case expressions, lambdas, do-notation, etc

    [...]

    [introducing an implicit bang] does make a difference in the degenerate case of variables and newtypes. So

    case x of y -> rhs

    is lazy in Haskell, but with Strict is interpreted as

    case x of !y -> rhs

    which evaluates x

which makes me think that the same behavior should apply in the pattern match checker if -XStrict is enabled.

Steps to reproduce

  1. Given Void.hs with contents

    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE Strict #-}
    
    module Void where
    
    data Void
    
    idV :: Void -> Void
    idV v = v
    
    idV' :: Void -> Void
    idV' v = case v of w -> w
    
    bangIdV :: Void -> Void
    bangIdV !v = v
    
    bangIdV' :: Void -> Void
    bangIdV' v = case v of !w -> w
  2. Compile with ghc Void.hs

Expected behavior

GHC should compile the program while showing four warnings, one for each function,

[1 of 1] Compiling Void             ( Void.hs, Void.o )

Void.hs:9:1: warning: [-Woverlapping-patterns]
    Pattern match has inaccessible right hand side
    In an equation for ‘idV’: idV v = ...
   |
15 | idV v = v
   | ^^^^^^^^^

Void.hs:12:24: warning: [-Woverlapping-patterns]
    Pattern match has inaccessible right hand side
    In a case alternative: w -> ...
   |
18 | idV' v = case v of w -> w
   |                    ^^^^^^

Void.hs:15:1: warning: [-Woverlapping-patterns]
    Pattern match has inaccessible right hand side
    In an equation for ‘bangIdV’: bangIdV !v = ...
   |
15 | bangIdV !v = v
   | ^^^^^^^^^^^^^^

Void.hs:18:24: warning: [-Woverlapping-patterns]
    Pattern match has inaccessible right hand side
    In a case alternative: !w -> ...
   |
18 | bangIdV' v = case v of !w -> w
   |                        ^^^^^^^

Current behavior

Only the last two warnings (for the definitions using strict patterns) are shown

Environment

  • GHC version used: 9.2.2

Optional:

  • Operating System: macOS
  • System Architecture: x86_64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information