Commit 1851349a authored by Matthew Pickering's avatar Matthew Pickering Committed by Ben Gamari
Browse files

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
parent 2d6642bd
......@@ -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
......
......@@ -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
......
{-# 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)
......@@ -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, [''])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment