Skip to content

Misleading errors when trying to write on a contract using self: @ContractState #7822

@feltroidprime

Description

@feltroidprime

Bug Report

Cairo version:

2.11.4

Current behavior:
[package]
name = "u384_store"
version = "0.1.0"
edition = "2023_10"

use core::circuit::{conversions, u384};
use starknet::storage_access::StorePacking;

#[derive(Copy, Drop, Debug, PartialEq, starknet::Store)]
pub struct u384Packed {
    pub x0: felt252,
    pub x1: felt252,
}

pub impl u384StorePacking of StorePacking<u384, u384Packed> {
    fn pack(value: u384) -> u384Packed {
        let x0 = conversions::two_u96_into_felt252(value.limb0, value.limb1);
        let x1 = conversions::two_u96_into_felt252(value.limb2, value.limb3);
        u384Packed { x0, x1 }
    }

    fn unpack(value: u384Packed) -> u384 {
        let x0 = value.x0;
        let x1 = value.x1;
        let (limb0, limb1) = conversions::felt252_try_into_two_u96(x0).unwrap();
        let (limb2, limb3) = conversions::felt252_try_into_two_u96(x1).unwrap();
        return u384 { limb0, limb1, limb2, limb3 };
    }
}


#[starknet::interface]
trait ITest<TContractState> {
    fn test(self: @TContractState);
}

#[starknet::contract]
mod Test {
    use super::{u384, u384StorePacking};
    #[storage]
    struct Storage {
        value: u384,
    }
    #[abi(embed_v0)]
    impl ITest of super::ITest<ContractState> {
        fn test(self: @ContractState) {
            let test_val = u384 { limb0: 1, limb1: 2, limb2: 3, limb3: 4 };

            self.value.write(test_val);

            let val = self.value.read();
        }
    }
}
(venv) felt@fedora:~/PycharmProjects/garaga/src/contracts/drand_quicknet$ scarb build
   Compiling drand_quicknet v0.1.0 (/home/felt/PycharmProjects/garaga/src/contracts/drand_quicknet/Scarb.toml)
error[E0002]: Method `write` could not be called on type `core::starknet::storage::storage_base::StorageBase::<core::circuit::u384>`.
Candidate `core::starknet::storage::map::StorageMapWriteAccess::write` inference failed with: Trait has no implementation in context: core::starknet::storage::map::StorageMapWriteAccess::<core::starknet::storage::storage_base::StorageBase::<core::circuit::u384>>.
Candidate `core::starknet::storage::StoragePointerWriteAccess::write` inference failed with: Trait has no implementation in context: core::starknet::storage::StoragePointerWriteAccess::<core::starknet::storage::storage_base::StorageBase::<core::circuit::u384>>.
Candidate `core::starknet::storage::map::StorageMapWriteAccess::write` inference failed with: Trait has no implementation in context: core::starknet::storage::map::StorageMapWriteAccess::<core::starknet::storage::StoragePath::<core::circuit::u384>>.
Candidate `core::starknet::storage::StoragePointerWriteAccess::write` inference failed with: Trait has no implementation in context: core::starknet::storage::StoragePointerWriteAccess::<core::starknet::storage::StoragePath::<core::circuit::u384>>.
 --> /home/felt/PycharmProjects/garaga/src/contracts/drand_quicknet/src/drand_verifier.cairo:49:24
            self.value.write(test_val);
                       ^^^^^

warn[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
 --> /home/felt/PycharmProjects/garaga/src/contracts/drand_quicknet/src/drand_verifier.cairo:47:17
            let test_val = u384 { limb0: 1, limb1: 2, limb2: 3, limb3: 4 };
                ^^^^^^^^

warn[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
 --> /home/felt/PycharmProjects/garaga/src/contracts/drand_quicknet/src/drand_verifier.cairo:51:17
            let val = self.value.read();
                ^^^

error: could not compile `drand_quicknet` due to previous error

Expected behavior:

The compiler should say we're trying to write inside a "read-only" function and suggest to change to ref self: ContractState, as the current error is misleading.

Other information:
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions