From ba46e63f3d6f7d0438a0262f6711f8a219c703bc Mon Sep 17 00:00:00 2001
From: Ryan Scott <ryan.gl.scott@gmail.com>
Date: Tue, 11 Jul 2017 13:59:07 -0400
Subject: [PATCH] Fix #13948 by being pickier about when to suggest DataKinds

Commit 343cb32d0983f576d344a2d04a35c3fd6eecf2c5 (#13568) made GHC a bit
too cavalier in suggesting when data constructors are in scope (and
suggesting the use of `DataKinds`). This tones down the suggestions so
that `DataKinds` is only suggested if a data constructor of that name is
actually in scope (previously, it would always suggest, even if it was
out of scope).

Fixes #13948.

Test Plan: ./validate

Reviewers: mpickering, austin, bgamari

Reviewed By: mpickering

Subscribers: rwbarton, thomie

GHC Trac Issues: #13948

Differential Revision: https://phabricator.haskell.org/D3719
---
 compiler/rename/RnEnv.hs                              | 11 ++++++++++-
 testsuite/tests/module/mod122.stderr                  |  4 +---
 testsuite/tests/module/mod123.stderr                  |  4 +---
 testsuite/tests/module/mod124.stderr                  |  1 -
 testsuite/tests/module/mod127.stderr                  |  1 -
 testsuite/tests/module/mod29.stderr                   |  1 -
 testsuite/tests/module/mod50.stderr                   |  4 +---
 testsuite/tests/parser/should_fail/readFail001.stderr |  1 -
 testsuite/tests/rename/prog003/rename.prog003.stderr  |  4 +---
 testsuite/tests/rename/should_fail/T1595a.stderr      |  1 -
 testsuite/tests/rename/should_fail/T5745.stderr       |  4 +---
 testsuite/tests/typecheck/should_fail/T1595.stderr    |  2 --
 .../tests/typecheck/should_fail/tcfail048.stderr      |  1 -
 .../tests/typecheck/should_fail/tcfail053.stderr      |  1 -
 14 files changed, 15 insertions(+), 25 deletions(-)

diff --git a/compiler/rename/RnEnv.hs b/compiler/rename/RnEnv.hs
index 2ad4413920c4..617b3556bbf4 100644
--- a/compiler/rename/RnEnv.hs
+++ b/compiler/rename/RnEnv.hs
@@ -77,6 +77,7 @@ import qualified GHC.LanguageExtensions as LangExt
 import RnUnbound
 import RnUtils
 import Data.Functor (($>))
+import Data.Maybe (isJust)
 
 {-
 *********************************************************
@@ -863,7 +864,15 @@ lookup_demoted rdr_name dflags
                                  (Reason Opt_WarnUntickedPromotedConstructors)
                                  (untickedPromConstrWarn demoted_name)
                              ; return demoted_name } }
-            else unboundNameX WL_Any rdr_name suggest_dk }
+            else do { -- We need to check if a data constructor of this name is
+                      -- in scope to give good error messages. However, we do
+                      -- not want to give an additional error if the data
+                      -- constructor happens to be out of scope! See #13947.
+                      mb_demoted_name <- discardErrs $
+                                         lookupOccRn_maybe demoted_rdr
+                    ; let suggestion | isJust mb_demoted_name = suggest_dk
+                                     | otherwise              = star_info
+                    ; unboundNameX WL_Any rdr_name suggestion } }
 
   | otherwise
   = reportUnboundName rdr_name
diff --git a/testsuite/tests/module/mod122.stderr b/testsuite/tests/module/mod122.stderr
index 66aaaf23041e..51338f0a067a 100644
--- a/testsuite/tests/module/mod122.stderr
+++ b/testsuite/tests/module/mod122.stderr
@@ -1,4 +1,2 @@
 
-mod122.hs:5:6: error:
-    Not in scope: type constructor or class ‘C’
-    A data constructor of that name is in scope; did you mean DataKinds?
+mod122.hs:5:6: error: Not in scope: type constructor or class ‘C’
diff --git a/testsuite/tests/module/mod123.stderr b/testsuite/tests/module/mod123.stderr
index 38390d05d1f5..c31f6d5a39ee 100644
--- a/testsuite/tests/module/mod123.stderr
+++ b/testsuite/tests/module/mod123.stderr
@@ -1,4 +1,2 @@
 
-mod123.hs:5:6: error:
-    Not in scope: type constructor or class ‘T’
-    A data constructor of that name is in scope; did you mean DataKinds?
+mod123.hs:5:6: error: Not in scope: type constructor or class ‘T’
diff --git a/testsuite/tests/module/mod124.stderr b/testsuite/tests/module/mod124.stderr
index cbf9f4558e55..a052a506ad2b 100644
--- a/testsuite/tests/module/mod124.stderr
+++ b/testsuite/tests/module/mod124.stderr
@@ -1,6 +1,5 @@
 
 mod124.hs:6:6: error:
     Not in scope: type constructor or class ‘T’
-    A data constructor of that name is in scope; did you mean DataKinds?
     Perhaps you want to remove ‘T’ from the explicit hiding list
     in the import of ‘Mod124_A’ (mod124.hs:4:1-26).
diff --git a/testsuite/tests/module/mod127.stderr b/testsuite/tests/module/mod127.stderr
index 462ebbccaf71..861d492d1a35 100644
--- a/testsuite/tests/module/mod127.stderr
+++ b/testsuite/tests/module/mod127.stderr
@@ -1,6 +1,5 @@
 
 mod127.hs:6:6: error:
     Not in scope: type constructor or class ‘T’
-    A data constructor of that name is in scope; did you mean DataKinds?
     Perhaps you want to remove ‘T’ from the explicit hiding list
     in the import of ‘Mod127_A’ (mod127.hs:4:1-26).
diff --git a/testsuite/tests/module/mod29.stderr b/testsuite/tests/module/mod29.stderr
index 08a019e13d06..e70c5df83d49 100644
--- a/testsuite/tests/module/mod29.stderr
+++ b/testsuite/tests/module/mod29.stderr
@@ -1,6 +1,5 @@
 
 mod29.hs:6:12: error:
     Not in scope: type constructor or class ‘Char’
-    A data constructor of that name is in scope; did you mean DataKinds?
     Perhaps you want to add ‘Char’ to the import list in the import of
     ‘Prelude’ (mod29.hs:5:1-19).
diff --git a/testsuite/tests/module/mod50.stderr b/testsuite/tests/module/mod50.stderr
index 94878a8faa9a..9669427f8f9d 100644
--- a/testsuite/tests/module/mod50.stderr
+++ b/testsuite/tests/module/mod50.stderr
@@ -1,4 +1,2 @@
 
-mod50.hs:3:22: error:
-    Not in scope: type constructor or class ‘Foo’
-    A data constructor of that name is in scope; did you mean DataKinds?
+mod50.hs:3:22: error: Not in scope: type constructor or class ‘Foo’
diff --git a/testsuite/tests/parser/should_fail/readFail001.stderr b/testsuite/tests/parser/should_fail/readFail001.stderr
index 3284c1b51c45..6425d16c49d9 100644
--- a/testsuite/tests/parser/should_fail/readFail001.stderr
+++ b/testsuite/tests/parser/should_fail/readFail001.stderr
@@ -16,4 +16,3 @@ readFail001.hs:107:42: error: Not in scope: data constructor ‘Bar’
 
 readFail001.hs:112:23: error:
     Not in scope: type constructor or class ‘Foo’
-    A data constructor of that name is in scope; did you mean DataKinds?
diff --git a/testsuite/tests/rename/prog003/rename.prog003.stderr b/testsuite/tests/rename/prog003/rename.prog003.stderr
index 6babd0383bd9..b8f84781a6ce 100644
--- a/testsuite/tests/rename/prog003/rename.prog003.stderr
+++ b/testsuite/tests/rename/prog003/rename.prog003.stderr
@@ -1,4 +1,2 @@
 
-B.hs:4:6: error:
-    Not in scope: type constructor or class ‘Class’
-    A data constructor of that name is in scope; did you mean DataKinds?
+B.hs:4:6: error: Not in scope: type constructor or class ‘Class’
diff --git a/testsuite/tests/rename/should_fail/T1595a.stderr b/testsuite/tests/rename/should_fail/T1595a.stderr
index 9b19421f3a45..bcd601a37791 100644
--- a/testsuite/tests/rename/should_fail/T1595a.stderr
+++ b/testsuite/tests/rename/should_fail/T1595a.stderr
@@ -1,4 +1,3 @@
 
 T1595a.hs:3:20: error:
     Not in scope: type constructor or class ‘Tpyo’
-    A data constructor of that name is in scope; did you mean DataKinds?
diff --git a/testsuite/tests/rename/should_fail/T5745.stderr b/testsuite/tests/rename/should_fail/T5745.stderr
index 94e3bd521718..dc590bba33f9 100644
--- a/testsuite/tests/rename/should_fail/T5745.stderr
+++ b/testsuite/tests/rename/should_fail/T5745.stderr
@@ -1,4 +1,2 @@
 
-T5745.hs:5:6: error:
-    Not in scope: type constructor or class ‘T’
-    A data constructor of that name is in scope; did you mean DataKinds?
+T5745.hs:5:6: error: Not in scope: type constructor or class ‘T’
diff --git a/testsuite/tests/typecheck/should_fail/T1595.stderr b/testsuite/tests/typecheck/should_fail/T1595.stderr
index bed30c42cf99..2f17fb8ffb2b 100644
--- a/testsuite/tests/typecheck/should_fail/T1595.stderr
+++ b/testsuite/tests/typecheck/should_fail/T1595.stderr
@@ -1,8 +1,6 @@
 
 T1595.hs:8:15: error:
     Not in scope: type constructor or class ‘DoesNotExist’
-    A data constructor of that name is in scope; did you mean DataKinds?
 
 T1595.hs:13:22: error:
     Not in scope: type constructor or class ‘DoesNotExist’
-    A data constructor of that name is in scope; did you mean DataKinds?
diff --git a/testsuite/tests/typecheck/should_fail/tcfail048.stderr b/testsuite/tests/typecheck/should_fail/tcfail048.stderr
index 4c1c300ef8f5..49c8cf036025 100644
--- a/testsuite/tests/typecheck/should_fail/tcfail048.stderr
+++ b/testsuite/tests/typecheck/should_fail/tcfail048.stderr
@@ -1,4 +1,3 @@
 
 tcfail048.hs:3:8: error:
     Not in scope: type constructor or class ‘B’
-    A data constructor of that name is in scope; did you mean DataKinds?
diff --git a/testsuite/tests/typecheck/should_fail/tcfail053.stderr b/testsuite/tests/typecheck/should_fail/tcfail053.stderr
index edd1537b1424..75308e5fca57 100644
--- a/testsuite/tests/typecheck/should_fail/tcfail053.stderr
+++ b/testsuite/tests/typecheck/should_fail/tcfail053.stderr
@@ -1,4 +1,3 @@
 
 tcfail053.hs:3:12: error:
     Not in scope: type constructor or class ‘A’
-    A data constructor of that name is in scope; did you mean DataKinds?
-- 
GitLab