Get rid of some stuttering in comments and docs
[ghc.git] / compiler / rename / RnFixity.hs
1 {-
2
3 This module contains code which maintains and manipulates the
4 fixity environment during renaming.
5
6 -}
7 module RnFixity ( MiniFixityEnv,
8 addLocalFixities,
9 lookupFixityRn, lookupFixityRn_help,
10 lookupFieldFixityRn, lookupTyFixityRn ) where
11
12 import GhcPrelude
13
14 import LoadIface
15 import HsSyn
16 import RdrName
17 import HscTypes
18 import TcRnMonad
19 import Name
20 import NameEnv
21 import Module
22 import BasicTypes ( Fixity(..), FixityDirection(..), minPrecedence,
23 defaultFixity, SourceText(..) )
24 import SrcLoc
25 import Outputable
26 import Maybes
27 import Data.List
28 import Data.Function ( on )
29 import RnUnbound
30
31 {-
32 *********************************************************
33 * *
34 Fixities
35 * *
36 *********************************************************
37
38 Note [Fixity signature lookup]
39 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 A fixity declaration like
41
42 infixr 2 ?
43
44 can refer to a value-level operator, e.g.:
45
46 (?) :: String -> String -> String
47
48 or a type-level operator, like:
49
50 data (?) a b = A a | B b
51
52 so we extend the lookup of the reader name '?' to the TcClsName namespace, as
53 well as the original namespace.
54
55 The extended lookup is also used in other places, like resolution of
56 deprecation declarations, and lookup of names in GHCi.
57 -}
58
59 --------------------------------
60 type MiniFixityEnv = FastStringEnv (Located Fixity)
61 -- Mini fixity env for the names we're about
62 -- to bind, in a single binding group
63 --
64 -- It is keyed by the *FastString*, not the *OccName*, because
65 -- the single fixity decl infix 3 T
66 -- affects both the data constructor T and the type constrctor T
67 --
68 -- We keep the location so that if we find
69 -- a duplicate, we can report it sensibly
70
71 --------------------------------
72 -- Used for nested fixity decls to bind names along with their fixities.
73 -- the fixities are given as a UFM from an OccName's FastString to a fixity decl
74
75 addLocalFixities :: MiniFixityEnv -> [Name] -> RnM a -> RnM a
76 addLocalFixities mini_fix_env names thing_inside
77 = extendFixityEnv (mapMaybe find_fixity names) thing_inside
78 where
79 find_fixity name
80 = case lookupFsEnv mini_fix_env (occNameFS occ) of
81 Just (L _ fix) -> Just (name, FixItem occ fix)
82 Nothing -> Nothing
83 where
84 occ = nameOccName name
85
86 {-
87 --------------------------------
88 lookupFixity is a bit strange.
89
90 * Nested local fixity decls are put in the local fixity env, which we
91 find with getFixtyEnv
92
93 * Imported fixities are found in the PIT
94
95 * Top-level fixity decls in this module may be for Names that are
96 either Global (constructors, class operations)
97 or Local/Exported (everything else)
98 (See notes with RnNames.getLocalDeclBinders for why we have this split.)
99 We put them all in the local fixity environment
100 -}
101
102 lookupFixityRn :: Name -> RnM Fixity
103 lookupFixityRn name = lookupFixityRn' name (nameOccName name)
104
105 lookupFixityRn' :: Name -> OccName -> RnM Fixity
106 lookupFixityRn' name = fmap snd . lookupFixityRn_help' name
107
108 -- | 'lookupFixityRn_help' returns @(True, fixity)@ if it finds a 'Fixity'
109 -- in a local environment or from an interface file. Otherwise, it returns
110 -- @(False, fixity)@ (e.g., for unbound 'Name's or 'Name's without
111 -- user-supplied fixity declarations).
112 lookupFixityRn_help :: Name
113 -> RnM (Bool, Fixity)
114 lookupFixityRn_help name =
115 lookupFixityRn_help' name (nameOccName name)
116
117 lookupFixityRn_help' :: Name
118 -> OccName
119 -> RnM (Bool, Fixity)
120 lookupFixityRn_help' name occ
121 | isUnboundName name
122 = return (False, Fixity NoSourceText minPrecedence InfixL)
123 -- Minimise errors from ubound names; eg
124 -- a>0 `foo` b>0
125 -- where 'foo' is not in scope, should not give an error (Trac #7937)
126
127 | otherwise
128 = do { local_fix_env <- getFixityEnv
129 ; case lookupNameEnv local_fix_env name of {
130 Just (FixItem _ fix) -> return (True, fix) ;
131 Nothing ->
132
133 do { this_mod <- getModule
134 ; if nameIsLocalOrFrom this_mod name
135 -- Local (and interactive) names are all in the
136 -- fixity env, and don't have entries in the HPT
137 then return (False, defaultFixity)
138 else lookup_imported } } }
139 where
140 lookup_imported
141 -- For imported names, we have to get their fixities by doing a
142 -- loadInterfaceForName, and consulting the Ifaces that comes back
143 -- from that, because the interface file for the Name might not
144 -- have been loaded yet. Why not? Suppose you import module A,
145 -- which exports a function 'f', thus;
146 -- module CurrentModule where
147 -- import A( f )
148 -- module A( f ) where
149 -- import B( f )
150 -- Then B isn't loaded right away (after all, it's possible that
151 -- nothing from B will be used). When we come across a use of
152 -- 'f', we need to know its fixity, and it's then, and only
153 -- then, that we load B.hi. That is what's happening here.
154 --
155 -- loadInterfaceForName will find B.hi even if B is a hidden module,
156 -- and that's what we want.
157 = do { iface <- loadInterfaceForName doc name
158 ; let mb_fix = mi_fix_fn iface occ
159 ; let msg = case mb_fix of
160 Nothing ->
161 text "looking up name" <+> ppr name
162 <+> text "in iface, but found no fixity for it."
163 <+> text "Using default fixity instead."
164 Just f ->
165 text "looking up name in iface and found:"
166 <+> vcat [ppr name, ppr f]
167 ; traceRn "lookupFixityRn_either:" msg
168 ; return (maybe (False, defaultFixity) (\f -> (True, f)) mb_fix) }
169
170 doc = text "Checking fixity for" <+> ppr name
171
172 ---------------
173 lookupTyFixityRn :: Located Name -> RnM Fixity
174 lookupTyFixityRn (L _ n) = lookupFixityRn n
175
176 -- | Look up the fixity of a (possibly ambiguous) occurrence of a record field
177 -- selector. We use 'lookupFixityRn'' so that we can specifiy the 'OccName' as
178 -- the field label, which might be different to the 'OccName' of the selector
179 -- 'Name' if @DuplicateRecordFields@ is in use (Trac #1173). If there are
180 -- multiple possible selectors with different fixities, generate an error.
181 lookupFieldFixityRn :: AmbiguousFieldOcc GhcRn -> RnM Fixity
182 lookupFieldFixityRn (Unambiguous (L _ rdr) n)
183 = lookupFixityRn' n (rdrNameOcc rdr)
184 lookupFieldFixityRn (Ambiguous (L _ rdr) _) = get_ambiguous_fixity rdr
185 where
186 get_ambiguous_fixity :: RdrName -> RnM Fixity
187 get_ambiguous_fixity rdr_name = do
188 traceRn "get_ambiguous_fixity" (ppr rdr_name)
189 rdr_env <- getGlobalRdrEnv
190 let elts = lookupGRE_RdrName rdr_name rdr_env
191
192 fixities <- groupBy ((==) `on` snd) . zip elts
193 <$> mapM lookup_gre_fixity elts
194
195 case fixities of
196 -- There should always be at least one fixity.
197 -- Something's very wrong if there are no fixity candidates, so panic
198 [] -> panic "get_ambiguous_fixity: no candidates for a given RdrName"
199 [ (_, fix):_ ] -> return fix
200 ambigs -> addErr (ambiguous_fixity_err rdr_name ambigs)
201 >> return (Fixity NoSourceText minPrecedence InfixL)
202
203 lookup_gre_fixity gre = lookupFixityRn' (gre_name gre) (greOccName gre)
204
205 ambiguous_fixity_err rn ambigs
206 = vcat [ text "Ambiguous fixity for record field" <+> quotes (ppr rn)
207 , hang (text "Conflicts: ") 2 . vcat .
208 map format_ambig $ concat ambigs ]
209
210 format_ambig (elt, fix) = hang (ppr fix)
211 2 (pprNameProvenance elt)