From 5e456c9dadc2cda00dce958149f5e7fed9d6d5ab Mon Sep 17 00:00:00 2001 From: Mitchell Rosen Date: Wed, 28 Aug 2024 14:10:22 -0400 Subject: [PATCH 1/2] fix 5301 --- .../src/Unison/Syntax/TermParser.hs | 12 ++--- unison-src/transcripts/fix-5301.md | 24 +++++++++ unison-src/transcripts/fix-5301.output.md | 49 +++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 unison-src/transcripts/fix-5301.md create mode 100644 unison-src/transcripts/fix-5301.output.md diff --git a/parser-typechecker/src/Unison/Syntax/TermParser.hs b/parser-typechecker/src/Unison/Syntax/TermParser.hs index ec4bc42177..308eba2811 100644 --- a/parser-typechecker/src/Unison/Syntax/TermParser.hs +++ b/parser-typechecker/src/Unison/Syntax/TermParser.hs @@ -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) @@ -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 @@ -203,7 +203,7 @@ matchCase = do _ <- reserved "|" guard <- asum - [ Nothing <$ P.try (quasikeyword "otherwise"), + [ Nothing <$ quasikeyword "otherwise", Just <$> infixAppOrBooleanOp ] (_spanAnn, t) <- layoutBlock "->" @@ -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 @@ -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) [], []) diff --git a/unison-src/transcripts/fix-5301.md b/unison-src/transcripts/fix-5301.md new file mode 100644 index 0000000000..edffb6ad75 --- /dev/null +++ b/unison-src/transcripts/fix-5301.md @@ -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 +``` diff --git a/unison-src/transcripts/fix-5301.output.md b/unison-src/transcripts/fix-5301.output.md new file mode 100644 index 0000000000..4d2c8ad06a --- /dev/null +++ b/unison-src/transcripts/fix-5301.output.md @@ -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 + + +``` From 64a0ce0b2cd1d3292198aac343f811035505fa73 Mon Sep 17 00:00:00 2001 From: Mitchell Rosen Date: Thu, 29 Aug 2024 14:12:25 -0400 Subject: [PATCH 2/2] restore pattern match coverage test that accidentally wasn't testing the right thing before --- unison-src/transcripts/pattern-match-coverage.md | 2 +- .../transcripts/pattern-match-coverage.output.md | 16 +++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/unison-src/transcripts/pattern-match-coverage.md b/unison-src/transcripts/pattern-match-coverage.md index b4fcce8be8..6b0b248de3 100644 --- a/unison-src/transcripts/pattern-match-coverage.md +++ b/unison-src/transcripts/pattern-match-coverage.md @@ -465,7 +465,7 @@ result : '{e, Give T} r -> {e} r result f = handle !f with cases { x } -> x { give _ -> resume } -> result resume - { give A -> resume } -> result resume + { give T.A -> resume } -> result resume ``` ## Exhaustive ability reinterpretations are accepted diff --git a/unison-src/transcripts/pattern-match-coverage.output.md b/unison-src/transcripts/pattern-match-coverage.output.md index b77d720389..6647fb1a37 100644 --- a/unison-src/transcripts/pattern-match-coverage.output.md +++ b/unison-src/transcripts/pattern-match-coverage.output.md @@ -1056,26 +1056,16 @@ result : '{e, Give T} r -> {e} r result f = handle !f with cases { x } -> x { give _ -> resume } -> result resume - { give A -> resume } -> result resume + { give T.A -> resume } -> result resume ``` ``` ucm Loading changes detected in scratch.u. - - ❓ - - I couldn't resolve any of these symbols: - - 10 | { give A -> resume } -> result resume - + This case would be ignored because it's already covered by the preceding case(s): + 10 | { give T.A -> resume } -> result resume - Symbol Suggestions - - A SomeType.A - T.A - ``` ## Exhaustive ability reinterpretations are accepted