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

Better error messages for use of old restricted types #2764

Merged
merged 3 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions runtime/parser/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@
typeLeftDenotations[tokenType] = leftDenotation
}

func setTypeMetaLeftDenotation(tokenType lexer.TokenType, metaLeftDenotation typeMetaLeftDenotationFunc) {
current := typeMetaLeftDenotations[tokenType]
if current != nil {
panic(errors.NewUnexpectedError(
"type meta left denotation for token %s already exists",
tokenType,
))

Check warning on line 89 in runtime/parser/type.go

View check run for this annotation

Codecov / codecov/patch

runtime/parser/type.go#L86-L89

Added lines #L86 - L89 were not covered by tests
}
typeMetaLeftDenotations[tokenType] = metaLeftDenotation
}

type prefixTypeFunc func(parser *parser, right ast.Type, tokenRange ast.Range) ast.Type
type postfixTypeFunc func(parser *parser, left ast.Type, tokenRange ast.Range) ast.Type

Expand Down Expand Up @@ -467,6 +478,35 @@
}
},
)

// While restricted types have been removed from Cadence, during the first few months of the
// migration period, leave a special error in place to help developers

setTypeMetaLeftDenotation(
lexer.TokenBraceOpen,
func(p *parser, rightBindingPower int, left ast.Type) (result ast.Type, err error, done bool) {

// Perform a lookahead

current := p.current
cursor := p.tokens.Cursor()

// Skip the `{` token.
p.next()

// In case there is a space, the type is *not* considered a restricted type.
// The buffered tokens are replayed to allow them to be re-parsed.
if p.current.Is(lexer.TokenSpace) {
p.current = current
p.tokens.Revert(cursor)

return left, nil, true
}

// It was determined that a restricted type is parsed, so error
return nil, p.syntaxError("restricted types have been removed; replace with an intersection type"), true
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
},
)
}

func parseNominalType(
Expand Down
12 changes: 6 additions & 6 deletions runtime/parser/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ func TestParseIntersectionType(t *testing.T) {
utils.AssertEqualWithDiff(t,
[]error{
&SyntaxError{
Message: "unexpected token: '{'",
Pos: ast.Position{Offset: 1, Line: 1, Column: 1},
Message: "restricted types have been removed; replace with an intersection type",
Pos: ast.Position{Offset: 2, Line: 1, Column: 2},
},
},
errs,
Expand All @@ -532,8 +532,8 @@ func TestParseIntersectionType(t *testing.T) {
utils.AssertEqualWithDiff(t,
[]error{
&SyntaxError{
Message: "unexpected token: '{'",
Pos: ast.Position{Offset: 1, Line: 1, Column: 1},
Message: "restricted types have been removed; replace with an intersection type",
Pos: ast.Position{Offset: 2, Line: 1, Column: 2},
},
},
errs,
Expand All @@ -548,8 +548,8 @@ func TestParseIntersectionType(t *testing.T) {
utils.AssertEqualWithDiff(t,
[]error{
&SyntaxError{
Message: "unexpected token: '{'",
Pos: ast.Position{Offset: 1, Line: 1, Column: 1},
Message: "restricted types have been removed; replace with an intersection type",
Pos: ast.Position{Offset: 2, Line: 1, Column: 2},
},
},
errs,
Expand Down
Loading