Skip to content

Commit

Permalink
Merge pull request #2356 from onflow/bastian/improve-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Feb 28, 2023
2 parents 0ec9b81 + b5f57e9 commit 60a7a95
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.1.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
Expand Down
1 change: 1 addition & 0 deletions runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ type DereferenceError struct {
}

var _ errors.UserError = DereferenceError{}
var _ errors.SecondaryError = DereferenceError{}

func (DereferenceError) IsUserError() {}

Expand Down
1 change: 1 addition & 0 deletions runtime/parser/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type InvalidIntegerLiteralError struct {

var _ ParseError = &InvalidIntegerLiteralError{}
var _ errors.UserError = &InvalidIntegerLiteralError{}
var _ errors.SecondaryError = &InvalidIntegerLiteralError{}

func (*InvalidIntegerLiteralError) isParseError() {}

Expand Down
42 changes: 33 additions & 9 deletions runtime/sema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package sema
import (
"fmt"
"math/big"
"sort"
"strings"

"github.com/texttheater/golang-levenshtein/levenshtein"
"golang.org/x/exp/maps"

"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
Expand Down Expand Up @@ -252,6 +252,7 @@ type AssignmentToConstantError struct {

var _ SemanticError = &AssignmentToConstantError{}
var _ errors.UserError = &AssignmentToConstantError{}
var _ errors.SecondaryError = &AssignmentToConstantError{}

func (*AssignmentToConstantError) isSemanticError() {}

Expand Down Expand Up @@ -541,8 +542,8 @@ type InvalidBinaryOperandError struct {
}

var _ SemanticError = &InvalidBinaryOperandError{}
var _ errors.SecondaryError = &InvalidBinaryOperandError{}
var _ errors.UserError = &InvalidBinaryOperandError{}
var _ errors.SecondaryError = &InvalidBinaryOperandError{}

func (*InvalidBinaryOperandError) isSemanticError() {}

Expand Down Expand Up @@ -621,8 +622,9 @@ type ControlStatementError struct {
ast.Range
}

var _ errors.UserError = &ControlStatementError{}
var _ SemanticError = &ControlStatementError{}
var _ errors.UserError = &ControlStatementError{}
var _ errors.SecondaryError = &ControlStatementError{}

func (*ControlStatementError) isSemanticError() {}

Expand Down Expand Up @@ -948,13 +950,27 @@ func (e *NotDeclaredMemberError) findClosestMember() (closestMember string) {
return
}

nameRunes := []rune(e.Name)

closestDistance := len(e.Name)
for _, member := range maps.Keys(e.Type.GetMembers()) {
distance := levenshtein.DistanceForStrings([]rune(e.Name), []rune(member), levenshtein.DefaultOptions)
// don't update the closest member if the distance is greater than one already found, or if the edits
// required would involve a complete replacement of the member's text
if distance < closestDistance && distance < len(member) {
closestMember = member

var sortedMemberNames []string
for memberName := range e.Type.GetMembers() { //nolint:maprange
sortedMemberNames = append(sortedMemberNames, memberName)
}
sort.Strings(sortedMemberNames)

for _, memberName := range sortedMemberNames {
distance := levenshtein.DistanceForStrings(
nameRunes,
[]rune(memberName),
levenshtein.DefaultOptions,
)

// Don't update the closest member if the distance is greater than one already found,
// or if the edits required would involve a complete replacement of the member's text
if distance < closestDistance && distance < len(memberName) {
closestMember = memberName
closestDistance = distance
}
}
Expand Down Expand Up @@ -1055,6 +1071,7 @@ type FieldTypeNotStorableError struct {

var _ SemanticError = &FieldTypeNotStorableError{}
var _ errors.UserError = &FieldTypeNotStorableError{}
var _ errors.SecondaryError = &FieldTypeNotStorableError{}

func (*FieldTypeNotStorableError) isSemanticError() {}

Expand Down Expand Up @@ -1187,6 +1204,7 @@ type InvalidEnumRawTypeError struct {

var _ SemanticError = &InvalidEnumRawTypeError{}
var _ errors.UserError = &InvalidEnumRawTypeError{}
var _ errors.SecondaryError = &InvalidEnumRawTypeError{}

func (*InvalidEnumRawTypeError) isSemanticError() {}

Expand Down Expand Up @@ -1272,6 +1290,7 @@ type ConformanceError struct {

var _ SemanticError = &ConformanceError{}
var _ errors.UserError = &ConformanceError{}
var _ errors.SecondaryError = &ConformanceError{}

func (*ConformanceError) isSemanticError() {}

Expand Down Expand Up @@ -2328,6 +2347,7 @@ type InvalidResourceAssignmentError struct {

var _ SemanticError = &InvalidResourceAssignmentError{}
var _ errors.UserError = &InvalidResourceAssignmentError{}
var _ errors.SecondaryError = &InvalidResourceAssignmentError{}

func (*InvalidResourceAssignmentError) isSemanticError() {}

Expand Down Expand Up @@ -2486,6 +2506,7 @@ type UnreachableStatementError struct {

var _ SemanticError = &UnreachableStatementError{}
var _ errors.UserError = &UnreachableStatementError{}
var _ errors.SecondaryError = &UnreachableStatementError{}

func (*UnreachableStatementError) isSemanticError() {}

Expand Down Expand Up @@ -3492,6 +3513,7 @@ type InvalidPathIdentifierError struct {

var _ SemanticError = &InvalidPathDomainError{}
var _ errors.UserError = &InvalidPathDomainError{}
var _ errors.SecondaryError = &InvalidPathDomainError{}

func (*InvalidPathDomainError) isSemanticError() {}

Expand Down Expand Up @@ -3654,6 +3676,7 @@ type TypeParameterTypeMismatchError struct {

var _ SemanticError = &TypeParameterTypeMismatchError{}
var _ errors.UserError = &TypeParameterTypeMismatchError{}
var _ errors.SecondaryError = &TypeParameterTypeMismatchError{}

func (*TypeParameterTypeMismatchError) isSemanticError() {}

Expand Down Expand Up @@ -3837,6 +3860,7 @@ type ExternalMutationError struct {

var _ SemanticError = &ExternalMutationError{}
var _ errors.UserError = &ExternalMutationError{}
var _ errors.SecondaryError = &ExternalMutationError{}

func (*ExternalMutationError) isSemanticError() {}

Expand Down

0 comments on commit 60a7a95

Please sign in to comment.