summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorJoachim Breitner <mail@joachim-breitner.de>2021-12-12 15:47:29 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-12-14 20:52:00 -0500
commita5d8d47f2a95de4b66c7299c25c301b89e6252f6 (patch)
treec218f03c01469ceb38978b2609f2217036d4432c /docs
parent1c8d609a0509d99722ccc0056dbf3336904bf102 (diff)
downloadhaskell-a5d8d47f2a95de4b66c7299c25c301b89e6252f6.tar.gz
Ghci environment: Do not remove shadowed ids
Names defined earier but shadowed need to be kept around, e.g. for type signatures: ``` ghci> data T = T ghci> let t = T ghci> data T = T ghci> :t t t :: Ghci1.T ``` and indeed they can be used: ``` ghci> let t2 = Ghci1.T :: Ghci1.T ghci> :t t2 t2 :: Ghci1.T ``` However, previously this did not happen for ids (non-types), although they are still around under the qualified name internally: ``` ghci> let t = "other t" ghci> t' <interactive>:8:1: error: • Variable not in scope: t' • Perhaps you meant one of these: ‘Ghci2.t’ (imported from Ghci2), ‘t’ (line 7), ‘t2’ (line 5) ghci> Ghci2.t <interactive>:9:1: error: • GHC internal error: ‘Ghci2.t’ is not in scope during type checking, but it passed the renamer tcl_env of environment: [] • In the expression: Ghci2.t In an equation for ‘it’: it = Ghci2.t ``` This fixes the problem by simply removing the code that tries to remove shadowed ids from the environment. Now you can refer to shadowed ids using `Ghci2.t`, just like you can do for data and type constructors. This simplifies the code, makes terms and types more similar, and also fixes #20455. Now all names ever defined in GHCi are in `ic_tythings`, which is printed by `:show bindings`. But for that commands, it seems to be more ergonomic to only list those bindings that are not shadowed. Or, even if it is not more ergonomic, it’s the current behavour. So let's restore that by filtering in `icInScopeTTs`. Of course a single `TyThing` can be associated with many names. We keep it it in the bindings if _any_ of its names are still visible unqualifiedly. It's a judgement call. This commit also turns a rather old comment into a test files. The comment is is rather stale and things are better explained elsewhere. Fixes #925. Two test cases are regressing: T14052(ghci) ghc/alloc 2749444288.0 12192109912.0 +343.4% BAD T14052Type(ghci) ghc/alloc 7365784616.0 10767078344.0 +46.2% BAD This is not unexpected; the `ic_tythings list grows` a lot more if we don’t remove shadowed Ids. I tried to alleviate it a bit with earlier MRs, but couldn’t make up for it completely. Metric Increase: T14052 T14052Type
Diffstat (limited to 'docs')
-rw-r--r--docs/users_guide/ghci.rst40
1 files changed, 40 insertions, 0 deletions
diff --git a/docs/users_guide/ghci.rst b/docs/users_guide/ghci.rst
index 72fba318bf..07377dab77 100644
--- a/docs/users_guide/ghci.rst
+++ b/docs/users_guide/ghci.rst
@@ -910,6 +910,46 @@ GHCi knows about. Using :ghci-cmd:`:module` or ``import`` to try bring into
scope a non-loaded module may result in the message
``module M is not loaded``.
+Shadowing and the ``Ghci1`` module name
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Bindings on the prompt can shadow earlier bindings:
+
+.. code-block:: none
+
+ ghci> let foo = True
+ ghci> let foo = False
+ ghci> :show bindings
+ foo :: Bool = False
+
+But the shadowed thing still exists, and may show up again later, for example
+in a type signature:
+
+.. code-block:: none
+
+ ghci> data T = A | B deriving Eq
+ ghci> let a = A
+ ghci> data T = ANewType
+ ghci> :t a
+ a :: Ghci1.T
+
+Now the type of ``a`` is printed using the fully qualified name of ``T``, using
+the module name ``Ghci1`` (and ``Ghci2`` for the next set of bindings, and so
+on). You can use these qualified names as well:
+
+.. code-block:: none
+
+ ghci> a == Ghci1.A
+ True
+ ghci> let a = False -- shadowing a
+ ghci> Ghci2.a == Ghci1.A
+ True
+
+The command ``:show bindings`` only shows bindings that are not shadowed.
+Bindings that define multiple names, such as a type constructor and its data
+constructors, are shown if *any* defined name is still available without the
+need for qualification.
+
The ``it`` variable
~~~~~~~~~~~~~~~~~~~