starkware-libs / cairo

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

bug: LSP doesn't check types in Storage #3939

Open Chepelau opened 10 months ago

Chepelau commented 10 months ago

Bug Report

I'm using latest scarb and cairo:

scarb 0.6.2 (c07fa6155 2023-08-15)
cairo: 2.1.1 (https://crates.io/crates/cairo-lang-compiler/2.1.1)

Also using LSP shipped with scarb(which is also the latest version provided by scarb).

Current behavior: When there are some unimported/wrongly named types in Storage used, the LSP does not report that, and pretends it's all alright even if we then use those storage vars in different module. This is problem because if we then try to build the contract it prints tens of errors which are hard debug and determine where they stem from. It might be simple on small contract, but when it contains several files and folders, it might prove quite difficult.

Expected behavior: LSP Should report that the type is missing/unknown.

Example code:

#[starknet::contract]
mod BugContract {

    // type my_felt = felt252;
    // type my_u256 = u256;

    #[storage]
    struct Storage {
        some_calculations_results: LegacyMap<(my_felt, some_completely_random_type), my_u256>
    }
}

Note the commented type aliases(if they are uncommented, then there is no problem).

First of all, with cairo-compile, the error is only one - but it points to LegacyMap:

error: Type not found.
 --> contract.cairo:9:36
        some_calculations_results: LegacyMap<(my_felt, some_completely_random_type), my_u256>
                                   ^*******^

Instead of the unknown types.

With scarb on the other hand it not only prints tens of errors, but some of them might seem kinda cryptic for some newcomer and if the project is large, it is almost impossible to determine what is going:

error: Candidate impl core::starknet::SyscallResultTraitImpl::<?0> has an unused generic parameter.
 --> contract:69:19
                ).unwrap_syscall()
                  ^************^

Other information:

 ->  cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.6 LTS"

Also here is picture of the code, where LSP clearly doesn't report any bug: image

orizi commented 10 months ago

The missing diags in the LSP are an issue - we are looking into it. About the cairo-compile call - do note that it runs without the starknet plugin - so all the attributes are ignored, and the LegacyMap is indeed note known at all. starknet-compile is the equivalent you were actually looking for there.

Chepelau commented 10 months ago

Yes, sorry. Tried with starknet-compile and it results in the same errors as scarb build