diff options
author | Simon Peyton Jones <simonpj@microsoft.com> | 2016-06-23 08:47:31 +0100 |
---|---|---|
committer | Simon Peyton Jones <simonpj@microsoft.com> | 2016-06-23 09:22:53 +0100 |
commit | 3e0af469c97d34bea92032d54d155afc65bd4b20 (patch) | |
tree | 63548506546b50bf1af78d82ba92340d77d42510 /compiler | |
parent | 210a2e122ce3b7c56c780e4541b9f222abe7d2f7 (diff) | |
download | haskell-3e0af469c97d34bea92032d54d155afc65bd4b20.tar.gz |
Give lookupGRE_Name a better API
lookupGRE_Name should return either zero or one GREs, never
several. This is a consequence of INVARIANT 1 on GlobalRdrEnv.
So it's better if it returns a Maybe; the panic on multiple results
is put in one place, instead of being scattered or ignored.
Just refactoring, no change in behaviour
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/basicTypes/RdrName.hs | 16 | ||||
-rw-r--r-- | compiler/main/InteractiveEval.hs | 3 | ||||
-rw-r--r-- | compiler/rename/RnEnv.hs | 2 | ||||
-rw-r--r-- | compiler/rename/RnNames.hs | 4 | ||||
-rw-r--r-- | compiler/rename/RnPat.hs | 7 | ||||
-rw-r--r-- | compiler/rename/RnSource.hs | 10 | ||||
-rw-r--r-- | compiler/typecheck/FamInst.hs | 2 | ||||
-rw-r--r-- | compiler/typecheck/TcDeriv.hs | 2 | ||||
-rw-r--r-- | compiler/typecheck/TcErrors.hs | 2 | ||||
-rw-r--r-- | compiler/typecheck/TcForeign.hs | 2 |
10 files changed, 28 insertions, 22 deletions
diff --git a/compiler/basicTypes/RdrName.hs b/compiler/basicTypes/RdrName.hs index 0856597805..592ee92034 100644 --- a/compiler/basicTypes/RdrName.hs +++ b/compiler/basicTypes/RdrName.hs @@ -734,10 +734,14 @@ lookupGRE_RdrName rdr_name env Nothing -> [] Just gres -> pickGREs rdr_name gres -lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt] +lookupGRE_Name :: GlobalRdrEnv -> Name -> Maybe GlobalRdrElt lookupGRE_Name env name - = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name), - gre_name gre == name ] + = case [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name) + , gre_name gre == name ] of + [] -> Nothing + [gre] -> Just gre + gres -> pprPanic "lookupGRE_Name" (ppr name $$ ppr gres) + -- See INVARIANT 1 on GlobalRdrEnv lookupGRE_Field_Name :: GlobalRdrEnv -> Name -> FastString -> [GlobalRdrElt] -- Used when looking up record fields, where the selector name and @@ -751,8 +755,10 @@ getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]] -- Returns all the qualifiers by which 'x' is in scope -- Nothing means "the unqualified version is in scope" -- [] means the thing is not in scope at all -getGRE_NameQualifier_maybes env - = map (qualifier_maybe) . lookupGRE_Name env +getGRE_NameQualifier_maybes env name + = case lookupGRE_Name env name of + Just gre -> [qualifier_maybe gre] + Nothing -> [] where qualifier_maybe (GRE { gre_lcl = lcl, gre_imp = iss }) | lcl = Nothing diff --git a/compiler/main/InteractiveEval.hs b/compiler/main/InteractiveEval.hs index 5d0d7e75f8..6c95dc3bcc 100644 --- a/compiler/main/InteractiveEval.hs +++ b/compiler/main/InteractiveEval.hs @@ -793,8 +793,7 @@ getInfo allInfo name -- The one we looked for in the first place! | pretendNameIsInScope n = True | isBuiltInSyntax n = True - | isExternalName n = any ((== n) . gre_name) - (lookupGRE_Name rdr_env n) + | isExternalName n = isJust (lookupGRE_Name rdr_env n) | otherwise = True -- | Returns all names in scope in the current interactive context diff --git a/compiler/rename/RnEnv.hs b/compiler/rename/RnEnv.hs index 4ab67ad56c..b0a728176c 100644 --- a/compiler/rename/RnEnv.hs +++ b/compiler/rename/RnEnv.hs @@ -1018,7 +1018,7 @@ addUsedDataCons :: GlobalRdrEnv -> TyCon -> RnM () addUsedDataCons rdr_env tycon = addUsedGREs [ gre | dc <- tyConDataCons tycon - , gre : _ <- [lookupGRE_Name rdr_env (dataConName dc) ] ] + , Just gre <- [lookupGRE_Name rdr_env (dataConName dc)] ] addUsedGRE :: Bool -> GlobalRdrElt -> RnM () -- Called for both local and imported things diff --git a/compiler/rename/RnNames.hs b/compiler/rename/RnNames.hs index 2fc62637e8..3803f58da6 100644 --- a/compiler/rename/RnNames.hs +++ b/compiler/rename/RnNames.hs @@ -1973,8 +1973,8 @@ exportClashErr global_env name1 name2 ie1 ie2 -- get_gre finds a GRE for the Name, so that we can show its provenance get_gre name = case lookupGRE_Name global_env name of - (gre:_) -> gre - [] -> pprPanic "exportClashErr" (ppr name) + Just gre -> gre + Nothing -> pprPanic "exportClashErr" (ppr name) get_loc name = greSrcSpan (get_gre name) (name1', ie1', name2', ie2') = if get_loc name1 < get_loc name2 then (name1, ie1, name2, ie2) diff --git a/compiler/rename/RnPat.hs b/compiler/rename/RnPat.hs index d252f7fef0..8c7831497b 100644 --- a/compiler/rename/RnPat.hs +++ b/compiler/rename/RnPat.hs @@ -637,12 +637,13 @@ rnHsRecFields ctxt mk_arg (HsRecFields { rec_flds = flds, rec_dotdot = dotdot }) -- (that is, the parent of the data constructor), -- or 'Nothing' if it is a pattern synonym or not in scope. -- That's the parent to use for looking up record fields. - find_tycon env con - | Just (AConLike (RealDataCon dc)) <- wiredInNameTyThing_maybe con + find_tycon env con_name + | Just (AConLike (RealDataCon dc)) <- wiredInNameTyThing_maybe con_name = Just (tyConName (dataConTyCon dc)) -- Special case for [], which is built-in syntax -- and not in the GlobalRdrEnv (Trac #8448) - | [gre] <- lookupGRE_Name env con + + | Just gre <- lookupGRE_Name env con_name = case gre_par gre of ParentIs p -> Just p _ -> Nothing diff --git a/compiler/rename/RnSource.hs b/compiler/rename/RnSource.hs index f16da6d727..3b23bb602f 100644 --- a/compiler/rename/RnSource.hs +++ b/compiler/rename/RnSource.hs @@ -1358,11 +1358,11 @@ toParents rdr_env ns getParent :: GlobalRdrEnv -> Name -> Name getParent rdr_env n = case lookupGRE_Name rdr_env n of - gre : _ -> case gre_par gre of - ParentIs { par_is = p } -> p - FldParent { par_is = p } -> p - _ -> n - _ -> n + Just gre -> case gre_par gre of + ParentIs { par_is = p } -> p + FldParent { par_is = p } -> p + _ -> n + Nothing -> n {- Note [Extra dependencies from .hs-boot files] diff --git a/compiler/typecheck/FamInst.hs b/compiler/typecheck/FamInst.hs index 39cd1a5c84..434f1f3e60 100644 --- a/compiler/typecheck/FamInst.hs +++ b/compiler/typecheck/FamInst.hs @@ -322,7 +322,7 @@ tcTopNormaliseNewTypeTF_maybe faminsts rdr_env ty (not (isAbstractTyCon tc) && all in_scope data_con_names) where data_con_names = map dataConName (tyConDataCons tc) - in_scope dc = not $ null $ lookupGRE_Name rdr_env dc + in_scope dc = isJust (lookupGRE_Name rdr_env dc) {- ************************************************************************ diff --git a/compiler/typecheck/TcDeriv.hs b/compiler/typecheck/TcDeriv.hs index 1a93687781..fc4fb45e67 100644 --- a/compiler/typecheck/TcDeriv.hs +++ b/compiler/typecheck/TcDeriv.hs @@ -901,7 +901,7 @@ mkEqnHelp overlap_mode tvs cls cls_tys tycon tc_args mtheta hidden_data_cons = not (isWiredInName (tyConName rep_tc)) && (isAbstractTyCon rep_tc || any not_in_scope data_con_names) - not_in_scope dc = null (lookupGRE_Name rdr_env dc) + not_in_scope dc = isNothing (lookupGRE_Name rdr_env dc) ; addUsedDataCons rdr_env rep_tc ; unless (isNothing mtheta || not hidden_data_cons) diff --git a/compiler/typecheck/TcErrors.hs b/compiler/typecheck/TcErrors.hs index 2a87975442..3a0b02b449 100644 --- a/compiler/typecheck/TcErrors.hs +++ b/compiler/typecheck/TcErrors.hs @@ -1244,7 +1244,7 @@ mkCoercibleExplanation rdr_env fam_envs ty1 ty2 | isNewTyCon tc , [data_con] <- tyConDataCons tc , let dc_name = dataConName data_con - , null (lookupGRE_Name rdr_env dc_name) + , isNothing (lookupGRE_Name rdr_env dc_name) = Just $ hang (text "The data constructor" <+> quotes (ppr dc_name)) 2 (sep [ text "of newtype" <+> quotes (pprSourceTyCon tc) , text "is not in scope" ]) diff --git a/compiler/typecheck/TcForeign.hs b/compiler/typecheck/TcForeign.hs index 02227c7ecb..aef1e9ca7b 100644 --- a/compiler/typecheck/TcForeign.hs +++ b/compiler/typecheck/TcForeign.hs @@ -189,7 +189,7 @@ normaliseFfiType' env ty0 = go initRecTc ty0 checkNewtypeFFI :: GlobalRdrEnv -> TyCon -> Maybe GlobalRdrElt checkNewtypeFFI rdr_env tc | Just con <- tyConSingleDataCon_maybe tc - , [gre] <- lookupGRE_Name rdr_env (dataConName con) + , Just gre <- lookupGRE_Name rdr_env (dataConName con) = Just gre -- See Note [Newtype constructor usage in foreign declarations] | otherwise = Nothing |