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

Problems loading values and @AnyResource #3491

Closed
sisyphusSmiling opened this issue Jul 25, 2024 · 10 comments · Fixed by #3493
Closed

Problems loading values and @AnyResource #3491

sisyphusSmiling opened this issue Jul 25, 2024 · 10 comments · Fixed by #3493
Assignees
Labels
Bug Something isn't working Feedback

Comments

@sisyphusSmiling
Copy link
Contributor

sisyphusSmiling commented Jul 25, 2024

Current Behavior

Attempting to load a resource from account storage as @AnyResource fails

Expected Behavior

Loading resources from storage as AnyResource should succeed

Steps To Reproduce

Attempt to load a resource from storage using a transaction such as:

import "FlowToken"

transaction {
    prepare(signer: auth(Storage) &Account) {
        let v <- FlowToken.createEmptyVault(vaultType: Type<@FlowToken.Vault>())

        signer.storage.save(<-v, to: /storage/temp)

        let loaded <- signer.storage.load<@AnyResource>(from: /storage/temp)
            ?? panic("no resource found in vault storage path")
        
        destroy loaded
    }
}

Which outputs the error:

Execution failed:\nerror: invalid transfer of value: expected `<<invalid>>`, got `FlowToken.Vault`\n --> 7e6d3a1240487452be2f9e265dfdcaa2e66bbb34f165518e700eb1be621fdbc1:6:17\n  |\n6 |         let m <- acct.storage.load<@AnyResource>(from: /storage/temp)\n7 |             ?? panic(\"no resource found in vault storage path\")\n  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n\n

Environment

- Flow CLI: v1.21.0-cadence-v1.0.0-preview.28 and above
- Cadence version: v1.0.0-preview.35
- Network: Emulator (waiting on PN HCU to validate there)
@sisyphusSmiling
Copy link
Contributor Author

sisyphusSmiling commented Jul 25, 2024

Was able to reproduce using the above transaction on migrationnet in transaction 55ac88fd65ed289e67d5aa960a1f6f3d4e8ab9b702f2687e3cab65b916d3e3e0 which output

Transaction ID: 55ac88fd65ed289e67d5aa960a1f6f3d4e8ab9b702f2687e3cab65b916d3e3e0

Block ID	d21c9f94745e2df7484862ebb3789159b15be1e1e70c9357186763cebf8e6e60
Block Height	204925860
❌ Transaction Error
[Error Code: 1101] error caused by: 1 error occurred:
	* transaction execute failed: [Error Code: 1101] cadence runtime error: Execution failed:
error: invalid transfer of value: expected `<<invalid>>`, got `FlowToken.Vault`
  --> 55ac88fd65ed289e67d5aa960a1f6f3d4e8ab9b702f2687e3cab65b916d3e3e0:9:22
   |
 9 |         let loaded <- signer.storage.load<@AnyResource>(from: /storage/temp)
10 |             ?? panic("no resource found in vault storage path")
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@turbolent
Copy link
Member

Flow CLI v1.21.0-cadence-v1.0.0-preview.28 uses Cadence v1.0.0-preview.35
Flow CLI v1.21.0-cadence-v1.0.0-preview.27 uses Cadence v1.0.0-preview.34

@turbolent turbolent self-assigned this Jul 25, 2024
@turbolent
Copy link
Member

Maybe this is related to the nil-coalescing changes? #3423

@turbolent
Copy link
Member

turbolent commented Jul 25, 2024

Smaller reproducer

resource R {}

fun f(): @AnyResource? { // specifically returning `@AnyResource?` fails. returning `@R?` works
    return <-create R()
}

let y <- f() ?? panic("no R")

@turbolent
Copy link
Member

@SupunS Any idea why this could happen? Maybe the new use of LeastCommonSuperType for checking of the nil-coalescing operator does not handle the combination of AnyResource and Never?

@turbolent
Copy link
Member

Pushed up a reproducer as a test case in https://github.com/onflow/cadence/pull/new/bastian/3491-fix-load-anyresource

@SupunS
Copy link
Member

SupunS commented Jul 25, 2024

hmm, strange. I'll have a look. Thanks for the reproducer!

@turbolent
Copy link
Member

I guess the problem is that for the LCS inputs AnyResource and Never the result is the Any type, which is specially returns InvalidType if the result is AnyType.

The LCS function probably needs to somehow handle the case, or we specially handle the case in the nil-coalescing checking

@SupunS
Copy link
Member

SupunS commented Jul 25, 2024

on a side note: It seems like the Invalid type somehow goes to interpreter. Should've been reported by the checker and terminate there.

@turbolent
Copy link
Member

It seems like the Invalid type somehow goes to interpreter. Should've been reported by the checker and terminate there.

Good point! It looks like most, but not all calls of LeastCommonSuperType are followed by a check if the result is InvalidType at the call-site. Maybe we should add this to all call-sites, or even move the error into the LCS function so it is not forgotten, like in the nil-coalescing case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Feedback
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants