cargo-contract icon indicating copy to clipboard operation
cargo-contract copied to clipboard

Confusing error output when using `String` in a contract

Open cmichi opened this issue 4 years ago • 1 comments

A question we are often asked is about strange errors which appear as soon as let _ = String::from(""); is inserted into a contract:

error[E0433]: failed to resolve: use of undeclared type `String`                                                                                               
  --> /tmp/foo/lib.rs:20:21                                                                                                                             
   |                                                                                                                                                           
20 |             let _ = String::from("");                                                                                                                 
   |                     ^^^^^^ use of undeclared type `String`                                                                                                
                                                                                                                                                               
error: duplicate lang item in crate `std`: `panic_impl`.                                                                                                       
  |                                                                                                                                                            
  = note: the lang item is first defined in crate `ink_env` (which `foo` depends on)                                                                           
  = note: first definition in `ink_env` loaded from /tmp/foo/target/ink/wasm32-unknown-unknown/release/deps/libink_env-ea0819580cd18483.rlib, /tmp/foo/target/ink/wasm32-unknown-unknown/release/deps/libink_env-ea0819580cd18483.rmeta                                                                           = note: second definition in `std` loaded from /tmp/foo/target/ink/wasm32-unknown-unknown/release/deps/libstd-bf37d98bcc6a8dd7.rlib, /tmp/foo/t
arget/ink/wasm32-unknown-unknown/release/deps/libstd-bf37d98bcc6a8dd7.rmeta                                                                                    
                                                                                                                                                               
error: duplicate lang item in crate `std`: `oom`.                                                                                                              
  |                                                                                                                                                            
  = note: the lang item is first defined in crate `ink_allocator` (which `ink_env` depends on)                                                                 
  = note: first definition in `ink_allocator` loaded from /tmp/foo/target/ink/wasm32-unknown-unknown/release/deps/libink_allocator-f5b1b17628829f7b.rlib
, /tmp/foo/target/ink/wasm32-unknown-unknown/release/deps/libink_allocator-f5b1b17628829f7b.rmeta                                                       
  = note: second definition in `std` loaded from /tmp/foo/target/ink/wasm32-unknown-unknown/release/deps/libstd-bf37d98bcc6a8dd7.rlib, /tmp/foo/t
arget/ink/wasm32-unknown-unknown/release/deps/libstd-bf37d98bcc6a8dd7.rmeta                                                                                    
                                                                                                                                                               
error: duplicate lang item in crate `core`: `bool`.                                                                                                            
  |                                                                                                                                                            
  = note: the lang item is first defined in crate `core` (which `foo` depends on)                                                                              
  = note: first definition in `core` loaded from /tmp/foo/target/ink/wasm32-unknown-unknown/release/deps/libcore-a85281212c83f9cf.rlib                  
  = note: second definition in `core` loaded from /home/michi/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libcor
e-9627209c1fcc368a.rlib                                       
...

Then there are a lot more duplicate lang item errors, those are especially irritating for beginners.

It would be great if we could improve our error output here.

cmichi avatar Apr 12 '21 13:04 cmichi

so i'm guessing it should suggest that they add the following to the top of the associated file:

use ink::prelude::string::String;

or could it just suggest

use ink::prelude::*;

https://use.ink/frontend/core/hooks/contracts/use-call#handling-resultt-e-responses-from-an-ink-contract

ltfschoen avatar May 29 '23 05:05 ltfschoen