Opened 2 months ago

Closed 2 months ago

#14114 closed bug (fixed)

Strange behavior when pattern variables are duplicated on pattern synonym RHS

Reported by: RyanGlScott Owned by:
Priority: normal Milestone: 8.4.1
Component: Compiler Version: 8.2.1
Keywords: PatternSynonyms Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: patsyn/should_fail/T14114
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D3866
Wiki Page:

Description

These might be two separate bugs, but they're similar enough that I'll bundle them under the same ticket.

If you take a pattern variable bound on the LHS of a unidirectional pattern synonym and use it multiple locations on the RHS, then GHC accepts it, surprisingly:

pattern Foo a <- (a,a)

What does Foo do? As it turns out, the right-most occurrence of a in the RHS seems to take precedence:

λ> :i Foo
pattern Foo :: b -> (a, b)      -- Defined at <interactive>:12:1
λ> case ('a', 'b') of Foo x -> x
'b'

I expect the definition of Foo to error.

When duplicate pattern variables are used in the RHS of an implicitly bidirectional pattern synonym, then it does error. However, the error message is quite misleading:

λ> pattern Foo a = (a,a)

<interactive>:16:17: error:
    Invalid right-hand side of bidirectional pattern synonym ‘Foo’:
      ‘a’ is not bound by the LHS of the pattern synonym
    RHS pattern: (a, a)

Change History (3)

comment:1 Changed 2 months ago by RyanGlScott

Differential Rev(s): Phab:D3866
Status: newpatch

comment:2 Changed 2 months ago by Ryan Scott <ryan.gl.scott@…>

In a89bb80/ghc:

Fix #14114 by checking for duplicate vars on pattern synonym RHSes

Summary:
Because we weren't checking for duplicate variables on the right-hand
sides of pattern synonyms, bogus definitions like this one passed the renamer:

```lang=haskell
pattern Foo a <- (a,a)
```

Luckily, the fix is simple.

Test Plan: make test TEST=T14114

Reviewers: mpickering, austin, bgamari, simonpj

Reviewed By: simonpj

Subscribers: simonpj, rwbarton, thomie

GHC Trac Issues: #14114

Differential Revision: https://phabricator.haskell.org/D3866

comment:3 Changed 2 months ago by RyanGlScott

Milestone: 8.4.1
Resolution: fixed
Status: patchclosed
Test Case: patsyn/should_fail/T14114
Note: See TracTickets for help on using tickets.