on master help sign in
Show `import XYZ as X` for ambiguous qualified names
commitae89ac798457a6b21333156689763315e56148c5 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 6 Feb 2018, 23:27 (9 years ago) parent2abb904
1 file changed+45−21
src/Reporting/Error/Canonicalize.hs+45−21
src/Reporting/Error/Canonicalize.hs+45−21
@@ -27,7 +27,7 @@ import qualified Reporting.Region as R
2727import qualified Reporting.Render.Code as Code
2828import qualified Reporting.Report as Report
2929import qualified Reporting.Helpers as H
30−import Reporting.Helpers ( Doc, (<>) )
30+import Reporting.Helpers ( Doc, (<+>), (<>) )
3131
3232
3333
@@ -164,7 +164,7 @@ toReport source err =
164164 )
165165
166166 AmbiguousVar region maybePrefix name possibleHomes ->
167− ambiguousName source region maybePrefix name possibleHomes "name"
167+ ambiguousName source region maybePrefix name possibleHomes "variable"
168168
169169 AmbiguousType region maybePrefix name possibleHomes ->
170170 ambiguousName source region maybePrefix name possibleHomes "type"
@@ -823,27 +823,51 @@ nameClash source r1 r2 messageThatEndsWithPunctuation =
823823
824824ambiguousName :: Code.Source -> R.Region -> Maybe N.Name -> N.Name -> [ModuleName.Canonical] -> String -> Report.Report
825825ambiguousName source region maybePrefix name possibleHomes thing =
826− let
827− givenName =
828− maybe N.toString toQualString maybePrefix name
826+ Report.Report "AMBIGUOUS NAME" region [] $
827+ Report.toCodeSnippet source region Nothing $
828+ case maybePrefix of
829+ Nothing ->
830+ let
831+ homeToYellowDoc (ModuleName.Canonical _ home) =
832+ H.dullyellow (H.nameToDoc home)
833+
834+ bothOrAll =
835+ if length possibleHomes == 2 then "both" else "all"
836+ in
837+ (
838+ H.reflow $ "This usage of `" ++ N.toString name ++ "` is ambiguous."
839+ ,
840+ H.stack
841+ [ H.reflow $
842+ "Check your imports. The following modules " ++ bothOrAll
843+ ++ " expose a `" ++ N.toString name ++ "` " ++ thing ++ ":"
844+ , H.indent 4 $ H.vcat $ map homeToYellowDoc possibleHomes
845+ , H.reflowLink "Read" "imports" "to learn how to clarify which one you want."
846+ ]
847+ )
829848
830− homeToYellowDoc (ModuleName.Canonical _ home) =
831− H.dullyellow (H.nameToDoc home)
849+ Just prefix ->
850+ let
851+ homeToYellowDoc (ModuleName.Canonical _ home) =
852+ if prefix == home then
853+ H.blue "import" <+> H.dullyellow (H.nameToDoc home)
854+ else
855+ H.blue "import" <+> H.dullyellow (H.nameToDoc home) <+> H.blue "as" <+> H.dullyellow (H.nameToDoc prefix)
832856
833− bothOrAll =
834− if length possibleHomes == 2 then "both" else "all"
835− in
836− Report.Report "AMBIGUOUS NAME" region [] $
837− Report.toCodeSnippet source region Nothing
838− (
839− H.reflow $ "This usage of `" ++ givenName ++ "` is ambiguous."
840− ,
841− H.stack
842− [ H.reflow $ "The following modules " ++ bothOrAll ++ " export this " ++ thing ++ ":"
843− , H.indent 4 $ H.vcat $ map homeToYellowDoc possibleHomes
844− , H.reflowLink "Read" "imports" "to learn how to clarify which one you want."
845− ]
846− )
857+ eitherOrAny =
858+ if length possibleHomes == 2 then "either" else "any"
859+ in
860+ (
861+ H.reflow $ "This usage of `" ++ toQualString prefix name ++ "` is ambiguous."
862+ ,
863+ H.stack
864+ [ H.reflow $
865+ "It could refer to a " ++ thing ++ " from "
866+ ++ eitherOrAny ++ " of these imports:"
867+ , H.indent 4 $ H.vcat $ map homeToYellowDoc possibleHomes
868+ , H.reflowLink "Read" "imports" "to learn how to clarify which one you want."
869+ ]
870+ )
847871
848872
849873