Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/onflow/cadence into supun…
Browse files Browse the repository at this point in the history
…/port-223
  • Loading branch information
SupunS committed May 15, 2024
2 parents f072d74 + b36c399 commit 7584510
Show file tree
Hide file tree
Showing 50 changed files with 727 additions and 284 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/kr/pretty v0.3.1
github.com/leanovate/gopter v0.2.9
github.com/logrusorgru/aurora/v4 v4.0.0
github.com/onflow/atree v0.6.1-0.20240429171449-cb486ceb1f9c
github.com/onflow/atree v0.7.0-rc.2
github.com/rivo/uniseg v0.4.4
github.com/schollz/progressbar/v3 v3.13.1
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvr
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/onflow/atree v0.6.1-0.20240429171449-cb486ceb1f9c h1:Ol+qFATYiS7LfwQQKBjfLJ8z6VwzZehVrYH1JI2ssUU=
github.com/onflow/atree v0.6.1-0.20240429171449-cb486ceb1f9c/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM=
github.com/onflow/atree v0.7.0-rc.2 h1:mZmVrl/zPlfI44EjV3FdR2QwIqT8nz1sCONUBFcML/U=
github.com/onflow/atree v0.7.0-rc.2/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM=
github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down
2 changes: 1 addition & 1 deletion migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (m *StorageMigration) WithErrorStacktrace(stacktraceEnabled bool) *StorageM
}

func (m *StorageMigration) Commit() error {
return m.storage.Commit(m.interpreter, false)
return m.storage.NondeterministicCommit(m.interpreter, false)
}

func (m *StorageMigration) Migrate(migrator StorageMapKeyMigrator) {
Expand Down
5 changes: 4 additions & 1 deletion runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,10 @@ func (declarationInterpreter *Interpreter) declareNonEnumCompositeValue(

var initializerFunction FunctionValue
if declaration.Kind() == common.CompositeKindEvent {
// Initializer is a static function.
// Initializer could ideally be a bound function.
// However, since it is created and being called here itself, and
// because it is never passed around, it is OK to just create as static function
// without the bound-function wrapper.
initializerFunction = NewStaticHostFunctionValue(
declarationInterpreter,
initializerType,
Expand Down
12 changes: 6 additions & 6 deletions runtime/interpreter/value_account_accountcapabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (

// Account.AccountCapabilities

var Account_AccountCapabilitiesTypeID = sema.Account_AccountCapabilitiesType.ID()
var Account_AccountCapabilitiesStaticType StaticType = PrimitiveStaticTypeAccount_AccountCapabilities // unmetered
var Account_AccountCapabilitiesFieldNames []string = nil
var account_AccountCapabilitiesTypeID = sema.Account_AccountCapabilitiesType.ID()
var account_AccountCapabilitiesStaticType StaticType = PrimitiveStaticTypeAccount_AccountCapabilities // unmetered
var account_AccountCapabilitiesFieldNames []string = nil

func NewAccountAccountCapabilitiesValue(
gauge common.MemoryGauge,
Expand All @@ -53,9 +53,9 @@ func NewAccountAccountCapabilitiesValue(

accountCapabilities := NewSimpleCompositeValue(
gauge,
Account_AccountCapabilitiesTypeID,
Account_AccountCapabilitiesStaticType,
Account_AccountCapabilitiesFieldNames,
account_AccountCapabilitiesTypeID,
account_AccountCapabilitiesStaticType,
account_AccountCapabilitiesFieldNames,
nil,
nil,
nil,
Expand Down
2 changes: 1 addition & 1 deletion runtime/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (r *REPL) Accept(code []byte, eval bool) (inputIsComplete bool, err error)
var expressionType sema.Type
expressionStatement, isExpression := statement.(*ast.ExpressionStatement)
if isExpression {
expressionType = r.checker.VisitExpression(expressionStatement.Expression, nil)
expressionType = r.checker.VisitExpression(expressionStatement.Expression, expressionStatement, nil)
if !eval && expressionType != sema.InvalidType {
r.onExpressionType(expressionType)
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/resource_duplicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/onflow/cadence/encoding/json"
. "github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"
. "github.com/onflow/cadence/runtime/tests/runtime_utils"
. "github.com/onflow/cadence/runtime/tests/utils"
)
Expand Down Expand Up @@ -204,6 +204,6 @@ func TestRuntimeResourceDuplicationWithContractTransfer(t *testing.T) {
)
RequireError(t, err)

var nonTransferableValueError interpreter.NonTransferableValueError
require.ErrorAs(t, err, &nonTransferableValueError)
var invalidMoveError *sema.InvalidMoveError
require.ErrorAs(t, err, &invalidMoveError)
}
117 changes: 0 additions & 117 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10634,123 +10634,6 @@ func TestRuntimeNonPublicAccessModifierInInterface(t *testing.T) {
require.Len(t, conformanceErr.MemberMismatches, 2)
}

func TestRuntimeMoveSelfVariable(t *testing.T) {

t.Parallel()

t.Run("contract", func(t *testing.T) {
t.Parallel()

contract := []byte(`
access(all) contract Foo {
access(all) fun moveSelf() {
var x = self!
}
}
`)

runtime := NewTestInterpreterRuntimeWithConfig(Config{
AtreeValidationEnabled: false,
})

address := common.MustBytesToAddress([]byte{0x1})

var contractCode []byte

runtimeInterface := &TestRuntimeInterface{
Storage: NewTestLedger(nil, nil),
OnGetSigningAccounts: func() ([]Address, error) {
return []Address{address}, nil
},
OnGetAccountContractCode: func(location common.AddressLocation) ([]byte, error) {
return contractCode, nil
},
OnResolveLocation: NewSingleIdentifierLocationResolver(t),
OnUpdateAccountContractCode: func(_ common.AddressLocation, code []byte) error {
contractCode = code
return nil
},
OnEmitEvent: func(event cadence.Event) error {
return nil
},
}

nextTransactionLocation := NewTransactionLocationGenerator()

// Deploy

deploymentTx := DeploymentTransaction("Foo", contract)
err := runtime.ExecuteTransaction(
Script{
Source: deploymentTx,
},
Context{
Interface: runtimeInterface,
Location: nextTransactionLocation(),
},
)
require.NoError(t, err)

// Execute script

nextScriptLocation := NewScriptLocationGenerator()

script := []byte(fmt.Sprintf(`
import Foo from %[1]s
access(all) fun main(): Void {
Foo.moveSelf()
}`,
address.HexWithPrefix(),
))

_, err = runtime.ExecuteScript(
Script{
Source: script,
},
Context{
Interface: runtimeInterface,
Location: nextScriptLocation(),
},
)

RequireError(t, err)
require.ErrorAs(t, err, &interpreter.NonTransferableValueError{})
})

t.Run("transaction", func(t *testing.T) {
t.Parallel()

script := []byte(`
transaction {
prepare() {
var x = true ? self : self
}
execute {}
}
`)

runtime := NewTestInterpreterRuntime()
runtimeInterface := &TestRuntimeInterface{}

nextTransactionLocation := NewTransactionLocationGenerator()

err := runtime.ExecuteTransaction(
Script{
Source: script,
},
Context{
Interface: runtimeInterface,
Location: nextTransactionLocation(),
},
)

RequireError(t, err)
require.ErrorAs(t, err, &interpreter.NonTransferableValueError{})
})
}

func TestRuntimeContractWithInvalidCapability(t *testing.T) {

t.Parallel()
Expand Down
17 changes: 8 additions & 9 deletions runtime/sema/check_array_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package sema

import "github.com/onflow/cadence/runtime/ast"

func (checker *Checker) VisitArrayExpression(expression *ast.ArrayExpression) Type {
func (checker *Checker) VisitArrayExpression(arrayExpression *ast.ArrayExpression) Type {

// visit all elements, ensure they are all the same type

Expand All @@ -29,7 +29,7 @@ func (checker *Checker) VisitArrayExpression(expression *ast.ArrayExpression) Ty
var elementType Type
var resultType ArrayType

elementCount := len(expression.Values)
elementCount := len(arrayExpression.Values)

switch typ := expectedType.(type) {

Expand All @@ -43,7 +43,7 @@ func (checker *Checker) VisitArrayExpression(expression *ast.ArrayExpression) Ty
&ConstantSizedArrayLiteralSizeError{
ExpectedSize: typ.Size,
ActualSize: literalCount,
Range: expression.Range,
Range: arrayExpression.Range,
},
)
}
Expand All @@ -68,13 +68,12 @@ func (checker *Checker) VisitArrayExpression(expression *ast.ArrayExpression) Ty
if elementCount > 0 {
argumentTypes = make([]Type, elementCount)

for i, value := range expression.Values {
valueType := checker.VisitExpression(value, elementType)
for i, element := range arrayExpression.Values {
valueType := checker.VisitExpression(element, arrayExpression, elementType)

argumentTypes[i] = valueType

checker.checkVariableMove(value)
checker.checkResourceMoveOperation(value, valueType)
checker.checkResourceMoveOperation(element, valueType)
}
}

Expand All @@ -87,7 +86,7 @@ func (checker *Checker) VisitArrayExpression(expression *ast.ArrayExpression) Ty
checker.report(
&TypeAnnotationRequiredError{
Cause: "cannot infer type from array literal:",
Pos: expression.StartPos,
Pos: arrayExpression.StartPos,
},
)

Expand All @@ -100,7 +99,7 @@ func (checker *Checker) VisitArrayExpression(expression *ast.ArrayExpression) Ty
}

checker.Elaboration.SetArrayExpressionTypes(
expression,
arrayExpression,
ArrayExpressionTypes{
ArgumentTypes: argumentTypes,
ArrayType: resultType,
Expand Down
3 changes: 1 addition & 2 deletions runtime/sema/check_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (checker *Checker) checkAssignment(
if checker.accessedSelfMember(target) == nil {
checkValue = checker.VisitExpressionWithReferenceCheck
}
valueType = checkValue(value, targetType)
valueType = checkValue(value, assignment, targetType)

// NOTE: Visiting the `value` checks the compatibility between value and target types.
// Check for the *target* type, so that assignment using non-resource typed value (e.g. `nil`)
Expand Down Expand Up @@ -110,7 +110,6 @@ func (checker *Checker) checkAssignment(
}

checker.enforceViewAssignment(assignment, target)
checker.checkVariableMove(value)

checker.recordResourceInvalidation(
value,
Expand Down
3 changes: 1 addition & 2 deletions runtime/sema/check_attach_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ func (checker *Checker) VisitAttachExpression(expression *ast.AttachExpression)
attachment := expression.Attachment
baseExpression := expression.Base

baseType := checker.VisitExpression(baseExpression, checker.expectedType)
baseType := checker.VisitExpression(baseExpression, expression, checker.expectedType)
attachmentType := checker.checkInvocationExpression(attachment)

if attachmentType.IsInvalidType() || baseType.IsInvalidType() {
return InvalidType
}

checker.checkVariableMove(baseExpression)
checker.checkResourceMoveOperation(baseExpression, attachmentType)

// check that the attachment type is a valid attachment,
Expand Down
21 changes: 18 additions & 3 deletions runtime/sema/check_binary_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func (checker *Checker) VisitBinaryExpression(expression *ast.BinaryExpression)
// Visit the expression, with contextually expected type. Use the expected type
// only for inferring wherever possible, but do not check for compatibility.
// Compatibility is checked separately for each operand kind.
leftType = checker.VisitExpressionWithForceType(expression.Left, expectedType, false)
leftType = checker.VisitExpressionWithForceType(
expression.Left,
expression,
expectedType,
false,
)

leftIsInvalid := leftType.IsInvalidType()

Expand Down Expand Up @@ -123,7 +128,12 @@ func (checker *Checker) VisitBinaryExpression(expression *ast.BinaryExpression)
expectedType = leftType
}

rightType = checker.VisitExpressionWithForceType(expression.Right, expectedType, false)
rightType = checker.VisitExpressionWithForceType(
expression.Right,
expression,
expectedType,
false,
)

rightIsInvalid := rightType.IsInvalidType()

Expand Down Expand Up @@ -174,7 +184,12 @@ func (checker *Checker) VisitBinaryExpression(expression *ast.BinaryExpression)
expectedType = optionalLeftType.Type
}
}
return checker.VisitExpressionWithForceType(expression.Right, expectedType, false)
return checker.VisitExpressionWithForceType(
expression.Right,
expression,
expectedType,
false,
)
})

rightIsInvalid := rightType.IsInvalidType()
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/check_casting_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (checker *Checker) VisitCastingExpression(expression *ast.CastingExpression

beforeErrors := len(checker.errors)

leftHandType, exprActualType := checker.visitExpression(leftHandExpression, expectedType)
leftHandType, exprActualType := checker.visitExpression(leftHandExpression, expression, expectedType)

hasErrors := len(checker.errors) > beforeErrors

Expand Down
12 changes: 10 additions & 2 deletions runtime/sema/check_composite_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,7 @@ func (checker *Checker) checkDefaultDestroyParamExpressionKind(

func (checker *Checker) checkDefaultDestroyEventParam(
param Parameter,
eventDeclaration ast.CompositeLikeDeclaration,
astParam *ast.Parameter,
containerType EntitlementSupportingType,
containerDeclaration ast.Declaration,
Expand All @@ -2113,7 +2114,8 @@ func (checker *Checker) checkDefaultDestroyEventParam(
compositeContainer,
compositeContainer.baseTypeDocString)
}
param.DefaultArgument = checker.VisitExpression(paramDefaultArgument, paramType)

param.DefaultArgument = checker.VisitExpression(paramDefaultArgument, eventDeclaration, paramType)

// default events must have default arguments for all their parameters; this is enforced in the parser
// we want to check that these arguments are all either literals or field accesses, and have primitive types
Expand Down Expand Up @@ -2143,7 +2145,13 @@ func (checker *Checker) checkDefaultDestroyEvent(
defer checker.leaveValueScope(eventDeclaration.EndPosition, true)

for index, param := range eventType.ConstructorParameters {
checker.checkDefaultDestroyEventParam(param, constructorFunctionParameters[index], containerType, containerDeclaration)
checker.checkDefaultDestroyEventParam(
param,
eventDeclaration,
constructorFunctionParameters[index],
containerType,
containerDeclaration,
)
}
}

Expand Down
Loading

0 comments on commit 7584510

Please sign in to comment.