Skip to content

Commit

Permalink
fix 5301
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Aug 28, 2024
1 parent 6299fc3 commit 5e456c9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
12 changes: 6 additions & 6 deletions parser-typechecker/src/Unison/Syntax/TermParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ link :: (Monad m, Var v) => TermP v m
link = termLink <|> typeLink
where
typeLink = do
_ <- P.try (reserved "typeLink") -- type opens a block, gotta use something else
_ <- reserved "typeLink" -- type opens a block, gotta use something else
tok <- typeLink'
pure $ Term.typeLink (ann tok) (L.payload tok)
termLink = do
_ <- P.try (reserved "termLink")
_ <- reserved "termLink"
tok <- termLink'
pure $ Term.termLink (ann tok) (L.payload tok)

Expand All @@ -169,7 +169,7 @@ match = do
scrutinee <- term
_ <- optionalCloseBlock
_ <-
P.try (openBlockWith "with") <|> do
openBlockWith "with" <|> do
t <- anyToken
P.customFailure (ExpectedBlockOpen "with" t)
(_arities, cases) <- unzip <$> matchCases
Expand Down Expand Up @@ -203,7 +203,7 @@ matchCase = do
_ <- reserved "|"
guard <-
asum
[ Nothing <$ P.try (quasikeyword "otherwise"),
[ Nothing <$ quasikeyword "otherwise",
Just <$> infixAppOrBooleanOp
]
(_spanAnn, t) <- layoutBlock "->"
Expand Down Expand Up @@ -280,7 +280,7 @@ parsePattern = label "pattern" root
ctor :: CT.ConstructorType -> (L.Token (HQ.HashQualified Name) -> Set ConstructorReference -> Error v) -> P v m (L.Token ConstructorReference)
ctor ct err = do
-- this might be a var, so we avoid consuming it at first
tok <- P.try (P.lookAhead hqPrefixId)
tok <- P.lookAhead hqPrefixId
names <- asks names
-- probably should avoid looking up in `names` if `L.payload tok`
-- starts with a lowercase
Expand Down Expand Up @@ -329,7 +329,7 @@ parsePattern = label "pattern" root
pure (Pattern.setLoc inner (ann start <> ann end), vs)

-- ex: unique type Day = Mon | Tue | ...
nullaryCtor = P.try do
nullaryCtor = do
tok <- ctor CT.Data UnknownDataConstructor
pure (Pattern.Constructor (ann tok) (L.payload tok) [], [])

Expand Down
24 changes: 24 additions & 0 deletions unison-src/transcripts/fix-5301.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This transcripts demonstrates that pattern matching on a "constructor" (defined as a variable that begins with a capital
letter) that is either not found or ambiguouus fails. Previously, it would be treated as a variable binding.

```ucm
scratch/main> builtins.merge
```

```unison:error
type Foo = Bar Nat
foo : Foo -> Nat
foo = cases
Bar X -> 5
```

```unison:error
type Foo = Bar A
type A = X
type B = X
foo : Foo -> Nat
foo = cases
Bar X -> 5
```
49 changes: 49 additions & 0 deletions unison-src/transcripts/fix-5301.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
This transcripts demonstrates that pattern matching on a "constructor" (defined as a variable that begins with a capital
letter) that is either not found or ambiguouus fails. Previously, it would be treated as a variable binding.

``` ucm
scratch/main> builtins.merge
Done.
```
``` unison
type Foo = Bar Nat
foo : Foo -> Nat
foo = cases
Bar X -> 5
```

``` ucm
Loading changes detected in scratch.u.
I don't know about any data constructor named X. Maybe make
sure it's correctly spelled and that you've imported it:
5 | Bar X -> 5
```
``` unison
type Foo = Bar A
type A = X
type B = X
foo : Foo -> Nat
foo = cases
Bar X -> 5
```

``` ucm
Loading changes detected in scratch.u.
I don't know about any data constructor named X. Maybe make
sure it's correctly spelled and that you've imported it:
7 | Bar X -> 5
```

0 comments on commit 5e456c9

Please sign in to comment.