Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: require relative names in import{Wordy,Symboly}Id #5568

Merged
merged 7 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion parser-typechecker/src/Unison/Syntax/DeclParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ modifierP = do
where
unique = do
tok <- openBlockWith "unique"
optional (openBlockWith "[" *> importWordyId <* closeBlock) >>= \case
optional (openBlockWith "[" *> importRelativeWordyId <* closeBlock) >>= \case
Nothing -> pure (UnresolvedModifier'UniqueWithoutGuid <$ tok)
Just guid -> pure (UnresolvedModifier'UniqueWithGuid (Name.toText (L.payload guid)) <$ tok)
structural = do
Expand Down
6 changes: 3 additions & 3 deletions parser-typechecker/src/Unison/Syntax/FileParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ file = do
optional (reserved "namespace") >>= \case
Nothing -> pure Nothing
Just _ -> do
namespace <- importWordyId <|> importSymbolyId
namespace <- importRelativeWordyId <|> importRelativeSymbolyId
void (optional semi)
pure (Just namespace.payload)
let maybeNamespaceVar = Name.toVar <$> maybeNamespace
Expand Down Expand Up @@ -401,9 +401,9 @@ stanza = watchExpression <|> unexpectedAction <|> binding

watched :: (Monad m, Var v) => P v m (UF.WatchKind, Text, Ann)
watched = P.try do
kind <- (fmap . fmap . fmap) (Text.unpack . Name.toText) (optional importWordyId)
kind <- (fmap . fmap . fmap) (Text.unpack . Name.toText) (optional importRelativeWordyId)
guid <- uniqueName 10
op <- optional (L.payload <$> P.lookAhead importSymbolyId)
op <- optional (L.payload <$> P.lookAhead importRelativeSymbolyId)
guard (op == Just (Name.fromSegment NameSegment.watchSegment))
tok <- anyToken
guard $ maybe True (`L.touches` tok) kind
Expand Down
2 changes: 1 addition & 1 deletion parser-typechecker/src/Unison/Syntax/TermParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ importp = do
optional $
fmap Right importWordyId
<|> fmap Left importSymbolyId
suffixes <- optional (some (importWordyId <|> importSymbolyId))
suffixes <- optional (some (importRelativeWordyId <|> importRelativeSymbolyId))
case (prefix, suffixes) of
(Nothing, _) -> P.customFailure $ UseEmpty kw
(Just prefix@(Left _), _) -> P.customFailure $ UseInvalidPrefixSuffix prefix suffixes
Expand Down
128 changes: 128 additions & 0 deletions unison-src/transcripts/idempotent/fix-4536.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
``` ucm
scratch/main> builtins.merge lib.builtin

Done.
```

``` unison :error
foo : Nat
foo =
use Nat .+
1 + 2
```

``` ucm :added-by-ucm
Loading changes detected in scratch.u.

I got confused here:

3 | use Nat .+


I was surprised to find a '.' here.
I was expecting one of these instead:

* bang
* binding
* do
* false
* force
* handle
* if
* lambda
* let
* newline or semicolon
* pattern
* quote
* termLink
* true
* tuple
* typeLink
Comment on lines +17 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note, I'm not sure which one of these it wants.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I know, the error messages are atrocious :|

```

``` unison :error
namespace .foo
```

``` ucm :added-by-ucm
Loading changes detected in scratch.u.

I got confused here:

1 | namespace .foo


I was surprised to find a .foo here.
```

``` unison :error
unique[.foo] type Foo = Foo
```

``` ucm :added-by-ucm
Loading changes detected in scratch.u.

I got confused here:

1 | unique[.foo] type Foo = Foo


I was surprised to find a .foo here.
```

``` unison :error
.foo> 17
```

``` ucm :added-by-ucm
Loading changes detected in scratch.u.

This looks like the start of an expression here

1 | .foo> 17

but at the file top-level, I expect one of the following:

- A binding, like .foo = 42 OR
.foo : Nat
.foo = 42
- A watch expression, like > .foo + 1
- An `ability` declaration, like unique ability Foo where ...
- A `type` declaration, like structural type Optional a = None | Some a
```

``` unison :error
foo.> 17
```

``` ucm :added-by-ucm
Loading changes detected in scratch.u.

I got confused here:

1 | foo.> 17


I was surprised to find a foo.> here.
I was expecting one of these instead:

* ability
* bang
* binding
* do
* false
* force
* handle
* if
* lambda
* let
* namespace
* newline or semicolon
* quote
* termLink
* true
* tuple
* type
* typeLink
* use
```
22 changes: 1 addition & 21 deletions unison-src/transcripts/idempotent/generic-parse-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,7 @@ namespace.blah = 1
1 | namespace.blah = 1


I was surprised to find a = here.
I was expecting one of these instead:

* ability
* bang
* binding
* do
* false
* force
* handle
* if
* lambda
* let
* newline or semicolon
* quote
* termLink
* true
* tuple
* type
* typeLink
* use
I was surprised to find a .blah here.
```

``` unison :error
Expand Down
14 changes: 14 additions & 0 deletions unison-syntax/src/Unison/Syntax/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module Unison.Syntax.Parser
failureIf,
hqInfixId,
hqPrefixId,
importRelativeSymbolyId,
importRelativeWordyId,
importSymbolyId,
importWordyId,
label,
Expand Down Expand Up @@ -363,12 +365,24 @@ importWordyId = queryToken \case
L.WordyId (HQ'.NameOnly n) -> Just n
_ -> Nothing

-- | Parse a wordyId as a relative Name, rejecting any hash
importRelativeWordyId :: (Ord v) => P v m (L.Token Name)
importRelativeWordyId = queryToken \case
L.WordyId (HQ'.NameOnly n) | Name.isRelative n -> Just n
_ -> Nothing

-- | The `+` in: use Foo.bar + as a Name
importSymbolyId :: (Ord v) => P v m (L.Token Name)
importSymbolyId = queryToken \case
L.SymbolyId (HQ'.NameOnly n) -> Just n
_ -> Nothing

-- | The `+` in: use Foo.bar + as a relative Name
importRelativeSymbolyId :: (Ord v) => P v m (L.Token Name)
importRelativeSymbolyId = queryToken \case
L.SymbolyId (HQ'.NameOnly n) | Name.isRelative n -> Just n
_ -> Nothing

-- | Parse a symboly ID like >>= or &&, discarding any hash
symbolyDefinitionName :: (Var v) => P v m (L.Token v)
symbolyDefinitionName = queryToken $ \case
Expand Down
Loading