Meet !16887
In this post I’m outlining what are the consequences of, and the opens questions about, the change to the diagnostics and diagnostics’ decorations I’m proposing in MR !16887.
Thanks in advance to whoever will stop by to have a thorough read and to give some feedback for the purpose of reaching consensus.
Each of the examples below shows a snippet of code accompanied by the comparison of current diagnostics with the one implemented by !16887, and some observation.
Benefits/impacts on GHC-38856
Most examples are about GHC-38856, the “redundant imports” diagnostic because improving that diagnostic (thus fixing #27746, #28011, and #28036) was the motivating case for !16887, which in fact also entails a change to compiler/GHC/Rename/Names.hs which aims at distinguishing between a redundantly imported type constructor form redundantly imported data constructor/field name (see the aforementioned issues if you’re unsure what I mean); see next section for the impact on other diagnostics.
-
module Foo where import Prelude (Int, undefined) import Data.Maybe (Maybe(Just)) s :: Maybe Int s = undefined-Foo.hs:3:20: warning: [GHC-38856] [-Wunused-imports] +Foo.hs:3:26: warning: [GHC-38856] [-Wunused-imports] The import of ‘Just’ from module ‘Data.Maybe’ is redundant | 3 | import Data.Maybe (Maybe(Just)) - | ^^^^^^^^^^^ + | ^^^^Notice that
Maybeis not redundantly imported. -
module Foo where import Prelude (Int) import Data.Maybe (Maybe(Just, Nothing)) s :: Maybe Int s = Just 3-Foo.hs:3:20: warning: [GHC-38856] [-Wunused-imports] +Foo.hs:3:32: warning: [GHC-38856] [-Wunused-imports] The import of ‘Nothing’ from module ‘Data.Maybe’ is redundant | 3 | import Data.Maybe (Maybe(Just, Nothing)) - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^Notice that
MaybeandJustare not redundantly imported. -
module Foo where import Prelude (Int) import Data.Maybe (fromMaybe, Maybe(Just), fromJust, Maybe(Nothing), isJust) s :: Maybe Int s = Just (if isJust Nothing then 3 else 2)Foo.hs:3:20: warning: [GHC-38856] [-Wunused-imports] - The import of ‘fromJust, fromMaybe’ + The import of ‘fromMaybe, fromJust’ from module ‘Data.Maybe’ is redundant | 3 | import Data.Maybe (fromMaybe, Maybe(Just), fromJust, Maybe(Nothing), isJust) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^ ^^^^^^^^Notice that
MaybeandJustare not redundantly imported. -
module Foo where import Prelude (Int, pure, undefined) import Data.Maybe (isJust, Maybe( Just, Nothing)) s :: Maybe Int s = if isJust undefined then pure 2 else pure 3-Foo.hs:3:28: warning: [GHC-38856] [-Wunused-imports] +Foo.hs:4:21: warning: [GHC-38856] [-Wunused-imports] The import of ‘Just, Nothing’ from module ‘Data.Maybe’ is redundant | -3 | import Data.Maybe (isJust, Maybe( - | ^^^^^^... +4 | Just, + | ^^^^ +5 | Nothing)) + | ^^^^^^^Notice that
Maybeis not redundantly imported.Also notice that the original code features a multiline
SrcSpan, whereas the proposed change features multiple single-linesSrcSpans.
The above examples show a few things
- !16887 constitutes a bug fix as regards the
row:columnposition of diagnostics;- I think this is an undeniable improvement, and I won’t discuss it further below, unless somebody thinks otherwise;
- !16887 constitutes a bug fix as regards the decorations associated to the diagnostics;
- according to comments in !16100, this is more controversial claim, but I think we can split it in two:
- as shown by all but the last example above, for one-line
imports, the carets drawn by !16887 are a subset of those currently drawn, meaning that !16887 is definitely not a regression, in terms of visual noise;- I think this is an undeniable improvement;
- as shown by the last example above, for multiple-lines
imports, the current code only prints the first line of the code snippet (which could still be a very long one, ifMaybe(occurs far to the right!), while !16887 prints them all;- I think this is an improvement, despite it arguably adds noise in some case, but I understand we need to reach some consensus.
- as shown by all but the last example above, for one-line
- according to comments in !16100, this is more controversial claim, but I think we can split it in two:
So I think we need to answer a couple of questions:
-
For one-line
imports only, are we happy with improving the diagnostics’ decorations and letting them be “horizontally annoyingly”? If not, how do we shorten them?My opinion is that I’m happy with that, and the motivation is that a one-line
importthat is so long that it annoys me if it ever lists some unused entity, is a problem in itself, hence I’m in favour of an “annoying” diagnostics that reminds me I should spend a few seconds re-formatting and removing redundantly imported entities.As regards possibly shortening the decorations, I’m against that, because it necessarily means either of these two, which I don’t like:
- not showing all the redundantly imported items,
- chopping pieces of source lines and collapsing them to
…or something like that.
I will stress gain that this is how things are now. !16887 doesn’t make anything worse, as far as I can tell.
-
For multiple-lines
imports only are we happy with showing all the redundantly imported items? If not, how do we shorten them?On the one hand, my opinion is still that we should be happy with that, for the same reason as above.
On the other hand, !16887 does represent a change in behavior, as shown by the last example above, so the decorations can become “vertically annoying” (beside still being possibly “horizontally annoying”).
However, the way I coded it, the diagnostics’ decorations’ vertical noise is as little as possible, as shown by this additional example:
module Foo where import Prelude (Int) import Data.Maybe (isJust, Maybe( Just, Nothing)) s :: Maybe Int s = Just (2 :: Int)Foo.hs:3:20: warning: [GHC-38856] [-Wunused-imports] - The import of ‘Nothing, isJust’ + The import of ‘isJust, Nothing’ from module ‘Data.Maybe’ is redundant | 3 | import Data.Maybe (isJust, Maybe( - | ^^^^^^^^^^^^^^... + | ^^^^^^ +5 | Nothing)) + | ^^^^^^^As you can see, line 4 is not printed, because there’s no redundantly imported item there. I could have put a
…, but I’ve decided not to, because the presence of line numbers is enough for me.I think one way to shorten them is to just not print a decoration at all if its number of lines goes beyond some threshold (to be exposed as a GHC flag).
Impact on other diagnostics
The change to compiler/GHC/Rename/Names.hs has the simplifying effect that “redundant imports” always comes with one or more single-line SrcSpans, each of which refers to a single entity.
On the other hand, other diagnostics will still come with one (or more, if/when anybody decides to take advantage of the multi-span diagnostics !16887 enables) multi-line SrcSpans, and while the original code only prints the one line where the SrcSpan starts, with my change we print them all:
module Foo where
data T = A | B | C
f :: T -> Int
f A = 1
f B =
2
Foo.hs:4:1: warning: [GHC-62161] [-Wincomplete-patterns]
In an equation for ‘f’: Patterns of type ‘T’ not matched: C
|
4 | f A = 1
- | ^^^^^^^...
+ | ^^^^^^^
+5 | f B =
+ | ^^^^^
+6 | 2
+ | ^^^
So here we have another question: do we want to show all the decoration? Or only the first line? Or, similar to what I propose in the previous section, we want to not show the decoration at all if it goes beyond some threshold number of lines (to be exposed as a GHC flag)?