Substrate ink! compile Error "function pointers cannot appear in constant functions"

347 views Asked by At
  1. I try to "https://substrate.dev/substrate-contracts-workshop/#/"
  2. I followed the tutorial to create a Rust environment.
  3. And I executed "cargo +nightly test"
  4. I get following Error.
error[E0658]: function pointers cannot appear in constant functions
   --> /Users/shin.takahashi/.cargo/git/checkouts/ink-1add513eda8f5a89/cca3154/lang/src/dispatcher.rs:186:30
    |
186 |               pub const fn new(dispatchable: $dispatchable_fn<Msg, S>) -> Self {
    |                                ^^^^^^^^^^^^
...
237 | / impl_dispatcher_for! {
238 | |     /// Dispatcher for storage preserving messages.
239 | |     struct Dispatcher(DispatchableFn);
240 | | }
    | |_- in this macro invocation
    |
    = note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
    = help: add `#![feature(const_fn_fn_ptr_basics)]` to the crate attributes to enable
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Please tell me what is wrong???

2

There are 2 answers

0
s.Takahashi On

I couldn't solve it, but I was able to make it compilable and executable. Correct the error part of ~ / .cargo / git / checkouts / ink-XXXXX / cca-XXXX / lang / source / disposacher.rs as follows.

//pub const fn new(dispatchable: $dispatchable_fn<Msg, S>) -> Self { // before
pub fn new(dispatchable: $dispatchable_fn<Msg, S>) -> Self { // after
0
ᛗᛁᛣᛖ ᛟᛁᚾᛋ On

Ink seems to have been updated. I ran cargo update and then built the contract without errors.

$ testcontract % cargo update
    Updating git repository `https://github.com/paritytech/ink`
    Updating crates.io index
    Updating git repository `https://github.com/type-metadata/type-metadata.git`
    Updating aho-corasick v0.7.13 -> v0.7.14
    Updating regex v1.3.9 -> v1.4.1
    Updating regex-syntax v0.6.18 -> v0.6.20
    Updating serde_json v1.0.58 -> v1.0.59
    Updating syn v1.0.43 -> v1.0.44
    Updating toml v0.5.6 -> v0.5.7
$ testcontract % sh build.sh
 [1/4] Collecting crate metadata
 [2/4] Building cargo project
   Compiling compiler_builtins v0.1.35
   Compiling core v0.0.0 (/Users/home/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core)
   Compiling rustc-std-workspace-core v1.99.0 (/Users/home/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/rustc-std-workspace-core)
   Compiling alloc v0.0.0 (/var/folders/xj/m862kvrj73x04dvsfr6vs_fm0000gn/T/cargo-xbuild8YkYwG)
    Finished release [optimized] target(s) in 13.19s
   Compiling cfg-if v0.1.10
   Compiling serde_json v1.0.59
   Compiling byte-slice-cast v0.3.5
   Compiling arrayvec v0.5.1
   Compiling memory_units v0.4.0
   Compiling regex-syntax v0.6.20
   Compiling smallvec v1.4.2
   Compiling crunchy v0.2.2
   Compiling paste v0.1.18
   Compiling aho-corasick v0.7.14
   Compiling num-traits v0.2.12
   Compiling toml v0.5.7
   Compiling ink_prelude v2.1.0 (https://github.com/paritytech/ink?tag=latest-v2#cca31543)
   Compiling tiny-keccak v2.0.2
   Compiling wee_alloc v0.4.5
   Compiling ink_alloc v2.1.0 (https://github.com/paritytech/ink?tag=latest-v2#cca31543)
   Compiling proc-macro-crate v0.1.5
   Compiling parity-scale-codec-derive v1.2.2
   Compiling regex v1.4.1
   Compiling parity-scale-codec v1.3.5
   Compiling ink_primitives v2.1.0 (https://github.com/paritytech/ink?tag=latest-v2#cca31543)
   Compiling ink_core_derive v2.1.0 (https://github.com/paritytech/ink?tag=latest-v2#cca31543)
   Compiling ink_lang_macro v2.1.0 (https://github.com/paritytech/ink?tag=latest-v2#cca31543)
   Compiling ink_core v2.1.0 (https://github.com/paritytech/ink?tag=latest-v2#cca31543)
   Compiling ink_lang v2.1.0 (https://github.com/paritytech/ink?tag=latest-v2#cca31543)
   Compiling testcontract v0.1.0 (/var/folders/xj/m862kvrj73x04dvsfr6vs_fm0000gn/T/.cargo-contract_rKI7GO)
    Finished release [optimized] target(s) in 13.91s
 [3/4] Post processing wasm file
 [4/4] Optimizing wasm file
 Original wasm size: 21.8K, Optimized: 5.0K

Your contract is ready. You can find it here:
/Users/home/projects/testcontract/target/testcontract.wasm