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

BUG: Should not be able to statically access the fields of a optionally casted reference #747

Closed
joshuahannan opened this issue Mar 31, 2021 · 1 comment · Fixed by #1303 or onflow/flow#722
Assignees
Labels
Bug Something isn't working Feedback

Comments

@joshuahannan
Copy link
Member

Problem

When creating a reference to an object stored in a dictionary using as?, the checker does not report a static error when trying to access a field of the optional reference.

Steps to Reproduce

  1. Copy this contract
pub contract TestCollection {
    pub resource IDRes {
        pub let id: UInt32
        init() {
            self.id = 0
        }
    }

    pub resource Collection {
        access(self) var idResources: @{String: IDRes}
        init() {
            self.idResources <- {}
        }

        destroy() {
            destroy self.idResources
        }

        pub fun borrowID(_ key: String, _ id: UInt32): UInt32 {
            let idref = &self.idResources[key] as? &IDRes

            let id = idref.id

            return id
        }
    }

    init() {
        let col <- create Collection()

        let del = col.borrowID("hello", 1)

        destroy col
    }
}
  1. Deploy the contract and see this error:
WARN[0004] ERR [08e509] Execution failed:
error: dereference failed
  --> 01cf0e2f2f715450.TestCollection:23:21
   |
23 |             let id = idref.id

Acceptance Criteria

It should cause a static error because it is trying to access the field of an optional, right?

@joshuahannan joshuahannan added Bug Something isn't working Feedback T-Bug 🐞 labels Mar 31, 2021
@joshuahannan joshuahannan changed the title Should not be able to statically access the fields of a optionally casted reference BUG: Should not be able to statically access the fields of a optionally casted reference Mar 31, 2021
@joshuahannan
Copy link
Member Author

Or maybe I am misinterpreting what the result of the cast should be if the key doesn't exist in the dictionary. It seems like it should be nil, but it looks like it is still giving me an &IDRes even though it doesn't point to anything. I get the same thing when doing a force cast also.

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
3 participants