summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsheaf <sam.derbyshire@gmail.com>2022-04-07 19:03:05 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-04-08 09:43:35 -0400
commit777365f18233d7ad032435ea2c93197cbb1d732e (patch)
tree059563b83d3ad3270a913cc834dfbbe8e2851e5d
parentc44432db254d2fc960d7864e080cb50e65dfa7c6 (diff)
downloadhaskell-777365f18233d7ad032435ea2c93197cbb1d732e.tar.gz
Correctly report SrcLoc of redundant constraints
We were accidentally dropping the source location information in certain circumstances when reporting redundant constraints. This patch makes sure that we set the TcLclEnv correctly before reporting the warning. Fixes #21315
-rw-r--r--compiler/GHC/Tc/Errors.hs19
-rw-r--r--compiler/GHC/Tc/Errors/Types.hs9
-rw-r--r--compiler/GHC/Tc/Utils/Monad.hs2
-rw-r--r--testsuite/tests/typecheck/should_compile/T21315.hs6
-rw-r--r--testsuite/tests/typecheck/should_compile/T21315.stderr4
-rw-r--r--testsuite/tests/typecheck/should_compile/all.T1
6 files changed, 32 insertions, 9 deletions
diff --git a/compiler/GHC/Tc/Errors.hs b/compiler/GHC/Tc/Errors.hs
index 9b9eb8077b..f662495f2c 100644
--- a/compiler/GHC/Tc/Errors.hs
+++ b/compiler/GHC/Tc/Errors.hs
@@ -393,17 +393,22 @@ warnRedundantConstraints ctxt env info ev_vars
= return ()
| SigSkol user_ctxt _ _ <- info
- = restoreLclEnv env $ -- We want to add "In the type signature for f"
- -- to the error context, which is a bit tiresome
+ -- When dealing with a user-written type signature,
+ -- we want to add "In the type signature for f".
+ = restoreLclEnv env $
setSrcSpan (redundantConstraintsSpan user_ctxt) $
report_redundant_msg True
+ -- ^^^^ add "In the type signature..."
- | otherwise -- But for InstSkol there already *is* a surrounding
- -- "In the instance declaration for Eq [a]" context
- -- and we don't want to say it twice. Seems a bit ad-hoc
- = report_redundant_msg False
+ | otherwise
+ -- But for InstSkol there already *is* a surrounding
+ -- "In the instance declaration for Eq [a]" context
+ -- and we don't want to say it twice. Seems a bit ad-hoc
+ = restoreLclEnv env
+ $ report_redundant_msg False
+ -- ^^^^^ don't add "In the type signature..."
where
- report_redundant_msg :: Bool -- whether to add "In ..." to the diagnostic
+ report_redundant_msg :: Bool -- whether to add "In the type signature..." to the diagnostic
-> TcRn ()
report_redundant_msg show_info
= do { lcl_env <- getLclEnv
diff --git a/compiler/GHC/Tc/Errors/Types.hs b/compiler/GHC/Tc/Errors/Types.hs
index 113e89c15b..3c67bcb507 100644
--- a/compiler/GHC/Tc/Errors/Types.hs
+++ b/compiler/GHC/Tc/Errors/Types.hs
@@ -204,7 +204,14 @@ data TcRnMessage where
Test cases: T9939, T10632, T18036a, T20602, PluralS, T19296.
-}
- TcRnRedundantConstraints :: [Id] -> (SkolemInfoAnon, Bool) -> TcRnMessage
+ TcRnRedundantConstraints :: [Id]
+ -> (SkolemInfoAnon, Bool)
+ -- ^ The contextual skolem info.
+ -- The boolean controls whether we
+ -- want to show it in the user message.
+ -- (Nice to keep track of the info in either case,
+ -- for other users of the GHC API.)
+ -> TcRnMessage
{-| TcRnInaccessibleCode is a warning that is emitted when the RHS of a pattern
match is inaccessible, because the constraint solver has detected a contradiction.
diff --git a/compiler/GHC/Tc/Utils/Monad.hs b/compiler/GHC/Tc/Utils/Monad.hs
index 75d6491bad..dada2c8041 100644
--- a/compiler/GHC/Tc/Utils/Monad.hs
+++ b/compiler/GHC/Tc/Utils/Monad.hs
@@ -536,7 +536,7 @@ The `tcRnSrcDecls` extends the environments in `gbl_env` and `lcl_env`
which we then want to be in scope in `more stuff`.
The problem is that `lcl_env :: TcLclEnv` has an IORef for error
-messages `tcl_errs`, and another for constraints (`tcl_lie`),a and
+messages `tcl_errs`, and another for constraints (`tcl_lie`), and
another for Linear Haskell usage information (`tcl_usage`). Now
suppose we change it a tiny bit
do { (gbl_env, lcl_env) <- checkNoErrs $
diff --git a/testsuite/tests/typecheck/should_compile/T21315.hs b/testsuite/tests/typecheck/should_compile/T21315.hs
new file mode 100644
index 0000000000..34a8c8296f
--- /dev/null
+++ b/testsuite/tests/typecheck/should_compile/T21315.hs
@@ -0,0 +1,6 @@
+module T21315 where
+
+data T a = MkT a deriving (Eq, Ord)
+
+class Ord a => C a
+instance (Eq a, Ord a) => C (T a)
diff --git a/testsuite/tests/typecheck/should_compile/T21315.stderr b/testsuite/tests/typecheck/should_compile/T21315.stderr
new file mode 100644
index 0000000000..ad718569ec
--- /dev/null
+++ b/testsuite/tests/typecheck/should_compile/T21315.stderr
@@ -0,0 +1,4 @@
+
+T21315.hs:6:10: warning: [-Wredundant-constraints]
+ • Redundant constraint: Eq a
+ • In the instance declaration for ‘C (T a)’
diff --git a/testsuite/tests/typecheck/should_compile/all.T b/testsuite/tests/typecheck/should_compile/all.T
index a503d60b7c..f110e273a8 100644
--- a/testsuite/tests/typecheck/should_compile/all.T
+++ b/testsuite/tests/typecheck/should_compile/all.T
@@ -823,3 +823,4 @@ test('T18529', normal, compile, ['-ddump-tc -fprint-explicit-foralls -dsuppress-
test('T21023', normal, compile, ['-ddump-types'])
test('T21205', normal, compile, ['-O0'])
test('T21323', normal, compile, [''])
+test('T21315', normal, compile, ['-Wredundant-constraints'])