starkware-libs / cairo

Cairo is the first Turing-complete language for creating provable programs for general computation.
Apache License 2.0
1.61k stars 499 forks source link

bug: Inference in component #4763

Open gaetbout opened 10 months ago

gaetbout commented 10 months ago

Bug Report

Cairo version:

scarb 2.3.1 (0c8def3aa 2023-10-31) cairo: 2.3.1 (https://crates.io/crates/cairo-lang-compiler/2.3.1) sierra: 1.3.0

Current behavior:

When doing this component:

#[starknet::component]
mod lama_contract {
    #[derive(PartialEq, Copy, Drop, Serde, starknet::Store)]
    struct Lama {
        lama: u128
    }

    #[storage]
    struct Storage {
        my_lama_map: LegacyMap::<(felt252, felt252), Lama>,
    }

    #[event]
    #[derive(Drop, starknet::Event)]
    enum Event {}

    #[generate_trait]
    impl Internal<TContractState, +HasComponent<TContractState>> of InternalTrait<TContractState> {
        fn set_model(self: @ComponentState<TContractState>) {
            let my_lama = self.my_lama_map.read((12, 12));
            assert(my_lama.lama == 0, 'lama-error');
        }
    }
}

and running scarb build I run into:

error: Type annotations needed. Failed to infer ?2
 --> /REDACTED/src/lama.cairo:22:28
            assert(my_lama.lama == 0, 'lama-error');
                           ^**^

However if I change let my_lama = self.my_lama_map.read((12, 12)); to let my_lama: Lama = self.my_lama_map.read((12, 12)); It works.

This works outside of a component...

I believe it is related to ComponentMemberState

Expected behaviour The type should be automatically infered

delaaxe commented 10 months ago

Encountered this issue again in another project