Skip to content

Commit 5e428a7

Browse files
authored
Merge pull request #4247 from kylegoetz/4233/declaration-name-hash-bug
Term declarations should not allow hashes in the term name (typesig or term definition)
2 parents 39c3bd5 + 5258b6c commit 5e428a7

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

CONTRIBUTORS.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ The format for this list: name, GitHub handle
7676
* Mario Bašić (@mabasic)
7777
* Chris Krycho (@chriskrycho)
7878
* Hatim Khambati (@hatimkhambati26)
79+
* Kyle Goetz (@kylegoetz)

parser-typechecker/src/Unison/Syntax/TermParser.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ infixAppOrBooleanOp = chainl1 term4 (or <|> and <|> infixApp)
985985
typedecl :: (Monad m, Var v) => P v m (L.Token v, Type v Ann)
986986
typedecl =
987987
(,)
988-
<$> P.try (prefixDefinitionName <* reserved ":")
988+
<$> P.try (prefixTermName <* reserved ":")
989989
<*> TypeParser.valueType
990990
<* semi
991991

@@ -1053,8 +1053,8 @@ binding = label "binding" do
10531053
arg2 <- prefixDefinitionName
10541054
pure (ann arg1, op, [arg1, arg2])
10551055
let prefixLhs = do
1056-
v <- prefixDefinitionName
1057-
vs <- many prefixDefinitionName
1056+
v <- prefixTermName
1057+
vs <- many prefixTermName
10581058
pure (ann v, v, vs)
10591059
let lhs :: P v m (Ann, L.Token v, [L.Token v])
10601060
lhs = infixLhs <|> prefixLhs
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# No Hashes in Term Declarations
2+
3+
There should not be hashes in the names used in term declarations, either in the type signature or the type definition.
4+
5+
```unison:hide:all:error
6+
x##Nat : Int -> Int -> Boolean
7+
x##Nat = 5
8+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# No Hashes in Term Declarations
2+
3+
There should not be hashes in the names used in term declarations, either in the type signature or the type definition.
4+

unison-syntax/src/Unison/Syntax/Parser.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Unison.Syntax.Parser
3131
peekAny,
3232
positionalVar,
3333
prefixDefinitionName,
34+
prefixTermName,
3435
queryToken,
3536
reserved,
3637
root,
@@ -297,6 +298,19 @@ prefixDefinitionName :: (Var v) => P v m (L.Token v)
297298
prefixDefinitionName =
298299
wordyDefinitionName <|> parenthesize symbolyDefinitionName
299300

301+
-- Parse a prefix identifier e.g. Foo or (+), rejecting any hash
302+
-- This is useful for term declarations, where type signatures and term names should not have hashes.
303+
prefixTermName :: (Var v) => P v m (L.Token v)
304+
prefixTermName = wordyTermName <|> parenthesize symbolyTermName
305+
where
306+
wordyTermName = queryToken $ \case
307+
L.WordyId s Nothing -> Just $ Var.nameds s
308+
L.Blank s -> Just $ Var.nameds ("_" <> s)
309+
_ -> Nothing
310+
symbolyTermName = queryToken $ \case
311+
L.SymbolyId s Nothing -> Just $ Var.nameds s
312+
_ -> Nothing
313+
300314
-- Parse a wordy identifier e.g. Foo, discarding any hash
301315
wordyDefinitionName :: (Var v) => P v m (L.Token v)
302316
wordyDefinitionName = queryToken $ \case

0 commit comments

Comments
 (0)