Skip to content

Commit

Permalink
fix ensure_single_gprogram per crate (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich authored Aug 12, 2024
1 parent a1f1b21 commit 27af47f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions rs/macros/core/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use parity_scale_codec::Encode;
use proc_macro2::{Span, TokenStream as TokenStream2};
use proc_macro_error::abort;
use quote::quote;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, env};
use syn::{
parse_quote, spanned::Spanned, Ident, ImplItem, ImplItemFn, ItemImpl, Path, Receiver,
ReturnType, Type, TypePath, Visibility,
};

mod args;

/// Static Span of Program `impl` block
static mut PROGRAM_SPAN: Option<Span> = None;
/// Static Spans of Program `impl` block
static mut PROGRAM_SPANS: BTreeMap<String, Span> = BTreeMap::new();

pub fn gprogram(args: TokenStream2, program_impl_tokens: TokenStream2) -> TokenStream2 {
let program_impl = parse_gprogram_impl(program_impl_tokens);
Expand Down Expand Up @@ -53,13 +53,14 @@ fn parse_gprogram_impl(program_impl_tokens: TokenStream2) -> ItemImpl {
}

fn ensure_single_gprogram(program_impl: &ItemImpl) {
if unsafe { PROGRAM_SPAN }.is_some() {
let crate_name = env::var("CARGO_CRATE_NAME").unwrap_or("crate".to_string());
if unsafe { PROGRAM_SPANS.get(&crate_name) }.is_some() {
abort!(
program_impl,
"multiple `program` attributes are not allowed"
)
}
unsafe { PROGRAM_SPAN = Some(program_impl.span()) };
unsafe { PROGRAM_SPANS.insert(crate_name, program_impl.span()) };
}

fn gen_gprogram_impl(program_impl: ItemImpl, program_args: ProgramArgs) -> TokenStream2 {
Expand Down

0 comments on commit 27af47f

Please sign in to comment.