summaryrefslogtreecommitdiff
path: root/compiler/GHC/Rename
diff options
context:
space:
mode:
authorShayne Fletcher <shayne@shaynefletcher.org>2021-04-24 16:59:26 -0400
committerShayne Fletcher <shayne@shaynefletcher.org>2021-05-23 08:02:58 +1000
commit0b1eed74e8ad5194152ed656ac3e4a547726b70a (patch)
tree3e654267d7077050a2358910ebe0ef29cfdddb0d /compiler/GHC/Rename
parentef4d2999a200f22c864d7c1a2bdfbfd726a0f849 (diff)
downloadhaskell-0b1eed74e8ad5194152ed656ac3e4a547726b70a.tar.gz
Change representation of field selector occurences
- Change the names of the fields in in `data FieldOcc` - Renames `HsRecFld` to `HsRecSel` - Replace `AmbiguousFieldOcc p` in `HsRecSel` with `FieldOcc p` - Contains a haddock submodule update The primary motivation of this change is to remove `AmbiguousFieldOcc`. This is one of a suite of changes improving how record syntax (most notably record update syntax) is represented in the AST.
Diffstat (limited to 'compiler/GHC/Rename')
-rw-r--r--compiler/GHC/Rename/Bind.hs4
-rw-r--r--compiler/GHC/Rename/Env.hs24
-rw-r--r--compiler/GHC/Rename/Expr.hs16
-rw-r--r--compiler/GHC/Rename/Fixity.hs45
-rw-r--r--compiler/GHC/Rename/HsType.hs14
-rw-r--r--compiler/GHC/Rename/Module.hs2
-rw-r--r--compiler/GHC/Rename/Pat.hs2
7 files changed, 36 insertions, 71 deletions
diff --git a/compiler/GHC/Rename/Bind.hs b/compiler/GHC/Rename/Bind.hs
index 1bd7a583b4..609ab180f9 100644
--- a/compiler/GHC/Rename/Bind.hs
+++ b/compiler/GHC/Rename/Bind.hs
@@ -706,7 +706,7 @@ rnPatSynBind sig_fn bind@(PSB { psb_id = L l name
; return ( (pat', InfixCon name1 name2)
, mkFVs (map unLoc [name1, name2])) }
RecCon vars ->
- do { checkDupRdrNames (map (rdrNameFieldOcc . recordPatSynField) vars)
+ do { checkDupRdrNames (map (foLabel . recordPatSynField) vars)
; fls <- lookupConstructorFields name
; let fld_env = mkFsEnv [ (flLabel fl, fl) | fl <- fls ]
; let rnRecordPatSynField
@@ -742,7 +742,7 @@ rnPatSynBind sig_fn bind@(PSB { psb_id = L l name
, psb_ext = fvs' }
selector_names = case details' of
RecCon names ->
- map (extFieldOcc . recordPatSynField) names
+ map (foExt . recordPatSynField) names
_ -> []
; fvs' `seq` -- See Note [Free-variable space leak]
diff --git a/compiler/GHC/Rename/Env.hs b/compiler/GHC/Rename/Env.hs
index 957b118b88..ba9a851171 100644
--- a/compiler/GHC/Rename/Env.hs
+++ b/compiler/GHC/Rename/Env.hs
@@ -1199,21 +1199,17 @@ lookupOccRn_maybe = lookupOccRnX_maybe lookupGlobalOccRn_maybe id
--
-- This may be a local variable, global variable, or one or more record selector
-- functions. It will not return record fields created with the
--- @NoFieldSelectors@ extension (see Note [NoFieldSelectors]). The
--- 'DuplicateRecordFields' argument controls whether ambiguous fields will be
--- allowed (resulting in an 'AmbiguousFields' result being returned).
+-- @NoFieldSelectors@ extension (see Note [NoFieldSelectors]).
--
-- If the name is not in scope at the term level, but its promoted equivalent is
-- in scope at the type level, the lookup will succeed (so that the type-checker
-- can report a more informative error later). See Note [Promotion].
--
-lookupExprOccRn
- :: DuplicateRecordFields -> RdrName
- -> RnM (Maybe AmbiguousResult)
-lookupExprOccRn dup_fields_ok rdr_name
- = do { mb_name <- lookupOccRnX_maybe global_lookup (UnambiguousGre . NormalGreName) rdr_name
+lookupExprOccRn :: RdrName -> RnM (Maybe GreName)
+lookupExprOccRn rdr_name
+ = do { mb_name <- lookupOccRnX_maybe global_lookup NormalGreName rdr_name
; case mb_name of
- Nothing -> fmap @Maybe (UnambiguousGre . NormalGreName) <$> lookup_promoted rdr_name
+ Nothing -> fmap @Maybe NormalGreName <$> lookup_promoted rdr_name
-- See Note [Promotion].
-- We try looking up the name as a
-- type constructor or type variable, if
@@ -1221,8 +1217,14 @@ lookupExprOccRn dup_fields_ok rdr_name
p -> return p }
where
- global_lookup :: RdrName -> RnM (Maybe AmbiguousResult)
- global_lookup = lookupGlobalOccRn_overloaded dup_fields_ok WantNormal
+ global_lookup :: RdrName -> RnM (Maybe GreName)
+ global_lookup rdr_name =
+ do { mb_name <- lookupGlobalOccRn_overloaded NoDuplicateRecordFields WantNormal rdr_name
+ ; case mb_name of
+ Just (UnambiguousGre name) -> return (Just name)
+ Just _ -> panic "GHC.Rename.Env.global_lookup: The impossible happened!"
+ Nothing -> return Nothing
+ }
lookupGlobalOccRn_maybe :: RdrName -> RnM (Maybe Name)
-- Looks up a RdrName occurrence in the top-level
diff --git a/compiler/GHC/Rename/Expr.hs b/compiler/GHC/Rename/Expr.hs
index bdcd7a4151..dce75ba1f2 100644
--- a/compiler/GHC/Rename/Expr.hs
+++ b/compiler/GHC/Rename/Expr.hs
@@ -211,12 +211,11 @@ rnUnboundVar v =
rnExpr (HsVar _ (L l v))
= do { dflags <- getDynFlags
- ; let dup_fields_ok = xopt_DuplicateRecordFields dflags
- ; mb_name <- lookupExprOccRn dup_fields_ok v
+ ; mb_name <- lookupExprOccRn v
; case mb_name of {
Nothing -> rnUnboundVar v ;
- Just (UnambiguousGre (NormalGreName name))
+ Just (NormalGreName name)
| name == nilDataConName -- Treat [] as an ExplicitList, so that
-- OverloadedLists works correctly
-- Note [Empty lists] in GHC.Hs.Expr
@@ -225,12 +224,11 @@ rnExpr (HsVar _ (L l v))
| otherwise
-> finishHsVar (L (na2la l) name) ;
- Just (UnambiguousGre (FieldGreName fl)) ->
+ Just (FieldGreName fl) ->
let sel_name = flSelector fl in
- return ( HsRecFld noExtField (Unambiguous sel_name (L l v) ), unitFV sel_name) ;
- Just AmbiguousFields ->
- return ( HsRecFld noExtField (Ambiguous noExtField (L l v) ), emptyFVs) } }
-
+ return ( HsRecSel noExtField (FieldOcc sel_name (L l v) ), unitFV sel_name) ;
+ }
+ }
rnExpr (HsIPVar x v)
= return (HsIPVar x v, emptyFVs)
@@ -292,7 +290,7 @@ rnExpr (OpApp _ e1 op e2)
-- should prevent bad things happening.
; fixity <- case op' of
L _ (HsVar _ (L _ n)) -> lookupFixityRn n
- L _ (HsRecFld _ f) -> lookupFieldFixityRn f
+ L _ (HsRecSel _ f) -> lookupFieldFixityRn f
_ -> return (Fixity NoSourceText minPrecedence InfixL)
-- c.f. lookupFixity for unbound
diff --git a/compiler/GHC/Rename/Fixity.hs b/compiler/GHC/Rename/Fixity.hs
index e45f3a5cdb..39462baf36 100644
--- a/compiler/GHC/Rename/Fixity.hs
+++ b/compiler/GHC/Rename/Fixity.hs
@@ -30,15 +30,11 @@ import GHC.Types.SourceText
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
-import GHC.Utils.Panic
import GHC.Data.Maybe
import GHC.Rename.Unbound
-import Data.List (groupBy)
-import Data.Function ( on )
-
{-
*********************************************************
* *
@@ -184,39 +180,10 @@ lookupFixityRn_help' name occ
lookupTyFixityRn :: LocatedN Name -> RnM Fixity
lookupTyFixityRn = lookupFixityRn . unLoc
--- | Look up the fixity of a (possibly ambiguous) occurrence of a record field
--- selector. We use 'lookupFixityRn'' so that we can specify the 'OccName' as
--- the field label, which might be different to the 'OccName' of the selector
--- 'Name' if @DuplicateRecordFields@ is in use (#1173). If there are
--- multiple possible selectors with different fixities, generate an error.
-lookupFieldFixityRn :: AmbiguousFieldOcc GhcRn -> RnM Fixity
-lookupFieldFixityRn (Unambiguous n lrdr)
+-- | Look up the fixity of an occurrence of a record field selector.
+-- We use 'lookupFixityRn'' so that we can specify the 'OccName' as
+-- the field label, which might be different to the 'OccName' of the
+-- selector 'Name' if @DuplicateRecordFields@ is in use (#1173).
+lookupFieldFixityRn :: FieldOcc GhcRn -> RnM Fixity
+lookupFieldFixityRn (FieldOcc n lrdr)
= lookupFixityRn' n (rdrNameOcc (unLoc lrdr))
-lookupFieldFixityRn (Ambiguous _ lrdr) = get_ambiguous_fixity (unLoc lrdr)
- where
- get_ambiguous_fixity :: RdrName -> RnM Fixity
- get_ambiguous_fixity rdr_name = do
- traceRn "get_ambiguous_fixity" (ppr rdr_name)
- rdr_env <- getGlobalRdrEnv
- let elts = lookupGRE_RdrName rdr_name rdr_env
-
- fixities <- groupBy ((==) `on` snd) . zip elts
- <$> mapM lookup_gre_fixity elts
-
- case fixities of
- -- There should always be at least one fixity.
- -- Something's very wrong if there are no fixity candidates, so panic
- [] -> panic "get_ambiguous_fixity: no candidates for a given RdrName"
- [ (_, fix):_ ] -> return fix
- ambigs -> addErr (ambiguous_fixity_err rdr_name ambigs)
- >> return (Fixity NoSourceText minPrecedence InfixL)
-
- lookup_gre_fixity gre = lookupFixityRn' (greMangledName gre) (greOccName gre)
-
- ambiguous_fixity_err rn ambigs
- = vcat [ text "Ambiguous fixity for record field" <+> quotes (ppr rn)
- , hang (text "Conflicts: ") 2 . vcat .
- map format_ambig $ concat ambigs ]
-
- format_ambig (elt, fix) = hang (ppr fix)
- 2 (pprNameProvenance elt)
diff --git a/compiler/GHC/Rename/HsType.hs b/compiler/GHC/Rename/HsType.hs
index f912ce84fa..92228b0003 100644
--- a/compiler/GHC/Rename/HsType.hs
+++ b/compiler/GHC/Rename/HsType.hs
@@ -1393,11 +1393,10 @@ mkOpAppRn e1 op fix e2 -- Default case, no rearrangment
----------------------------
-- | Name of an operator in an operator application or section
-data OpName = NormalOp Name -- ^ A normal identifier
- | NegateOp -- ^ Prefix negation
- | UnboundOp OccName -- ^ An unbound indentifier
- | RecFldOp (AmbiguousFieldOcc GhcRn)
- -- ^ A (possibly ambiguous) record field occurrence
+data OpName = NormalOp Name -- ^ A normal identifier
+ | NegateOp -- ^ Prefix negation
+ | UnboundOp OccName -- ^ An unbound indentifier
+ | RecFldOp (FieldOcc GhcRn) -- ^ A record field occurrence
instance Outputable OpName where
ppr (NormalOp n) = ppr n
@@ -1410,7 +1409,7 @@ get_op :: LHsExpr GhcRn -> OpName
-- See GHC.Rename.Expr.rnUnboundVar
get_op (L _ (HsVar _ n)) = NormalOp (unLoc n)
get_op (L _ (HsUnboundVar _ uv)) = UnboundOp uv
-get_op (L _ (HsRecFld _ fld)) = RecFldOp fld
+get_op (L _ (HsRecSel _ fld)) = RecFldOp fld
get_op other = pprPanic "get_op" (ppr other)
-- Parser left-associates everything, but
@@ -1574,8 +1573,7 @@ checkSectionPrec direction section op arg
(arg_op, arg_fix) section)
-- | Look up the fixity for an operator name. Be careful to use
--- 'lookupFieldFixityRn' for (possibly ambiguous) record fields
--- (see #13132).
+-- 'lookupFieldFixityRn' for record fields (see #13132).
lookupFixityOp :: OpName -> RnM Fixity
lookupFixityOp (NormalOp n) = lookupFixityRn n
lookupFixityOp NegateOp = lookupFixityRn negateName
diff --git a/compiler/GHC/Rename/Module.hs b/compiler/GHC/Rename/Module.hs
index 25bca4c0a2..61aa6a54d2 100644
--- a/compiler/GHC/Rename/Module.hs
+++ b/compiler/GHC/Rename/Module.hs
@@ -2435,7 +2435,7 @@ extendPatSynEnv dup_fields_ok has_sel val_decls local_fix_env thing = do {
, psb_args = RecCon as }))) <- bind
= do
bnd_name <- newTopSrcBinder (L (l2l bind_loc) n)
- let field_occs = map ((\ f -> L (getLocA (rdrNameFieldOcc f)) f) . recordPatSynField) as
+ let field_occs = map ((\ f -> L (getLocA (foLabel f)) f) . recordPatSynField) as
flds <- mapM (newRecordSelector dup_fields_ok has_sel [bnd_name]) field_occs
return ((bnd_name, flds): names)
| L bind_loc (PatSynBind _ (PSB { psb_id = L _ n})) <- bind
diff --git a/compiler/GHC/Rename/Pat.hs b/compiler/GHC/Rename/Pat.hs
index e9943c8be7..5e0723d4cb 100644
--- a/compiler/GHC/Rename/Pat.hs
+++ b/compiler/GHC/Rename/Pat.hs
@@ -803,7 +803,7 @@ getFieldIds flds = map (hsRecFieldSel . unLoc) flds
getFieldLbls :: forall p arg . UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls flds
- = map (unLoc . rdrNameFieldOcc . unXRec @p . hfbLHS . unXRec @p) flds
+ = map (unXRec @p . foLabel . unXRec @p . hfbLHS . unXRec @p) flds
getFieldUpdLbls :: [LHsRecUpdField GhcPs] -> [RdrName]
getFieldUpdLbls flds = map (rdrNameAmbiguousFieldOcc . unLoc . hfbLHS . unLoc) flds