into_hir.rs 25.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of ink!.
//
// ink! is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ink! is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ink!.  If not, see <http://www.gnu.org/licenses/>.

//! Contains all conversion routines from Rust AST to ink! IR.

use crate::{
    ir,
    ir::utils,
};
use core::convert::TryFrom;
use either::Either;
use itertools::Itertools as _;
26 27 28 29
use proc_macro2::{
    Ident,
    Span,
};
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
use std::collections::HashSet;
use syn::{
    parse::{
        Parse,
        ParseStream,
    },
    punctuated::Punctuated,
    spanned::Spanned as _,
    Result,
    Token,
};

impl Parse for ir::Marker {
    fn parse(input: ParseStream) -> Result<Self> {
        let content;
        let paren_token = syn::parenthesized!(content in input);
        let ident = content.parse::<Ident>()?;
        if content.is_empty() {
            return Ok(ir::Marker::Simple(ir::SimpleMarker { paren_token, ident }))
        }
        bail_span!(
            paren_token.span,
            "invalid ink! attribute in the given context",
        )
    }
}

impl TryFrom<(ir::Params, syn::ItemMod)> for ir::Contract {
    type Error = syn::Error;

    fn try_from((params, item_mod): (ir::Params, syn::ItemMod)) -> Result<Self> {
        if item_mod.vis != syn::Visibility::Inherited {
            bail!(
                item_mod.vis,
                "contract module must have no visibility modifier",
            )
        }
        let items = match &item_mod.content {
            None => {
                bail!(
                    item_mod,
                    "contract module must be inline, e.g. `mod m {{ ... }}`",
                )
            }
            Some((_brace, items)) => items.clone(),
        };
        use itertools::Itertools as _;
        let (ink_items, rust_items): (Vec<_>, Vec<_>) = items
            .into_iter()
            .map(ir::Item::try_from)
            .collect::<Result<Vec<_>>>()?
            .into_iter()
            .partition_map(|item| {
                match item {
                    ir::Item::Ink(ink_item) => Either::Left(ink_item),
                    ir::Item::Rust(rust_item) => Either::Right(rust_item),
                }
            });
        let (storage, events, functions) = split_items(ink_items)?;
        if functions.iter().filter(|f| f.is_constructor()).count() == 0 {
            bail!(
                &item_mod,
                "ink! contracts require at least one `#[ink(constructor)]`"
            )
        }
        if functions.iter().filter(|f| f.is_message()).count() == 0 {
            bail!(
                &item_mod,
                "ink! contracts require at least one `#[ink(message)]`"
            )
        }
        let meta_info = ir::MetaInfo::try_from(params)?;
        Ok(Self {
            mod_token: item_mod.mod_token,
            ident: item_mod.ident,
            attrs: item_mod.attrs,
            meta_info,
            storage,
            events,
            functions,
            non_ink_items: rust_items,
        })
    }
}

impl TryFrom<ir::Params> for ir::MetaInfo {
    type Error = syn::Error;

    fn try_from(params: ir::Params) -> Result<Self> {
        let mut unique_params = HashSet::new();
        let mut env_types = None;
        let mut ink_version = None;
        let mut dynamic_allocations: Option<bool> = None;
        let mut compile_as_dependency: Option<bool> = None;
        for param in params.params.iter().cloned() {
            let name = param.ident().to_string();
            if !unique_params.insert(name) {
                bail_span!(param.span(), "encountered parameter multiple times",)
            }
            match param {
                ir::MetaParam::Types(param) => {
                    env_types = Some(ir::MetaTypes::try_from(param)?)
                }
                ir::MetaParam::Version(param) => ink_version = Some(param.data),
                ir::MetaParam::DynamicAllocations(param) => {
                    dynamic_allocations = Some(param.value.value)
                }
                ir::MetaParam::CompileAsDependency(param) => {
                    compile_as_dependency = Some(param.value.value)
                }
            }
        }
        let ink_version = match ink_version {
            None => {
                bail_span!(
                    params.span(),
                    "expected `version` argument at `#[ink::contract(..)]`",
                )
            }
            Some(ink_version) => ink_version,
        };
        Ok(Self {
            env_types: env_types.unwrap_or_default(),
            ink_version,
            dynamic_allocations_enabled: dynamic_allocations.unwrap_or(false),
            compile_as_dependency: compile_as_dependency.unwrap_or(false),
        })
    }
}

impl TryFrom<ir::ParamTypes> for ir::MetaTypes {
    type Error = syn::Error;

    fn try_from(params: ir::ParamTypes) -> Result<Self> {
        Ok(Self { ty: params.ty })
    }
}

impl TryFrom<syn::Attribute> for ir::Marker {
    type Error = syn::Error;

    fn try_from(attr: syn::Attribute) -> Result<Self> {
        if !attr.path.is_ident("ink") {
            bail!(attr, "encountered non-ink! attribute")
        }
        syn::parse2::<Self>(attr.tokens)
    }
}

impl TryFrom<syn::ItemStruct> for ir::ItemStorage {
    type Error = syn::Error;

    fn try_from(item_struct: syn::ItemStruct) -> Result<Self> {
        if let Some(invalid_meta) = item_struct
            .attrs
            .iter()
            .filter_map(|attr| ir::Marker::try_from(attr.clone()).ok())
            .find(|ink_meta| !ink_meta.is_simple("storage"))
        {
            bail_span!(
                invalid_meta.span(),
                "invalid ink! attribute found for `#[ink(storage)]` struct",
            )
        }
        if item_struct.vis != syn::Visibility::Inherited {
            bail!(
                item_struct.vis,
                "visibility modifiers are not allowed for `#[ink(storage)]` structs",
            )
        }
        let span = item_struct.span();
        let fields = match item_struct.fields {
            syn::Fields::Named(named_fields) => named_fields,
            syn::Fields::Unnamed(unnamed_fields) => {
                bail!(
                    unnamed_fields,
                    "`#[ink(storage)]` tuple-structs are forbidden"
                )
            }
            syn::Fields::Unit => {
                bail!(item_struct, "`#[ink(storage)]` unit-structs are forbidden")
            }
        };
        Ok(ir::ItemStorage {
            struct_token: item_struct.struct_token,
            ident: item_struct.ident,
            attrs: item_struct.attrs,
            fields,
            span,
        })
    }
}

impl TryFrom<syn::ItemStruct> for ir::ItemEvent {
    type Error = syn::Error;

    fn try_from(item_struct: syn::ItemStruct) -> Result<Self> {
        if let Some(invalid_meta) = item_struct
            .attrs
            .iter()
            .filter_map(|attr| ir::Marker::try_from(attr.clone()).ok())
            .find(|ink_meta| !ink_meta.is_simple("event"))
        {
            bail_span!(
                invalid_meta.span(),
                "invalid ink! attribute found for `#[ink(event)]` struct",
            )
        }
        if item_struct.vis != syn::Visibility::Inherited {
            bail!(
                item_struct,
                "visibility modifiers are not allowed for `#[ink(event)]` structs",
            )
        }
        let fields = match item_struct.fields {
            syn::Fields::Named(named_fields) => named_fields,
            syn::Fields::Unnamed(unnamed_fields) => {
                bail!(
                    unnamed_fields,
                    "`#[ink(event)]` tuple-structs are forbidden",
                )
            }
            syn::Fields::Unit => {
                bail!(item_struct, "`#[ink(event)]` unit-structs are forbidden",)
            }
        };
        Ok(ir::ItemEvent {
            struct_token: item_struct.struct_token,
            ident: item_struct.ident,
            attrs: item_struct.attrs,
            fields,
        })
    }
}

impl TryFrom<syn::ItemImpl> for ir::ItemImpl {
    type Error = syn::Error;

    fn try_from(item_impl: syn::ItemImpl) -> Result<Self> {
        if item_impl.defaultness.is_some() {
            bail!(item_impl.defaultness, "`default` not supported in ink!",)
        }
        if item_impl.unsafety.is_some() {
            bail!(
                item_impl.unsafety,
                "`unsafe` implementation blocks are not supported in ink!",
            )
        }
        if !(item_impl.generics.params.is_empty()
            && item_impl.generics.where_clause.is_none())
        {
            bail!(
                item_impl.generics,
                "generic implementation blocks are not supported in ink!",
            )
        }
        if item_impl.trait_.is_some() {
            bail!(item_impl, "trait implementations are not supported in ink!",)
        }
        let type_path = match &*item_impl.self_ty {
            syn::Type::Path(type_path) => type_path,
            _ => {
                bail!(
                    item_impl.self_ty,
                    "encountered invalid ink! implementer type ascription",
                )
            }
        };
        if let Some(qself) = &type_path.qself {
            let span = qself
                .lt_token
                .span()
                .join(qself.gt_token.span())
                .expect("all spans are in the same file; qed");
            bail_span!(
                span,
                "implementation blocks for self qualified paths are not supported in ink!",
            )
        };
        let ident: Ident = match type_path.path.get_ident() {
            Some(ident) => ident.clone(),
            None => {
                bail!(
                    type_path.path,
                    "encountered invalid ink! implementer type path",
                )
            }
        };
        for impl_item in item_impl.items.iter() {
            match impl_item {
                syn::ImplItem::Method(_) => (),
                unsupported_item => {
                    bail!(
                        unsupported_item,
                        "only methods are supported inside impl blocks in ink!",
                    )
                }
            }
        }
        let functions = item_impl
            .items
            .into_iter()
            .filter_map(|impl_item| {
                if let syn::ImplItem::Method(method) = impl_item {
                    return Some(method)
                }
                None
            })
            .map(ir::Function::try_from)
            .collect::<Result<Vec<_>>>()?;
        Ok(Self {
            attrs: item_impl.attrs,
            impl_token: item_impl.impl_token,
            self_ty: ident,
            brace_token: item_impl.brace_token,
            functions,
        })
    }
}

impl TryFrom<syn::ImplItemMethod> for ir::Function {
    type Error = syn::Error;

    fn try_from(method: syn::ImplItemMethod) -> Result<Self> {
        let span = method.span();
        // Partitions ink! attributes into simple and non-simple.
        //
        // Simple attributes are e.g. `#[ink(event)]` that have only
        // one simple identifier in their `(` and `)` body.
        let simple = method
            .attrs
            .iter()
            .cloned()
            .filter_map(|attr| ir::Marker::try_from(attr).ok())
            .map(|attr| {
                match attr {
                    ir::Marker::Simple(simple) => simple,
                }
            });
        // Checks for ink! attributes concerning ink! functions.
        //
        // Bails out into error upon unknown or unsupported found ink! attributes.
        // Also errors if some ink! attributes conflict, e.g. if there is a
        // `#[ink(constructor)]` and `#[ink(message)]` attribute or if there is
        // the same attribute multiple times.
        let mut kind = ir::FunctionKind::Method;
        if let Some(err) = simple
            .map(|attr| {
                let new_kind = match attr.ident.to_string().as_str() {
                    "constructor" => {
                        Ok(ir::FunctionKind::Constructor(ir::KindConstructor {
                            selector: ir::FunctionSelector::from(&method.sig.ident),
                        }))
                    }
                    "message" => {
                        Ok(ir::FunctionKind::Message(ir::KindMessage {
                            selector: ir::FunctionSelector::from(&method.sig.ident),
                        }))
                    }
                    unknown => Err(format_err!(unknown, "unknown ink! attribute found",)),
                }?;
                if kind == ir::FunctionKind::Method {
                    kind = new_kind;
                    Ok(())
                } else {
                    Err(format_err_span!(
                        attr.span(),
                        "conflicting ink! attribute found",
                    ))
                }
            })
            .filter_map(Result::err)
            .fold1(|mut err1, err2| {
                err1.combine(err2);
                err1
            })
        {
            return Err(err)
        }
        // Visibility modifiers are currently not supported for ink! functions.
        if method.vis != syn::Visibility::Inherited {
            bail!(
                method.vis,
                "encountered invalid visibility modifier for ink! function",
            )
        }
        // Functions in ink! must not be `default` since that is unsupported.
        if let Some(defaultness) = method.defaultness {
            bail!(
                defaultness,
                "encountered invalid `default` modifier for ink! function",
            )
        }
        // Check and convert method signature into ink! function signature.
        let sig = ir::Signature::try_from(method.sig)?;
        // Followed by some checks that are depending on the given function kind:
        match kind {
            ir::FunctionKind::Constructor(_) => {
428
                let self_arg = sig.self_arg();
429
                if !sig.is_mut() {
430 431 432
                    bail!(
                        self_arg,
                        "#[ink(constructor)] functions must have a `&mut self` receiver",
433 434 435 436 437
                    )
                }
                if sig.output != syn::ReturnType::Default {
                    bail!(
                        sig.output,
438
                        "#[ink(constructor)] functions must not have a return type",
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
                    )
                }
            }
            ir::FunctionKind::Message(_) | ir::FunctionKind::Method => {
                if sig.self_arg().reference.is_none() {
                    bail_span!(
                        sig.span(),
                        "ink! messages and methods must be either `&self` or `&mut self`",
                    )
                }
            }
        }
        // Retain non-ink! attributes only.
        let non_ink_attrs = method
            .attrs
            .into_iter()
            .filter(|attr| ir::Marker::try_from(attr.clone()).is_err())
            .collect::<Vec<_>>();
        // Finally return the checked ink! function.
        Ok(Self {
            attrs: non_ink_attrs,
            kind,
            sig,
            block: method.block,
            span,
        })
    }
}

impl TryFrom<syn::Signature> for ir::Signature {
    type Error = syn::Error;

    fn try_from(sig: syn::Signature) -> Result<Self> {
        if let Some(constness) = sig.constness {
            bail!(constness, "`const fn` is not supported for ink! functions",)
        }
        if let Some(asyncness) = sig.asyncness {
            bail!(asyncness, "`async fn` is not supported for ink! functions",)
        }
        if let Some(unsafety) = sig.unsafety {
            bail!(unsafety, "`unsafe fn` is not supported for ink! functions",)
        }
        if let Some(abi) = sig.abi {
            bail!(abi, "specifying ABI is not allowed in ink! functions",)
        }
        if let Some(variadic) = sig.variadic {
            bail!(
                variadic,
                "variadic functions are not allowed as ink! functions",
            )
        }
        if sig.inputs.is_empty() {
            bail!(
492
                sig,
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
                "`&self` or `&mut self` is mandatory for ink! functions",
            )
        }
        let inputs = sig
            .inputs
            .iter()
            .cloned()
            .map(ir::FnArg::try_from)
            .collect::<Result<Punctuated<ir::FnArg, Token![,]>>>()?;
        if let ir::FnArg::Typed(ident_type) = &inputs[0] {
            bail_span!(
                ident_type.span(),
                "first argument of ink! functions must be `&self` or `&mut self`",
            )
        }
        for input in inputs.iter().skip(1) {
            if let ir::FnArg::Receiver(receiver) = input {
                bail!(
                    receiver,
                    "unexpected `self` argument found for ink! function",
                )
            }
        }
        Ok(ir::Signature {
            fn_token: sig.fn_token,
            ident: sig.ident,
            generics: sig.generics,
            paren_token: sig.paren_token,
            inputs,
            output: sig.output,
        })
    }
}

impl TryFrom<syn::FnArg> for ir::FnArg {
    type Error = syn::Error;

    fn try_from(fn_arg: syn::FnArg) -> Result<Self> {
        match fn_arg {
            syn::FnArg::Receiver(receiver) => Ok(ir::FnArg::Receiver(receiver)),
            syn::FnArg::Typed(pat_type) => {
                match *pat_type.pat {
                    syn::Pat::Ident(pat_ident) => {
                        if let Some(by_ref) = pat_ident.by_ref {
                            bail!(
                            by_ref,
                            "`ref` modifier is unsupported for ink! function arguments",
                        )
                        }
                        Ok(ir::FnArg::Typed(ir::IdentType {
                            attrs: pat_ident.attrs,
                            ident: pat_ident.ident,
                            colon_token: pat_type.colon_token,
                            ty: *pat_type.ty,
                        }))
                    }
                    unsupported => {
                        bail!(
                    unsupported,
                    "encountered unsupported function argument syntax for ink! function",
                )
                    }
                }
            }
        }
    }
}

impl TryFrom<syn::Item> for ir::Item {
    type Error = syn::Error;

    fn try_from(item: syn::Item) -> Result<Self> {
        match item {
            syn::Item::Impl(item_impl) => {
                // An ink! `impl` block is identified by having `#[ink(..)]`
                // markers on at least one function or having an `#[ink(impl)]`
                // marker on itself.
                let has_ink_marker_on_impl = utils::has_ink_attributes(&item_impl.attrs);
                // Queries all methods in the `impl` block for `#[ink(..)]` markers.
                let has_ink_marker_on_fns = item_impl
                    .items
                    .iter()
                    .filter_map(|impl_item| {
                        match impl_item {
                            syn::ImplItem::Method(method) => Some(method),
                            _ => None,
                        }
                    })
                    .filter(|method| utils::has_ink_attributes(&method.attrs))
                    .count()
                    > 0;
                if has_ink_marker_on_impl || has_ink_marker_on_fns {
                    ir::ItemImpl::try_from(item_impl)
                        .map(Into::into)
                        .map(ir::Item::Ink)
                } else {
                    Ok(ir::RustItem::from(syn::Item::Impl(item_impl)).into())
                }
            }
            syn::Item::Struct(item_struct) => {
593
                let markers = utils::filter_map_ink_attributes(&item_struct.attrs)
594
                    .collect::<Vec<_>>();
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
                if markers.is_empty() {
                    return Ok(ir::RustItem::from(syn::Item::Struct(item_struct)).into())
                }
                let event_marker =
                    markers.iter().position(|marker| marker.is_simple("event"));
                let storage_marker =
                    markers.iter().position(|marker| marker.is_simple("storage"));

                match (storage_marker, event_marker) {
                    (Some(_storage_marker), None) => {
                        ir::ItemStorage::try_from(item_struct)
                            .map(Into::into)
                            .map(ir::Item::Ink)
                    },
                    (None, Some(_event_marker)) => {
                        ir::ItemEvent::try_from(item_struct)
                            .map(Into::into)
                            .map(ir::Item::Ink)
                    },
                    (None, None) => {
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
                        Err(markers
                            .iter()
                            .map(|marker| {
                                format_err_span!(
                                    marker.span(),
                                    "unsupported ink! marker for struct")
                            })
                            .fold(
                                format_err!(
                                    item_struct,
                                    "encountered unsupported ink! markers for struct",
                                ),
                                |mut err1, err2| {
                                err1.combine(err2);
                                err1
                            })
                        )
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
                    }
                    (Some(storage_marker), Some(event_marker)) => {
                        // Special case: We have both #[ink(storage)] and #[ink(event)].
                        //               This is treated as error but depending on the
                        //               order in which the markers have been provided
                        //               we either treat it as storage or event definition.
                        //
                        // We take whatever ink! marker was provided first.
                        if storage_marker < event_marker {
                            ir::ItemStorage::try_from(item_struct)
                                .map(Into::into)
                                .map(ir::Item::Ink)
                        } else {
                            ir::ItemEvent::try_from(item_struct)
                                .map(Into::into)
                                .map(ir::Item::Ink)
                        }
                    }
650 651 652 653 654 655 656 657 658 659 660
                }
            }
            rust_item => Ok(ir::Item::Rust(rust_item.into())),
        }
    }
}

/// Split the storage, the events and functions out of the general contract items vector.
///
/// # Erros
///
661
/// - When there is not exactly one storage struct.
662 663 664 665 666 667 668 669 670 671 672
/// - When a contract item is invalid.
fn split_items(
    items: Vec<ir::InkItem>,
) -> Result<(ir::ItemStorage, Vec<ir::ItemEvent>, Vec<ir::Function>)> {
    let (mut storages, non_storage_items): (Vec<ir::ItemStorage>, Vec<ir::InkItem>) =
        items.into_iter().partition_map(|item| {
            match item {
                ir::InkItem::Storage(item_storage) => Either::Left(item_storage),
                other => Either::Right(other),
            }
        });
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    let storage = match storages.len() {
        0 => {
            Err(format_err_span!(
                Span::call_site(),
                "no #[ink(storage)] struct found but expected exactly 1"
            ))
        }
        1 => {
            Ok(storages
                .pop()
                .expect("there must be exactly one storage in `storages`"))
        }
        n => {
            Err(storages
                .iter()
                .map(|storage| {
                    format_err!(storage.ident, "{} conflicting storage struct found", n)
                })
                .fold1(|mut err1, err2| {
                    err1.combine(err2);
                    err1
                })
                .expect("there must be at least 2 conflicting storages; qed"))
        }
697 698 699 700 701 702 703 704 705 706 707 708 709
    }?;
    let (events, impl_blocks): (Vec<ir::ItemEvent>, Vec<ir::ItemImpl>) =
        non_storage_items.into_iter().partition_map(|item| {
            match item {
                ir::InkItem::Event(item_event) => Either::Left(item_event),
                ir::InkItem::Impl(item_impl) => Either::Right(item_impl),
                ir::InkItem::Storage(_) => {
                    unreachable!(
                        "we should not have any storages left at this point; qed"
                    )
                }
            }
        });
710 711 712 713 714 715 716 717 718
    let storage_ident = &storage.ident;
    for item_impl in &impl_blocks {
        if item_impl.self_ty != storage_ident.to_string() {
            bail!(
                item_impl.self_ty,
                "ink! impl blocks need to be implemented for the #[ink(storage)] struct"
            )
        }
    }
719 720 721 722 723 724 725
    let functions = impl_blocks
        .into_iter()
        .map(|impl_block| impl_block.functions)
        .flatten()
        .collect::<Vec<_>>();
    Ok((storage, events, functions))
}