Skip to content

Commit

Permalink
refactor interpreter errors to non-pointer values
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Oct 6, 2020
1 parent f210c78 commit 2a28498
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 140 deletions.
42 changes: 16 additions & 26 deletions runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ type NotDeclaredError struct {
Name string
}

func (e *NotDeclaredError) Error() string {
func (e NotDeclaredError) Error() string {
return fmt.Sprintf(
"cannot find %s in this scope: `%s`",
e.ExpectedKind.Name(),
e.Name,
)
}

func (e *NotDeclaredError) SecondaryError() string {
func (e NotDeclaredError) SecondaryError() string {
return "not found in this scope"
}

Expand All @@ -79,7 +79,7 @@ type NotInvokableError struct {
Value Value
}

func (e *NotInvokableError) Error() string {
func (e NotInvokableError) Error() string {
return fmt.Sprintf("cannot call value: %#+v", e.Value)
}

Expand All @@ -90,31 +90,21 @@ type ArgumentCountError struct {
ArgumentCount int
}

func (e *ArgumentCountError) Error() string {
func (e ArgumentCountError) Error() string {
return fmt.Sprintf(
"incorrect number of arguments: expected %d, got %d",
e.ParameterCount,
e.ArgumentCount,
)
}

// InvalidParameterTypeInInvocationError

type InvalidParameterTypeInInvocationError struct {
InvalidParameterType sema.Type
}

func (e *InvalidParameterTypeInInvocationError) Error() string {
return fmt.Sprintf("cannot invoke functions with parameter type: `%s`", e.InvalidParameterType)
}

// TransactionNotDeclaredError

type TransactionNotDeclaredError struct {
Index int
}

func (e *TransactionNotDeclaredError) Error() string {
func (e TransactionNotDeclaredError) Error() string {
return fmt.Sprintf(
"cannot find transaction with index %d in this scope",
e.Index,
Expand All @@ -129,7 +119,7 @@ type ConditionError struct {
LocationRange
}

func (e *ConditionError) Error() string {
func (e ConditionError) Error() string {
if e.Message == "" {
return fmt.Sprintf("%s failed", e.ConditionKind.Name())
}
Expand All @@ -142,7 +132,7 @@ type RedeclarationError struct {
Name string
}

func (e *RedeclarationError) Error() string {
func (e RedeclarationError) Error() string {
return fmt.Sprintf("cannot redeclare: `%s` is already declared", e.Name)
}

Expand All @@ -152,7 +142,7 @@ type DereferenceError struct {
LocationRange
}

func (e *DereferenceError) Error() string {
func (e DereferenceError) Error() string {
return "dereference failed"
}

Expand Down Expand Up @@ -187,7 +177,7 @@ type DestroyedCompositeError struct {
LocationRange
}

func (e *DestroyedCompositeError) Error() string {
func (e DestroyedCompositeError) Error() string {
return fmt.Sprintf("%s is destroyed and cannot be accessed anymore", e.CompositeKind.Name())
}

Expand All @@ -197,7 +187,7 @@ type ForceAssignmentToNonNilResourceError struct {
LocationRange
}

func (e *ForceAssignmentToNonNilResourceError) Error() string {
func (e ForceAssignmentToNonNilResourceError) Error() string {
return "force assignment to non-nil resource-typed value"
}

Expand All @@ -207,7 +197,7 @@ type ForceNilError struct {
LocationRange
}

func (e *ForceNilError) Error() string {
func (e ForceNilError) Error() string {
return "unexpectedly found nil while forcing an Optional value"
}

Expand All @@ -218,7 +208,7 @@ type TypeMismatchError struct {
LocationRange
}

func (e *TypeMismatchError) Error() string {
func (e TypeMismatchError) Error() string {
return fmt.Sprintf(
"unexpectedly found non-`%s` while force-casting value",
e.ExpectedType.QualifiedString(),
Expand All @@ -233,11 +223,11 @@ type InvalidPathDomainError struct {
LocationRange
}

func (e *InvalidPathDomainError) Error() string {
func (e InvalidPathDomainError) Error() string {
return "invalid path domain"
}

func (e *InvalidPathDomainError) SecondaryError() string {
func (e InvalidPathDomainError) SecondaryError() string {

domainNames := make([]string, len(e.ExpectedDomains))

Expand All @@ -260,7 +250,7 @@ type OverwriteError struct {
LocationRange
}

func (e *OverwriteError) Error() string {
func (e OverwriteError) Error() string {
return fmt.Sprintf(
"failed to save object: path %s in account %s already stores an object",
e.Path,
Expand All @@ -276,7 +266,7 @@ type CyclicLinkError struct {
LocationRange
}

func (e *CyclicLinkError) Error() string {
func (e CyclicLinkError) Error() string {
var builder strings.Builder
for i, path := range e.Paths {
if i > 0 {
Expand Down
28 changes: 14 additions & 14 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func (interpreter *Interpreter) prepareInvokeVariable(
// function must be defined as a global variable
variable, ok := interpreter.Globals[functionName]
if !ok {
return nil, &NotDeclaredError{
return nil, NotDeclaredError{
ExpectedKind: common.DeclarationKindFunction,
Name: functionName,
}
Expand All @@ -730,7 +730,7 @@ func (interpreter *Interpreter) prepareInvokeVariable(
// the global variable must be declared as a function
functionValue, ok := variableValue.(FunctionValue)
if !ok {
return nil, &NotInvokableError{
return nil, NotInvokableError{
Value: variableValue,
}
}
Expand All @@ -741,7 +741,7 @@ func (interpreter *Interpreter) prepareInvokeVariable(
invokableType, ok := ty.(sema.InvokableType)

if !ok {
return nil, &NotInvokableError{
return nil, NotInvokableError{
Value: variableValue,
}
}
Expand All @@ -756,7 +756,7 @@ func (interpreter *Interpreter) prepareInvokeTransaction(
arguments []Value,
) (trampoline Trampoline, err error) {
if index >= len(interpreter.Transactions) {
return nil, &TransactionNotDeclaredError{Index: index}
return nil, TransactionNotDeclaredError{Index: index}
}

functionValue := interpreter.Transactions[index]
Expand Down Expand Up @@ -787,7 +787,7 @@ func (interpreter *Interpreter) prepareInvoke(
if functionType.RequiredArgumentCount == nil ||
argumentCount < *functionType.RequiredArgumentCount {

return nil, &ArgumentCountError{
return nil, ArgumentCountError{
ParameterCount: parameterCount,
ArgumentCount: argumentCount,
}
Expand All @@ -800,7 +800,7 @@ func (interpreter *Interpreter) prepareInvoke(
// TODO: value type is not known, reject for now
switch parameterType.(type) {
case *sema.AnyStructType, *sema.AnyResourceType:
return nil, &NotInvokableError{
return nil, NotInvokableError{
Value: functionValue,
}
}
Expand Down Expand Up @@ -936,7 +936,7 @@ func (interpreter *Interpreter) functionDeclarationValue(
// NOTE: consider using NewInterpreter if the value should be predefined in all programs
func (interpreter *Interpreter) ImportValue(name string, value Value) error {
if _, ok := interpreter.Globals[name]; ok {
return &RedeclarationError{
return RedeclarationError{
Name: name,
}
}
Expand Down Expand Up @@ -1059,7 +1059,7 @@ func (interpreter *Interpreter) visitConditions(conditions []*ast.Condition) Tra
Then(func(result interface{}) {
message := result.(*StringValue).Str

panic(&ConditionError{
panic(ConditionError{
ConditionKind: condition.Kind,
Message: message,
LocationRange: interpreter.locationRange(condition.Test),
Expand Down Expand Up @@ -1355,7 +1355,7 @@ func (interpreter *Interpreter) visitAssignment(
if _, ok := target.(NilValue); !ok {
locationRange := interpreter.locationRange(position)

panic(&ForceAssignmentToNonNilResourceError{
panic(ForceAssignmentToNonNilResourceError{
LocationRange: locationRange,
})
}
Expand Down Expand Up @@ -3329,7 +3329,7 @@ func (interpreter *Interpreter) VisitCastingExpression(expression *ast.CastingEx
case ast.OperationForceCast:
if !isSubType {
panic(
&TypeMismatchError{
TypeMismatchError{
ExpectedType: expectedType,
LocationRange: interpreter.locationRange(expression.Expression),
},
Expand Down Expand Up @@ -3390,7 +3390,7 @@ func (interpreter *Interpreter) VisitForceExpression(expression *ast.ForceExpres

case NilValue:
panic(
&ForceNilError{
ForceNilError{
LocationRange: interpreter.locationRange(expression.Expression),
},
)
Expand Down Expand Up @@ -3717,7 +3717,7 @@ func mustPathDomain(
}

panic(
&InvalidPathDomainError{
InvalidPathDomainError{
ActualDomain: path.Domain,
ExpectedDomains: expectedDomains,
LocationRange: locationRange,
Expand Down Expand Up @@ -3758,7 +3758,7 @@ func (interpreter *Interpreter) authAccountSaveFunction(addressValue AddressValu

if interpreter.storedValueExists(address, key) {
panic(
&OverwriteError{
OverwriteError{
Address: addressValue,
Path: path,
LocationRange: invocation.LocationRange,
Expand Down Expand Up @@ -4143,7 +4143,7 @@ func (interpreter *Interpreter) getCapabilityFinalTargetStorageKey(
// Detect cyclic links

if _, ok := seenKeys[key]; ok {
panic(&CyclicLinkError{
panic(CyclicLinkError{
Address: addressValue,
Paths: paths,
LocationRange: locationRange,
Expand Down
22 changes: 11 additions & 11 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -5294,7 +5294,7 @@ func (v *CompositeValue) Copy() Value {

func (v *CompositeValue) checkStatus(locationRange LocationRange) {
if v.destroyed {
panic(&DestroyedCompositeError{
panic(DestroyedCompositeError{
CompositeKind: v.Kind,
LocationRange: locationRange,
})
Expand Down Expand Up @@ -6026,7 +6026,7 @@ func (v *StorageReferenceValue) String() string {
func (v *StorageReferenceValue) DynamicType(interpreter *Interpreter) DynamicType {
referencedValue := v.referencedValue(interpreter)
if referencedValue == nil {
panic(&DereferenceError{})
panic(DereferenceError{})
}

innerType := (*referencedValue).DynamicType(interpreter)
Expand Down Expand Up @@ -6076,7 +6076,7 @@ func (v *StorageReferenceValue) referencedValue(interpreter *Interpreter) *Value
func (v *StorageReferenceValue) GetMember(interpreter *Interpreter, locationRange LocationRange, name string) Value {
referencedValue := v.referencedValue(interpreter)
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand All @@ -6087,7 +6087,7 @@ func (v *StorageReferenceValue) GetMember(interpreter *Interpreter, locationRang
func (v *StorageReferenceValue) SetMember(interpreter *Interpreter, locationRange LocationRange, name string, value Value) {
referencedValue := v.referencedValue(interpreter)
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand All @@ -6098,7 +6098,7 @@ func (v *StorageReferenceValue) SetMember(interpreter *Interpreter, locationRang
func (v *StorageReferenceValue) Get(interpreter *Interpreter, locationRange LocationRange, key Value) Value {
referencedValue := v.referencedValue(interpreter)
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand All @@ -6110,7 +6110,7 @@ func (v *StorageReferenceValue) Get(interpreter *Interpreter, locationRange Loca
func (v *StorageReferenceValue) Set(interpreter *Interpreter, locationRange LocationRange, key Value, value Value) {
referencedValue := v.referencedValue(interpreter)
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand Down Expand Up @@ -6146,7 +6146,7 @@ func (v *EphemeralReferenceValue) String() string {
func (v *EphemeralReferenceValue) DynamicType(interpreter *Interpreter) DynamicType {
referencedValue := v.referencedValue()
if referencedValue == nil {
panic(&DereferenceError{})
panic(DereferenceError{})
}

innerType := (*referencedValue).DynamicType(interpreter)
Expand Down Expand Up @@ -6195,7 +6195,7 @@ func (v *EphemeralReferenceValue) referencedValue() *Value {
func (v *EphemeralReferenceValue) GetMember(interpreter *Interpreter, locationRange LocationRange, name string) Value {
referencedValue := v.referencedValue()
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand All @@ -6206,7 +6206,7 @@ func (v *EphemeralReferenceValue) GetMember(interpreter *Interpreter, locationRa
func (v *EphemeralReferenceValue) SetMember(interpreter *Interpreter, locationRange LocationRange, name string, value Value) {
referencedValue := v.referencedValue()
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand All @@ -6217,7 +6217,7 @@ func (v *EphemeralReferenceValue) SetMember(interpreter *Interpreter, locationRa
func (v *EphemeralReferenceValue) Get(interpreter *Interpreter, locationRange LocationRange, key Value) Value {
referencedValue := v.referencedValue()
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand All @@ -6229,7 +6229,7 @@ func (v *EphemeralReferenceValue) Get(interpreter *Interpreter, locationRange Lo
func (v *EphemeralReferenceValue) Set(interpreter *Interpreter, locationRange LocationRange, key Value, value Value) {
referencedValue := v.referencedValue()
if referencedValue == nil {
panic(&DereferenceError{
panic(DereferenceError{
LocationRange: locationRange,
})
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3185,7 +3185,7 @@ func TestRuntimeInvokeStoredInterfaceFunction(t *testing.T) {
} else {
require.Error(t, err)
require.IsType(t, Error{}, err)
assert.IsType(t, &interpreter.ConditionError{}, err.(Error).Err)
assert.IsType(t, interpreter.ConditionError{}, err.(Error).Err)
}
})
}
Expand Down
Loading

0 comments on commit 2a28498

Please sign in to comment.