Commit 144ddb41 authored by niteria's avatar niteria

Construct in_scope set in mkTopTCvSubst

The pre-condition on `mkTopTCvSubst` turned out to be wrong and
not satisfied by any of the callers. I've fixed it, so that it
constructs the in_scope set from the range of the substitution.
`mkTopTCvSubst` was also unnecessarily general it is never called
with `CoVars`, so I changed the type signature and added an assertion.

Test Plan: ./validate --slow

Reviewers: goldfire, simonpj, bgamari, austin

Reviewed By: simonpj

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D1801

GHC Trac Issues: #11371
parent 6817703b
......@@ -184,7 +184,7 @@ buildPatSyn src_name declared_infix matcher@(matcher_id,_) builder
-- compatible with the pattern synonym
ASSERT2((and [ univ_tvs `equalLength` univ_tvs1
, ex_tvs `equalLength` ex_tvs1
, pat_ty `eqType` substTyUnchecked subst pat_ty1
, pat_ty `eqType` substTy subst pat_ty1
, prov_theta `eqTypes` substTys subst prov_theta1
, req_theta `eqTypes` substTys subst req_theta1
, arg_tys `eqTypes` substTys subst arg_tys1
......
......@@ -543,13 +543,13 @@ bindLocalsAtBreakpoint hsc_env apStack_fhv (Just BreakInfo{..}) = do
let tv_subst = newTyVars us free_tvs
filtered_ids = [ id | (id, Just _hv) <- zip ids mb_hValues ]
(_,tidy_tys) = tidyOpenTypes emptyTidyEnv $
map (substTyUnchecked tv_subst . idType) filtered_ids
map (substTy tv_subst . idType) filtered_ids
new_ids <- zipWith3M mkNewId occs tidy_tys filtered_ids
result_name <- newInteractiveBinder hsc_env (mkVarOccFS result_fs) span
let result_id = Id.mkVanillaGlobal result_name
(substTyUnchecked tv_subst result_ty)
(substTy tv_subst result_ty)
result_ok = isPointer result_id
final_ids | result_ok = result_id : new_ids
......
......@@ -956,7 +956,7 @@ flatten_one (TyConApp tc tys)
-- Expand type synonyms that mention type families
-- on the RHS; see Note [Flattening synonyms]
| Just (tenv, rhs, tys') <- expandSynTyCon_maybe tc tys
, let expanded_ty = mkAppTys (substTyUnchecked (mkTopTCvSubst tenv) rhs) tys'
, let expanded_ty = mkAppTys (substTy (mkTopTCvSubst tenv) rhs) tys'
= do { mode <- getMode
; let used_tcs = tyConsOfType rhs
; case mode of
......
......@@ -307,7 +307,7 @@ tcSuperSkolTyVars :: [TyVar] -> (TCvSubst, [TcTyVar])
-- Moreover, make them "super skolems"; see comments with superSkolemTv
-- see Note [Kind substitution when instantiating]
-- Precondition: tyvars should be ordered by scoping
tcSuperSkolTyVars = mapAccumL tcSuperSkolTyVar (mkTopTCvSubst [])
tcSuperSkolTyVars = mapAccumL tcSuperSkolTyVar emptyTCvSubst
tcSuperSkolTyVar :: TCvSubst -> TyVar -> (TCvSubst, TcTyVar)
tcSuperSkolTyVar subst tv
......
......@@ -99,7 +99,7 @@ import GHC.Serialized
import ErrUtils
import Util
import Unique
import VarSet ( isEmptyVarSet, filterVarSet )
import VarSet ( isEmptyVarSet, filterVarSet, mkVarSet, elemVarSet )
import Data.List ( find )
import Data.Maybe
import FastString
......@@ -1395,8 +1395,8 @@ reifyDataCon isGadtDataCon tys dc
name = reifyName dc
-- Universal tvs present in eq_spec need to be filtered out, as
-- they will not appear anywhere in the type.
subst = mkTopTCvSubst (map eqSpecPair g_eq_spec)
g_unsbst_univ_tvs = filter (`notElemTCvSubst` subst) g_univ_tvs
eq_spec_tvs = mkVarSet (map eqSpecTyVar g_eq_spec)
g_unsbst_univ_tvs = filterOut (`elemVarSet` eq_spec_tvs) g_univ_tvs
; r_arg_tys <- reifyTypes (if isGadtDataCon then g_arg_tys else arg_tys)
......
......@@ -1237,7 +1237,7 @@ normalise_tc_app tc tys
; case expandSynTyCon_maybe tc ntys of
{ Just (tenv, rhs, ntys') ->
do { (co2, ninst_rhs)
<- normalise_type (substTyUnchecked (mkTopTCvSubst tenv) rhs)
<- normalise_type (substTy (mkTopTCvSubst tenv) rhs)
; return $
if isReflCo co2
then (args_co, mkTyConApp tc ntys)
......
......@@ -1686,12 +1686,15 @@ zipOpenTCvSubstBinders bndrs tys
is = mkInScopeSet (tyCoVarsOfTypes tys)
tenv = mkVarEnv [ (tv, ty) | (Named tv _, ty) <- zip bndrs tys ]
-- | Called when doing top-level substitutions. Here we expect that the
-- free vars of the range of the substitution will be empty.
mkTopTCvSubst :: [(TyCoVar, Type)] -> TCvSubst
mkTopTCvSubst prs = TCvSubst emptyInScopeSet tenv cenv
where (tenv, cenv) = foldl extend (emptyTvSubstEnv, emptyCvSubstEnv) prs
extend envs (v, ty) = extendSubstEnvs envs v ty
-- | Called when doing top-level substitutions. No CoVars, please!
mkTopTCvSubst :: [(TyVar, Type)] -> TCvSubst
mkTopTCvSubst prs =
ASSERT2( onlyTyVarsAndNoCoercionTy, text "prs" <+> ppr prs )
mkOpenTCvSubst tenv emptyCvSubstEnv
where tenv = mkVarEnv prs
onlyTyVarsAndNoCoercionTy =
and [ isTyVar tv && not (isCoercionTy ty)
| (tv, ty) <- prs ]
zipTyEnv :: [TyVar] -> [Type] -> TvSubstEnv
zipTyEnv tyvars tys
......
......@@ -294,7 +294,11 @@ coreView :: Type -> Maybe Type
-- By being non-recursive and inlined, this case analysis gets efficiently
-- joined onto the case analysis that the caller is already doing
coreView (TyConApp tc tys) | Just (tenv, rhs, tys') <- expandSynTyCon_maybe tc tys
= Just (mkAppTys (substTyUnchecked (mkTopTCvSubst tenv) rhs) tys')
= Just (mkAppTys (substTy (mkTopTCvSubst tenv) rhs) tys')
-- The free vars of 'rhs' should all be bound by 'tenv', so it's
-- ok to use 'substTy' here.
-- See also Note [Generating the in-scope set for a substitution]
-- in TyCoRep.
-- Its important to use mkAppTys, rather than (foldl AppTy),
-- because the function part might well return a
-- partially-applied type constructor; indeed, usually will!
......
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