diff options
author | Simon Peyton Jones <simonpj@microsoft.com> | 2014-01-09 17:58:18 +0000 |
---|---|---|
committer | Simon Peyton Jones <simonpj@microsoft.com> | 2014-01-09 17:58:48 +0000 |
commit | 73c08ab10e4077e18e459a1325996bff110360c3 (patch) | |
tree | 779ba96f15da23ff993ff7a15f05ec5adb01436e /compiler/utils | |
parent | 322b48b92b93e1250b360d21873fa9f31c142403 (diff) | |
download | haskell-73c08ab10e4077e18e459a1325996bff110360c3.tar.gz |
Re-work the naming story for the GHCi prompt (Trac #8649)
The basic idea here is simple, and described in Note [The interactive package]
in HscTypes, which starts thus:
Note [The interactive package]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type and class declarations at the command prompt are treated as if
they were defined in modules
interactive:Ghci1
interactive:Ghci2
...etc...
with each bunch of declarations using a new module, all sharing a
common package 'interactive' (see Module.interactivePackageId, and
PrelNames.mkInteractiveModule).
This scheme deals well with shadowing. For example:
ghci> data T = A
ghci> data T = B
ghci> :i A
data Ghci1.T = A -- Defined at <interactive>:2:10
Here we must display info about constructor A, but its type T has been
shadowed by the second declaration. But it has a respectable
qualified name (Ghci1.T), and its source location says where it was
defined.
So the main invariant continues to hold, that in any session an original
name M.T only refers to oe unique thing. (In a previous iteration both
the T's above were called :Interactive.T, albeit with different uniques,
which gave rise to all sorts of trouble.)
This scheme deals nicely with the original problem. It allows us to
eliminate a couple of grotseque hacks
- Note [Outputable Orig RdrName] in HscTypes
- Note [interactive name cache] in IfaceEnv
(both these comments have gone, because the hacks they describe are no
longer necessary). I was also able to simplify Outputable.QueryQualifyName,
so that it takes a Module/OccName as args rather than a Name.
However, matters are never simple, and this change took me an
unreasonably long time to get right. There are some details in
Note [The interactive package] in HscTypes.
Diffstat (limited to 'compiler/utils')
-rw-r--r-- | compiler/utils/Outputable.lhs | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/compiler/utils/Outputable.lhs b/compiler/utils/Outputable.lhs index f357208077..9cf8c33d46 100644 --- a/compiler/utils/Outputable.lhs +++ b/compiler/utils/Outputable.lhs @@ -53,7 +53,9 @@ module Outputable ( -- * Controlling the style in which output is printed BindingSite(..), - PprStyle, CodeStyle(..), PrintUnqualified, alwaysQualify, neverQualify, + PprStyle, CodeStyle(..), PrintUnqualified, + alwaysQualify, alwaysQualifyNames, alwaysQualifyModules, + neverQualify, neverQualifyNames, neverQualifyModules, QualifyName(..), sdocWithDynFlags, sdocWithPlatform, getPprStyle, withPprStyle, withPprStyleDoc, @@ -75,7 +77,7 @@ import {-# SOURCE #-} DynFlags( DynFlags, useUnicodeQuotes, unsafeGlobalDynFlags ) import {-# SOURCE #-} Module( Module, ModuleName, moduleName ) -import {-# SOURCE #-} Name( Name, nameModule ) +import {-# SOURCE #-} OccName( OccName ) import {-# SOURCE #-} StaticFlags( opt_PprStyle_Debug, opt_NoDebugOutput ) import FastString @@ -145,13 +147,20 @@ data Depth = AllTheWay -- purpose of the pair of functions that gets passed around -- when rendering 'SDoc'. +type PrintUnqualified = (QueryQualifyName, QueryQualifyModule) + -- | given an /original/ name, this function tells you which module -- name it should be qualified with when printing for the user, if -- any. For example, given @Control.Exception.catch@, which is in scope -- as @Exception.catch@, this fuction will return @Just "Exception"@. -- Note that the return value is a ModuleName, not a Module, because -- in source code, names are qualified by ModuleNames. -type QueryQualifyName = Name -> QualifyName +type QueryQualifyName = Module -> OccName -> QualifyName + +-- | For a given module, we need to know whether to print it with +-- a package name to disambiguate it. +type QueryQualifyModule = Module -> Bool + -- See Note [Printing original names] in HscTypes data QualifyName -- given P:M.T @@ -164,18 +173,11 @@ data QualifyName -- given P:M.T -- it is not in scope at all, and M.T is already bound in the -- current scope, so we must refer to it as "P:M.T" - --- | For a given module, we need to know whether to print it with --- a package name to disambiguate it. -type QueryQualifyModule = Module -> Bool - -type PrintUnqualified = (QueryQualifyName, QueryQualifyModule) - alwaysQualifyNames :: QueryQualifyName -alwaysQualifyNames n = NameQual (moduleName (nameModule n)) +alwaysQualifyNames m _ = NameQual (moduleName m) neverQualifyNames :: QueryQualifyName -neverQualifyNames _ = NameUnqual +neverQualifyNames _ _ = NameUnqual alwaysQualifyModules :: QueryQualifyModule alwaysQualifyModules _ = True @@ -296,8 +298,8 @@ sdocWithPlatform f = sdocWithDynFlags (f . targetPlatform) \begin{code} qualName :: PprStyle -> QueryQualifyName -qualName (PprUser (qual_name,_) _) n = qual_name n -qualName _other n = NameQual (moduleName (nameModule n)) +qualName (PprUser (qual_name,_) _) mod occ = qual_name mod occ +qualName _other mod _ = NameQual (moduleName mod) qualModule :: PprStyle -> QueryQualifyModule qualModule (PprUser (_,qual_mod) _) m = qual_mod m |