From c66466eb6b1a1f93925ebe577ef799f8518857f6 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Wed, 2 Aug 2023 09:48:11 -0700 Subject: [PATCH 1/4] Fix error on reference creation with invalid type --- runtime/sema/check_reference_expression.go | 4 ++- runtime/tests/checker/reference_test.go | 37 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/runtime/sema/check_reference_expression.go b/runtime/sema/check_reference_expression.go index a18e5f3e81..16092129b5 100644 --- a/runtime/sema/check_reference_expression.go +++ b/runtime/sema/check_reference_expression.go @@ -93,7 +93,9 @@ func (checker *Checker) VisitReferenceExpression(referenceExpression *ast.Refere // If the reference type was a non-optional type, // check that the referenced expression does not have an optional type - if _, ok := actualType.(*OptionalType); ok != isOpt { + // Do not report an error if the `expectedLeftType` is unknown + + if _, ok := actualType.(*OptionalType); ok != isOpt && expectedLeftType != nil { checker.report(&TypeMismatchError{ ExpectedType: expectedLeftType, ActualType: actualType, diff --git a/runtime/tests/checker/reference_test.go b/runtime/tests/checker/reference_test.go index 809a461135..e804153e9f 100644 --- a/runtime/tests/checker/reference_test.go +++ b/runtime/tests/checker/reference_test.go @@ -2812,3 +2812,40 @@ func TestCheckResourceReferenceMethodInvocationAfterMove(t *testing.T) { invalidatedRefError := &sema.InvalidatedResourceReferenceError{} assert.ErrorAs(t, errs[0], &invalidatedRefError) } + +func TestCheckReferenceCreationWithInvalidType(t *testing.T) { + + t.Parallel() + + t.Run("invalid reference type", func(t *testing.T) { + + t.Parallel() + + _, err := ParseAndCheck(t, ` + let foo: AnyStruct? = nil + let x = &foo as &Foo + `) + + errs := RequireCheckerErrors(t, err, 1) + + var notDeclaredError *sema.NotDeclaredError + require.ErrorAs(t, errs[0], ¬DeclaredError) + }) + + t.Run("valid non-reference type", func(t *testing.T) { + + t.Parallel() + + _, err := ParseAndCheck(t, ` + struct Foo {} + + let foo: AnyStruct? = nil + let x = &foo as Foo + `) + + errs := RequireCheckerErrors(t, err, 1) + + var nonReferenceTypeReferenceError *sema.NonReferenceTypeReferenceError + require.ErrorAs(t, errs[0], &nonReferenceTypeReferenceError) + }) +} From ec4d615b89d8542cfb9d618866c0c21a32356cc9 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Mon, 18 Sep 2023 08:37:30 -0700 Subject: [PATCH 2/4] Fix TestAccount creation --- runtime/stdlib/test.go | 2 +- runtime/stdlib/test_test.go | 201 ++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+), 1 deletion(-) diff --git a/runtime/stdlib/test.go b/runtime/stdlib/test.go index cbfd46c98f..47a565cb8f 100644 --- a/runtime/stdlib/test.go +++ b/runtime/stdlib/test.go @@ -40,7 +40,7 @@ const testTransactionResultTypeName = "TransactionResult" const testResultStatusTypeName = "ResultStatus" const testResultStatusTypeSucceededCaseName = "succeeded" const testResultStatusTypeFailedCaseName = "failed" -const testAccountTypeName = "Account" +const testAccountTypeName = "TestAccount" const testErrorTypeName = "Error" const testMatcherTypeName = "Matcher" diff --git a/runtime/stdlib/test_test.go b/runtime/stdlib/test_test.go index 980201bc03..da207198ed 100644 --- a/runtime/stdlib/test_test.go +++ b/runtime/stdlib/test_test.go @@ -2532,6 +2532,51 @@ func TestBlockchain(t *testing.T) { // TODO: Add more tests for the remaining functions. } +func TestBlockchainAccount(t *testing.T) { + + t.Parallel() + + t.Run("create account", func(t *testing.T) { + t.Parallel() + + const script = ` + import Test + + access(all) fun test() { + let blockchain = Test.newEmulatorBlockchain() + let account = blockchain.createAccount() + assert(account.address == 0x0100000000000000) + } + ` + + testFramework := &mockedTestFramework{ + newEmulatorBackend: func() Blockchain { + return &mockedBlockchain{ + createAccount: func() (*Account, error) { + return &Account{ + PublicKey: &PublicKey{ + PublicKey: []byte{1, 2, 3}, + SignAlgo: sema.SignatureAlgorithmECDSA_P256, + }, + Address: common.Address{1}, + }, nil + }, + + stdlibHandler: func() StandardLibraryHandler { + return testStandardLibraryHandler{} + }, + } + }, + } + + inter, err := newTestContractInterpreterWithTestFramework(t, script, testFramework) + require.NoError(t, err) + + _, err = inter.Invoke("test") + require.NoError(t, err) + }) +} + type mockedTestFramework struct { newEmulatorBackend func() Blockchain readFile func(s string) (string, error) @@ -2555,6 +2600,7 @@ func (m mockedTestFramework) ReadFile(fileName string) (string, error) { return m.readFile(fileName) } +// mockedBlockchain is the implementation of `Blockchain` for testing purposes. type mockedBlockchain struct { runScript func(inter *interpreter.Interpreter, code string, arguments []interpreter.Value) createAccount func() (*Account, error) @@ -2713,3 +2759,158 @@ func (m mockedBlockchain) LoadSnapshot(name string) error { return m.loadSnapshot(name) } + +// testStandardLibraryHandler is the implementation of `StandardLibraryHandler` for testing purposes. +type testStandardLibraryHandler struct { +} + +var _ StandardLibraryHandler = testStandardLibraryHandler{} + +func (t testStandardLibraryHandler) ProgramLog(message string, locationRange interpreter.LocationRange) error { + panic("not implemented") +} + +func (t testStandardLibraryHandler) ReadRandom(bytes []byte) error { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetBlockAtHeight(height uint64) (block Block, exists bool, err error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetCurrentBlockHeight() (uint64, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) EmitEvent( + inter *interpreter.Interpreter, + eventType *sema.CompositeType, + values []interpreter.Value, + locationRange interpreter.LocationRange, +) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GenerateAccountID(address common.Address) (uint64, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetAccountBalance(address common.Address) (uint64, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetAccountAvailableBalance(address common.Address) (uint64, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) CommitStorageTemporarily(inter *interpreter.Interpreter) error { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetStorageUsed(address common.Address) (uint64, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetStorageCapacity(address common.Address) (uint64, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) ValidatePublicKey(key *PublicKey) error { + panic("not implemented") +} + +func (t testStandardLibraryHandler) VerifySignature( + signature []byte, + tag string, + signedData []byte, + publicKey []byte, + signatureAlgorithm sema.SignatureAlgorithm, + hashAlgorithm sema.HashAlgorithm, +) (bool, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) BLSVerifyPOP(publicKey *PublicKey, signature []byte) (bool, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) Hash(data []byte, tag string, algorithm sema.HashAlgorithm) ([]byte, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetAccountKey(address common.Address, index int) (*AccountKey, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) AccountKeysCount(address common.Address) (uint64, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) AddAccountKey( + address common.Address, + key *PublicKey, + algo sema.HashAlgorithm, + weight int, +) (*AccountKey, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) RevokeAccountKey(address common.Address, index int) (*AccountKey, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetAccountContractCode(location common.AddressLocation) ([]byte, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) ParseAndCheckProgram( + code []byte, + location common.Location, + getAndSetProgram bool) (*interpreter.Program, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) UpdateAccountContractCode(location common.AddressLocation, code []byte) error { + panic("not implemented") +} + +func (t testStandardLibraryHandler) RecordContractUpdate(location common.AddressLocation, value *interpreter.CompositeValue) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) InterpretContract( + location common.AddressLocation, + program *interpreter.Program, + name string, + invocation DeployedContractConstructorInvocation, +) (*interpreter.CompositeValue, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) TemporarilyRecordCode(location common.AddressLocation, code []byte) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) RemoveAccountContractCode(location common.AddressLocation) error { + panic("not implemented") +} + +func (t testStandardLibraryHandler) RecordContractRemoval(location common.AddressLocation) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) GetAccountContractNames(address common.Address) ([]string, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) CreateAccount(payer common.Address) (address common.Address, err error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) BLSAggregatePublicKeys(publicKeys []*PublicKey) (*PublicKey, error) { + panic("not implemented") +} + +func (t testStandardLibraryHandler) BLSAggregateSignatures(signatures [][]byte) ([]byte, error) { + panic("not implemented") +} From 9050a96146eafdbd75053292d525be6c1839e004 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Thu, 21 Sep 2023 07:48:40 -0700 Subject: [PATCH 3/4] Bump version --- npm-packages/cadence-parser/package.json | 2 +- version.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/npm-packages/cadence-parser/package.json b/npm-packages/cadence-parser/package.json index eb05dc83e8..e3c4b8f8ea 100644 --- a/npm-packages/cadence-parser/package.json +++ b/npm-packages/cadence-parser/package.json @@ -1,6 +1,6 @@ { "name": "@onflow/cadence-parser", - "version": "0.40.0", + "version": "1.0.0-preview.1", "description": "The Cadence parser", "homepage": "https://github.com/onflow/cadence", "repository": { diff --git a/version.go b/version.go index 7f8e0767df..df4564bfcd 100644 --- a/version.go +++ b/version.go @@ -21,4 +21,4 @@ package cadence -const Version = "v0.40.0" +const Version = "v1.0.0-preview.1" From 091e17fce76db8538bae433ce70f0076c36b8cb3 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Thu, 21 Sep 2023 08:27:21 -0700 Subject: [PATCH 4/4] Remove unused testStandardLibraryHandler --- runtime/stdlib/test_test.go | 157 +----------------------------------- 1 file changed, 1 insertion(+), 156 deletions(-) diff --git a/runtime/stdlib/test_test.go b/runtime/stdlib/test_test.go index da207198ed..f16b66075d 100644 --- a/runtime/stdlib/test_test.go +++ b/runtime/stdlib/test_test.go @@ -2563,7 +2563,7 @@ func TestBlockchainAccount(t *testing.T) { }, stdlibHandler: func() StandardLibraryHandler { - return testStandardLibraryHandler{} + return nil }, } }, @@ -2759,158 +2759,3 @@ func (m mockedBlockchain) LoadSnapshot(name string) error { return m.loadSnapshot(name) } - -// testStandardLibraryHandler is the implementation of `StandardLibraryHandler` for testing purposes. -type testStandardLibraryHandler struct { -} - -var _ StandardLibraryHandler = testStandardLibraryHandler{} - -func (t testStandardLibraryHandler) ProgramLog(message string, locationRange interpreter.LocationRange) error { - panic("not implemented") -} - -func (t testStandardLibraryHandler) ReadRandom(bytes []byte) error { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetBlockAtHeight(height uint64) (block Block, exists bool, err error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetCurrentBlockHeight() (uint64, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) EmitEvent( - inter *interpreter.Interpreter, - eventType *sema.CompositeType, - values []interpreter.Value, - locationRange interpreter.LocationRange, -) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GenerateAccountID(address common.Address) (uint64, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetAccountBalance(address common.Address) (uint64, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetAccountAvailableBalance(address common.Address) (uint64, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) CommitStorageTemporarily(inter *interpreter.Interpreter) error { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetStorageUsed(address common.Address) (uint64, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetStorageCapacity(address common.Address) (uint64, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) ValidatePublicKey(key *PublicKey) error { - panic("not implemented") -} - -func (t testStandardLibraryHandler) VerifySignature( - signature []byte, - tag string, - signedData []byte, - publicKey []byte, - signatureAlgorithm sema.SignatureAlgorithm, - hashAlgorithm sema.HashAlgorithm, -) (bool, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) BLSVerifyPOP(publicKey *PublicKey, signature []byte) (bool, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) Hash(data []byte, tag string, algorithm sema.HashAlgorithm) ([]byte, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetAccountKey(address common.Address, index int) (*AccountKey, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) AccountKeysCount(address common.Address) (uint64, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) AddAccountKey( - address common.Address, - key *PublicKey, - algo sema.HashAlgorithm, - weight int, -) (*AccountKey, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) RevokeAccountKey(address common.Address, index int) (*AccountKey, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetAccountContractCode(location common.AddressLocation) ([]byte, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) ParseAndCheckProgram( - code []byte, - location common.Location, - getAndSetProgram bool) (*interpreter.Program, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) UpdateAccountContractCode(location common.AddressLocation, code []byte) error { - panic("not implemented") -} - -func (t testStandardLibraryHandler) RecordContractUpdate(location common.AddressLocation, value *interpreter.CompositeValue) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) InterpretContract( - location common.AddressLocation, - program *interpreter.Program, - name string, - invocation DeployedContractConstructorInvocation, -) (*interpreter.CompositeValue, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) TemporarilyRecordCode(location common.AddressLocation, code []byte) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) RemoveAccountContractCode(location common.AddressLocation) error { - panic("not implemented") -} - -func (t testStandardLibraryHandler) RecordContractRemoval(location common.AddressLocation) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) GetAccountContractNames(address common.Address) ([]string, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) CreateAccount(payer common.Address) (address common.Address, err error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) BLSAggregatePublicKeys(publicKeys []*PublicKey) (*PublicKey, error) { - panic("not implemented") -} - -func (t testStandardLibraryHandler) BLSAggregateSignatures(signatures [][]byte) ([]byte, error) { - panic("not implemented") -}