[commit: ghc] ghc-8.0: Don't warn about name shadowing when renaming the patten in a PatSyn decl (0c2b766)

git at git.haskell.org git at git.haskell.org
Sun Oct 2 01:04:28 UTC 2016


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

On branch  : ghc-8.0
Link       : http://ghc.haskell.org/trac/ghc/changeset/0c2b766d5961562cc8e8603d12399db279ac7e51/ghc

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

commit 0c2b766d5961562cc8e8603d12399db279ac7e51
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
    
    (cherry picked from commit 1851349acd9e73f1c18d68f70d5cf7b46a843cb5)


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

0c2b766d5961562cc8e8603d12399db279ac7e51
 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     |  1 +
 4 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/compiler/hsSyn/HsExpr.hs b/compiler/hsSyn/HsExpr.hs
index ee64c0c..6b9774b 100644
--- a/compiler/hsSyn/HsExpr.hs
+++ b/compiler/hsSyn/HsExpr.hs
@@ -2208,6 +2208,12 @@ data HsMatchContext id  -- Context of a Match
   | PatSyn                      -- A pattern synonym declaration
   deriving (Data, Typeable)
 
+isPatSynCtxt :: HsMatchContext id -> Bool
+isPatSynCtxt ctxt =
+  case ctxt of
+    PatSyn -> True
+    _      -> False
+
 data HsStmtContext id
   = ListComp
   | MonadComp
diff --git a/compiler/rename/RnPat.hs b/compiler/rename/RnPat.hs
index 39ecdb0..bda186b 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
 
 {-
@@ -249,6 +249,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
@@ -294,9 +313,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 9bb4f59..8afa2b6 100644
--- a/testsuite/tests/patsyn/should_compile/all.T
+++ b/testsuite/tests/patsyn/should_compile/all.T
@@ -55,3 +55,4 @@ test('T12094', normal, compile, [''])
 test('T12484', normal, compile, [''])
 test('T12489', normal, compile, [''])
 test('T11959', expect_broken(11959), multimod_compile, ['T11959', '-v0'])
+test('T12615', normal, compile, [''])



More information about the ghc-commits mailing list