[GHC] #16249: no runtime error for -fdefer-type-errors with TypeError constraint

#16249: no runtime error for -fdefer-type-errors with TypeError constraint -------------------------------------+------------------------------------- Reporter: guibou | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.3 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Part of a testsuite, I'm using `-fdefer-type-errors` to check if haskell expression raises type error. However the haskell expression may be perfectly valid except for a `TypeError` constraint on a typeclass. The following code: {{{#!haskell {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE DataKinds #-} import GHC.TypeLits class Foo t where foo :: t -> t foo = id instance Foo Int instance (TypeError (Text "String does not work")) => Foo String main :: IO () main = do putStrLn (show (foo 10 :: Int)) putStrLn (foo "hello") }}} Correctly generates errors when compiled: {{{ $ ghc ./DeferTypes.hs [1 of 1] Compiling Main ( DeferTypes.hs, DeferTypes.o ) [flags changed] DeferTypes.hs:17:13: error: • String does not work • In the first argument of ‘putStrLn’, namely ‘(foo "hello")’ In a stmt of a 'do' block: putStrLn (foo "hello") In the expression: do putStrLn (show (foo 10 :: Int)) putStrLn (foo "hello") | 17 | putStrLn (foo "hello") | }}} And also with `-fdefer-type-erros`, the error is transformed into a warning: {{{ $ ghc -fdefer-type-errors ./DeferTypes.hs [1 of 1] Compiling Main ( DeferTypes.hs, DeferTypes.o ) [flags changed] DeferTypes.hs:17:13: warning: [-Wdeferred-type-errors] • String does not work • In the first argument of ‘putStrLn’, namely ‘(foo "hello")’ In a stmt of a 'do' block: putStrLn (foo "hello") In the expression: do putStrLn (show (foo 10 :: Int)) putStrLn (foo "hello") | 17 | putStrLn (foo "hello") | ^^^^^^^^^^^ Linking DeferTypes ... }}} However, executing the program gives no runtime error: {{{ $ ./DeferTypes 10 hello }}} I was expecting something such as: {{{ $ ./DeferTypes 10 [a defered type error exception] }}} With ghc 8.6.3 from nix. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/16249 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#16249: no runtime error for -fdefer-type-errors with TypeError constraint -------------------------------------+------------------------------------- Reporter: guibou | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by simonpj): Hmm. The trouble here is that, at runtime the `TypeError` dictionary is not actually used. I think we get something like this {{{ data Foo t = MkFoo (t -> t) foo :: Foo t -> t -> t foo (MkFoo f) = f $dfFooString :: TypeError (Text "String does not work") -> Foo String -- Arises from the instance $dfFooString d = MkFoo id derr:: TypeError (Text "String does not work") derr = error "String does not work" x :: String -- Haskell source code: -- x = foo "hello" x = foo ($dfFooString derr) "hello" }}} Evaluating `f` does not evaluate `derr`, hence the no-error run. I suppose someone could think about evaluating calls to `TypeError` more eagerly. But I'd be inclined to think that an easier path is to say {{{ instance (TypeError (Text "String does not work")) => Foo String where foo = error "String does not work" }}} I'm sure there is more than could be done to make `TypeError` better. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/16249#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC