Commit 9dc56b61 authored by Ryan Scott's avatar Ryan Scott

Control validity-checking of type synonym applications more carefully

Trac #16059 shows that when validity checking applications of type
synonyms, GHC sometimes wasn't checking the expanded type enough.
We must be careful, however, since checking both the expanded type as
well as the arguments to the type synonym can lead to exponential
blowup (see https://ghc.haskell.org/trac/ghc/ticket/16059#comment:4).
Nor can we omit checking either the expanded type or the argument for
correctness reasons.

The solution here is to introduce a new `ExpandMode` data type that
is plumbed through all of the type-validity-checking functions in
`TcValidity`. `ExpandMode` dictates whether we only check the
expanded type (`Expand`), only check the arguments (`NoExpand), or
both (`Both`). Importantly, if we check `Both` in the function for
validity checking type synonym applications, then we switch to
`NoExpand` when checking the arguments so as to avoid exponential
blowup. See `Note [Correctness and performance of type synonym validity
checking]` for the full story.
parent e63518f5
This diff is collapsed.
{-# LANGUAGE Safe #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE UnboxedTuples #-}
module Dep05 where
import GHC.Arr
......
Dep05.hs:5:1:
Dep05.hs:6:1: error:
GHC.Arr: Can't be safely imported! The module itself isn't safe.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
module T16059a where
type Foo b = Eq b => b
f :: forall b (a :: Eq b => b). Int
f = 27
g :: forall b (a :: Foo b). Int
g = 42
T16059a.hs:8:6: error:
• Illegal constraint in a kind: Eq b => b
• In the type signature: f :: forall b (a :: Eq b => b). Int
T16059a.hs:11:6: error:
• Illegal constraint in a kind: Eq b => b
• In the expansion of type synonym ‘Foo’
In the type signature: g :: forall b (a :: Foo b). Int
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE UnboxedTuples #-}
module T16059b where
type Foo = forall a. a
type Bar = (# #)
module T16059c where
import T16059b
f :: Foo -> a -> f
f g x = g x
T16059c.hs:5:6: error:
• Illegal polymorphic type: forall a1. a1
Perhaps you intended to use RankNTypes
• In the expansion of type synonym ‘Foo’
In the type signature: f :: Foo -> a -> f
module T16059d where
import T16059b
type Bar' = Bar
T16059d.hs:5:1: error:
• Illegal unboxed tuple type as function argument: (# #)
Perhaps you intended to use UnboxedTuples
• In the expansion of type synonym ‘Bar’
In the type synonym declaration for ‘Bar'’
module T16059e where
import T16059b
type Const a b = a
-- This is accepted, since the rank-n type `Foo` never makes it into the type
-- of `f` post-expansion...
f :: Const Int Foo -> Int
f _ = 42
-- ...but this *is* rejected, since `Foo` does make it into the type of `g`
-- post-expansion.
g :: Const Foo Foo -> Int
g _ = f 27
T16059e.hs:14:6: error:
• Illegal polymorphic type: forall a. a
Perhaps you intended to use RankNTypes
• In the expansion of type synonym ‘Foo’
In the expansion of type synonym ‘Const’
In the type signature: g :: Const Foo Foo -> Int
T9858a.hs:28:18: error:
No instance for (Typeable
((() :: Constraint, () :: Constraint) => ()))
arising from a use of ‘cast’
(maybe you haven't applied a function to enough arguments?)
In the expression: cast e
In the expression: case cast e of { Just e' -> ecast e' }
In an equation for ‘supercast’:
supercast
= case cast e of { Just e' -> ecast e' }
where
e = Refl
e :: E PX PX
T9858a.hs:20:18: error:
• Illegal qualified type:
(() :: Constraint, () :: Constraint) => ()
• In the expansion of type synonym ‘PX’
In the newtype instance declaration for ‘F’
......@@ -501,3 +501,10 @@ test('T15797', normal, compile_fail, [''])
test('T15799', normal, compile_fail, [''])
test('T15801', normal, compile_fail, [''])
test('T15816', normal, compile_fail, [''])
test('T16059a', normal, compile_fail, [''])
test('T16059c', [extra_files(['T16059b.hs'])], multimod_compile_fail,
['T16059c', '-v0'])
test('T16059d', [extra_files(['T16059b.hs'])], multimod_compile_fail,
['T16059d', '-v0'])
test('T16059e', [extra_files(['T16059b.hs'])], multimod_compile_fail,
['T16059e', '-v0'])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment