[commit: ghc] master: Misleading msg with qualified imports "No module named X imported" (4c174dd)

git at git.haskell.org git at git.haskell.org
Tue Dec 11 23:21:51 UTC 2018


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/4c174dddc7b36ebf97ba0e182f843d563e3d598c/ghc

>---------------------------------------------------------------

commit 4c174dddc7b36ebf97ba0e182f843d563e3d598c
Author: Roland Senn <rsx at bluewin.ch>
Date:   Tue Dec 11 13:20:37 2018 -0500

    Misleading msg with qualified imports "No module named X imported"
    
    To check whether a given module has been imported, we do the following:
    From the list of all qualified names we extract the distinct module
    names to a list of module names.
    Then we check whether the given module name is in this list of module
    names.
    
    Test Plan: make test TEST=T14225
    
    Reviewers: mpickering, hvr, monoidal, osa1, bgamari
    
    Reviewed By: bgamari
    
    Subscribers: rwbarton, carter
    
    GHC Trac Issues: #14225
    
    Differential Revision: https://phabricator.haskell.org/D5331


>---------------------------------------------------------------

4c174dddc7b36ebf97ba0e182f843d563e3d598c
 compiler/rename/RnUnbound.hs                     | 50 +++++++++++++-----------
 testsuite/tests/rename/should_fail/T14225.script |  2 +
 testsuite/tests/rename/should_fail/T14225.stderr |  3 ++
 testsuite/tests/rename/should_fail/all.T         |  1 +
 4 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/compiler/rename/RnUnbound.hs b/compiler/rename/RnUnbound.hs
index bf481c5..bdda66f 100644
--- a/compiler/rename/RnUnbound.hs
+++ b/compiler/rename/RnUnbound.hs
@@ -105,7 +105,8 @@ unknownNameSuggestions_ :: WhereLooking -> DynFlags
 unknownNameSuggestions_ where_look dflags hpt curr_mod global_env local_env
                           imports tried_rdr_name =
     similarNameSuggestions where_look dflags global_env local_env tried_rdr_name $$
-    importSuggestions where_look hpt curr_mod imports tried_rdr_name $$
+    importSuggestions where_look global_env hpt
+                      curr_mod imports tried_rdr_name $$
     extensionSuggestions tried_rdr_name
 
 
@@ -165,26 +166,20 @@ similarNameSuggestions where_look dflags global_env
                         , let occ = nameOccName name
                         , correct_name_space occ]
 
-    gre_ok :: GlobalRdrElt -> Bool
-    gre_ok = case where_look of
-                   WL_LocalTop  -> isLocalGRE
-                   WL_LocalOnly -> const False
-                   _            -> const True
-
     global_possibilities :: GlobalRdrEnv -> [(RdrName, (RdrName, HowInScope))]
     global_possibilities global_env
       | tried_is_qual = [ (rdr_qual, (rdr_qual, how))
                         | gre <- globalRdrEnvElts global_env
-                        , gre_ok gre
+                        , isGreOk where_look gre
                         , let name = gre_name gre
                               occ  = nameOccName name
                         , correct_name_space occ
-                        , (mod, how) <- quals_in_scope gre
+                        , (mod, how) <- qualsInScope gre
                         , let rdr_qual = mkRdrQual mod occ ]
 
       | otherwise = [ (rdr_unqual, pair)
                     | gre <- globalRdrEnvElts global_env
-                    , gre_ok gre
+                    , isGreOk where_look gre
                     , let name = gre_name gre
                           occ  = nameOccName name
                           rdr_unqual = mkRdrUnqual occ
@@ -212,15 +207,6 @@ similarNameSuggestions where_look dflags global_env
                     | i <- is, let ispec = is_decl i
                     , not (is_qual ispec) ]
 
-    --------------------
-    quals_in_scope :: GlobalRdrElt -> [(ModuleName, HowInScope)]
-    -- Ones for which the qualified version is in scope
-    quals_in_scope (GRE { gre_name = n, gre_lcl = lcl, gre_imp = is })
-      | lcl = case nameModule_maybe n of
-                Nothing -> []
-                Just m  -> [(moduleName m, Left (nameSrcSpan n))]
-      | otherwise = [ (is_as ispec, Right ispec)
-                    | i <- is, let ispec = is_decl i ]
 
     --------------------
     quals_only :: GlobalRdrElt -> [(RdrName, HowInScope)]
@@ -231,9 +217,10 @@ similarNameSuggestions where_look dflags global_env
 
 -- | Generate helpful suggestions if a qualified name Mod.foo is not in scope.
 importSuggestions :: WhereLooking
+                  -> GlobalRdrEnv
                   -> HomePackageTable -> Module
                   -> ImportAvails -> RdrName -> SDoc
-importSuggestions where_look hpt currMod imports rdr_name
+importSuggestions where_look global_env hpt currMod imports rdr_name
   | WL_LocalOnly <- where_look                 = Outputable.empty
   | not (isQual rdr_name || isUnqual rdr_name) = Outputable.empty
   | null interesting_imports
@@ -344,8 +331,7 @@ importSuggestions where_look hpt currMod imports rdr_name
   -- See note [When to show/hide the module-not-imported line]
   show_not_imported_line :: ModuleName -> Bool                    -- #15611
   show_not_imported_line modnam
-      | modnam `elem`
-          fmap moduleName (moduleEnvKeys (imp_mods imports)) = False   -- 1
+      | modnam `elem` globMods                = False    -- #14225     -- 1
       | moduleName currMod == modnam          = False                  -- 2.1
       | is_last_loaded_mod modnam hpt_uniques = False                  -- 2.2
       | otherwise                             = True
@@ -353,6 +339,11 @@ importSuggestions where_look hpt currMod imports rdr_name
       hpt_uniques = map fst (udfmToList hpt)
       is_last_loaded_mod _ []         = False
       is_last_loaded_mod modnam uniqs = last uniqs == getUnique modnam
+      globMods = nub [ mod
+                     | gre <- globalRdrEnvElts global_env
+                     , isGreOk where_look gre
+                     , (mod, _) <- qualsInScope gre
+                     ]
 
 extensionSuggestions :: RdrName -> SDoc
 extensionSuggestions rdrName
@@ -366,6 +357,21 @@ perhapsForallMsg
   = vcat [ text "Perhaps you intended to use ExplicitForAll or similar flag"
          , text "to enable explicit-forall syntax: forall <tvs>. <type>"]
 
+qualsInScope :: GlobalRdrElt -> [(ModuleName, HowInScope)]
+-- Ones for which the qualified version is in scope
+qualsInScope GRE { gre_name = n, gre_lcl = lcl, gre_imp = is }
+      | lcl = case nameModule_maybe n of
+                Nothing -> []
+                Just m  -> [(moduleName m, Left (nameSrcSpan n))]
+      | otherwise = [ (is_as ispec, Right ispec)
+                    | i <- is, let ispec = is_decl i ]
+
+isGreOk :: WhereLooking -> GlobalRdrElt -> Bool
+isGreOk where_look = case where_look of
+                         WL_LocalTop  -> isLocalGRE
+                         WL_LocalOnly -> const False
+                         _            -> const True
+
 {- Note [When to show/hide the module-not-imported line]           -- #15611
 For the error message:
     Not in scope X.Y
diff --git a/testsuite/tests/rename/should_fail/T14225.script b/testsuite/tests/rename/should_fail/T14225.script
new file mode 100644
index 0000000..93f212f
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T14225.script
@@ -0,0 +1,2 @@
+import qualified Data.Maybe as M
+M.fromJusr
diff --git a/testsuite/tests/rename/should_fail/T14225.stderr b/testsuite/tests/rename/should_fail/T14225.stderr
new file mode 100644
index 0000000..f54e463
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T14225.stderr
@@ -0,0 +1,3 @@
+<interactive>:2:1: error:
+    Not in scope: ‘M.fromJusr’
+    Perhaps you meant ‘M.fromJust’ (imported from Data.Maybe)
diff --git a/testsuite/tests/rename/should_fail/all.T b/testsuite/tests/rename/should_fail/all.T
index 6fd0143..ba69754 100644
--- a/testsuite/tests/rename/should_fail/all.T
+++ b/testsuite/tests/rename/should_fail/all.T
@@ -129,6 +129,7 @@ test('T13644', normal, multimod_compile_fail, ['T13644','-v0'])
 test('T13568', normal, multimod_compile_fail, ['T13568','-v0'])
 test('T13947', normal, compile_fail, [''])
 test('T13847', normal, multimod_compile_fail, ['T13847','-v0'])
+test('T14225', normal, ghci_script, ['T14225.script'])
 test('T14307', normal, compile_fail, [''])
 test('T14591', normal, compile_fail, [''])
 test('T14907a', normal, compile_fail, [''])



More information about the ghc-commits mailing list