[commit: ghc] master: Don't warn about name shadowing when renaming the patten in a PatSyn decl (1851349)

git at git.haskell.org git at git.haskell.org
Sun Oct 2 00:02:18 UTC 2016


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/1851349acd9e73f1c18d68f70d5cf7b46a843cb5/ghc

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

commit 1851349acd9e73f1c18d68f70d5cf7b46a843cb5
Author: Matthew Pickering <matthewtpickering at gmail.com>
Date:   Sat Oct 1 17:55:26 2016 -0400

    Don't warn about name shadowing when renaming the patten in a PatSyn decl
    
    Previously the renamer assumed that *any* time we renamed a pattern, the
    pattern was introducing new binders. This isn't true in pattern synonym
    declarations where the pattern is used as part of a definition.
    
    We add a special case to not warn in this situation.
    
    Reviewers: simonpj, austin, bgamari
    
    Reviewed By: simonpj
    
    Subscribers: simonpj, thomie
    
    Differential Revision: https://phabricator.haskell.org/D2545
    
    GHC Trac Issues: #12615


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

1851349acd9e73f1c18d68f70d5cf7b46a843cb5
 compiler/hsSyn/HsExpr.hs                        |  6 +++++
 compiler/rename/RnPat.hs                        | 30 +++++++++++++++++++++----
 testsuite/tests/patsyn/should_compile/T12615.hs | 12 ++++++++++
 testsuite/tests/patsyn/should_compile/all.T     |  2 +-
 4 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/compiler/hsSyn/HsExpr.hs b/compiler/hsSyn/HsExpr.hs
index 1ff204b..fdce60a 100644
--- a/compiler/hsSyn/HsExpr.hs
+++ b/compiler/hsSyn/HsExpr.hs
@@ -2277,6 +2277,12 @@ data HsMatchContext id
   deriving Functor
 deriving instance (DataIdPost id) => Data (HsMatchContext id)
 
+isPatSynCtxt :: HsMatchContext id -> Bool
+isPatSynCtxt ctxt =
+  case ctxt of
+    PatSyn -> True
+    _      -> False
+
 -- | Haskell Statement Context
 data HsStmtContext id
   = ListComp
diff --git a/compiler/rename/RnPat.hs b/compiler/rename/RnPat.hs
index 7e41bec..e67be63 100644
--- a/compiler/rename/RnPat.hs
+++ b/compiler/rename/RnPat.hs
@@ -62,7 +62,7 @@ import TysWiredIn          ( nilDataCon )
 import DataCon
 import qualified GHC.LanguageExtensions as LangExt
 
-import Control.Monad       ( when, liftM, ap )
+import Control.Monad       ( when, liftM, ap, unless )
 import Data.Ratio
 
 {-
@@ -248,6 +248,25 @@ We want to "see" this use, and in let-bindings we collect all uses and
 report unused variables at the binding level. So we must use bindLocalNames
 here, *not* bindLocalNameFV.  Trac #3943.
 
+
+Note: [Don't report shadowing for pattern synonyms]
+There is one special context where a pattern doesn't introduce any new binders -
+pattern synonym declarations. Therefore we don't check to see if pattern
+variables shadow existing identifiers as they are never bound to anything
+and have no scope.
+
+Without this check, there would be quite a cryptic warning that the `x`
+in the RHS of the pattern synonym declaration shadowed the top level `x`.
+
+```
+x :: ()
+x = ()
+
+pattern P x = Just x
+```
+
+See #12615 for some more examples.
+
 *********************************************************
 *                                                      *
         External entry points
@@ -293,9 +312,12 @@ rnPats ctxt pats thing_inside
           --    check incrementally for duplicates;
           -- Nor can we check incrementally for shadowing, else we'll
           --    complain *twice* about duplicates e.g. f (x,x) = ...
-        ; addErrCtxt doc_pat $
-          checkDupAndShadowedNames envs_before $
-          collectPatsBinders pats'
+          --
+          -- See note [Don't report shadowing for pattern synonyms]
+        ; unless (isPatSynCtxt ctxt)
+              (addErrCtxt doc_pat $
+                checkDupAndShadowedNames envs_before $
+                collectPatsBinders pats')
         ; thing_inside pats' } }
   where
     doc_pat = text "In" <+> pprMatchContext ctxt
diff --git a/testsuite/tests/patsyn/should_compile/T12615.hs b/testsuite/tests/patsyn/should_compile/T12615.hs
new file mode 100644
index 0000000..1405525
--- /dev/null
+++ b/testsuite/tests/patsyn/should_compile/T12615.hs
@@ -0,0 +1,12 @@
+{-# LANGUAGE NoImplicitPrelude, PatternSynonyms #-}
+{-# OPTIONS_GHC -Wall #-}
+module Test where
+
+x :: ()
+x = ()
+
+pattern Point2 :: () -> () -> ((), ())
+pattern Point2 x y = (x, y)
+
+pattern Point :: () -> () -> ((), ())
+pattern Point{x1, y1} = (x1, y1)
diff --git a/testsuite/tests/patsyn/should_compile/all.T b/testsuite/tests/patsyn/should_compile/all.T
index 1297d8c..d26fc84 100644
--- a/testsuite/tests/patsyn/should_compile/all.T
+++ b/testsuite/tests/patsyn/should_compile/all.T
@@ -59,4 +59,4 @@ test('T11977', normal, compile, [''])
 test('T12108', normal, compile, [''])
 test('T12484', normal, compile, [''])
 test('T11987', normal, multimod_compile, ['T11987', '-v0'])
-
+test('T12615', normal, compile, [''])



More information about the ghc-commits mailing list