Skip to content

Commit 14429ac

Browse files
committed
fix tests
1 parent 6706b96 commit 14429ac

File tree

11 files changed

+47
-64
lines changed

11 files changed

+47
-64
lines changed

crates/env/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub trait Event: scale::Encode + GetSignatureTopic {
217217
/// The unique signature topic of the event. `None` for anonymous events.
218218
///
219219
/// Usually this is calculated using the `#[derive(ink::Event)]` derive, which by
220-
/// default calculates this as `blake2b("Event(field1_type,field2_type)")
220+
/// default calculates this as `blake2b("Event(field1_type,field2_type)")`
221221
pub trait GetSignatureTopic {
222222
/// Retrieve the signature topic
223223
fn signature_topic() -> Option<[u8; 32]>;

crates/ink/codegen/src/generator/event.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ use derive_more::From;
1717
use ir::HexLiteral;
1818
use proc_macro2::TokenStream as TokenStream2;
1919
use quote::quote;
20+
use syn::spanned::Spanned;
2021

21-
/// Generates code for the storage item.
22+
/// Generates code for the event item.
2223
#[derive(From, Copy, Clone)]
2324
pub struct Event<'a> {
2425
/// The storage item to generate code for.
2526
item: &'a ir::Event,
2627
}
2728

2829
impl GenerateCode for Event<'_> {
29-
/// Generates ink! storage item code.
30+
/// Generates ink! event item code.
3031
fn generate_code(&self) -> TokenStream2 {
3132
let item = self.item.item();
3233
let anonymous = self
@@ -35,10 +36,13 @@ impl GenerateCode for Event<'_> {
3536
.then(|| quote::quote! { #[ink(anonymous)] });
3637

3738
let signature_topic = self.generate_signature_topic();
39+
let cfg_attrs = self.item.get_cfg_attrs(item.span());
3840

3941
quote::quote! (
42+
#( #cfg_attrs )*
4043
#signature_topic
4144

45+
#( #cfg_attrs )*
4246
#[cfg_attr(feature = "std", derive(::ink::EventMetadata))]
4347
#[derive(::ink::Event)]
4448
#[::ink::scale_derive(Encode, Decode)]

crates/ink/ir/src/ir/attrs.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::{
4343
},
4444
};
4545

46-
use super::SignatureTopic;
46+
use super::SignatureTopicArg;
4747

4848
/// An extension trait for [`syn::Attribute`] in order to query for documentation.
4949
pub trait IsDocAttribute {
@@ -287,7 +287,7 @@ impl InkAttribute {
287287
}
288288

289289
/// Returns the signature topic of the ink! attribute if any.
290-
pub fn signature_topic(&self) -> Option<SignatureTopic> {
290+
pub fn signature_topic(&self) -> Option<SignatureTopicArg> {
291291
self.args().find_map(|arg| {
292292
if let ir::AttributeArg::SignatureTopic(topic) = arg.kind() {
293293
return Some(*topic);
@@ -376,7 +376,7 @@ pub enum AttributeArgKind {
376376
/// `#[ink(selector = 0xDEADBEEF)]`
377377
Selector,
378378
/// `#[ink(signature_topic = "DEADBEEF")]`
379-
SignatureTopic,
379+
SignatureTopicArg,
380380
/// `#[ink(function = N: u16)]`
381381
Function,
382382
/// `#[ink(namespace = "my_namespace")]`
@@ -433,7 +433,7 @@ pub enum AttributeArg {
433433
/// that is invoked if no other ink! message matches a given selector.
434434
Selector(SelectorOrWildcard),
435435
/// `#[ink(signature_topic = "DEADBEEF")]`
436-
SignatureTopic(SignatureTopic),
436+
SignatureTopic(SignatureTopicArg),
437437
/// `#[ink(namespace = "my_namespace")]`
438438
///
439439
/// Applied on ink! trait implementation blocks to disambiguate other trait
@@ -478,7 +478,7 @@ impl core::fmt::Display for AttributeArgKind {
478478
Self::Selector => {
479479
write!(f, "selector = S:[u8; 4] || _")
480480
}
481-
Self::SignatureTopic => {
481+
Self::SignatureTopicArg => {
482482
write!(f, "signature_topic = S:[u8; 32]")
483483
}
484484
Self::Function => {
@@ -505,7 +505,7 @@ impl AttributeArg {
505505
Self::Constructor => AttributeArgKind::Constructor,
506506
Self::Payable => AttributeArgKind::Payable,
507507
Self::Selector(_) => AttributeArgKind::Selector,
508-
Self::SignatureTopic(_) => AttributeArgKind::SignatureTopic,
508+
Self::SignatureTopic(_) => AttributeArgKind::SignatureTopicArg,
509509
Self::Function(_) => AttributeArgKind::Function,
510510
Self::Namespace(_) => AttributeArgKind::Namespace,
511511
Self::Implementation => AttributeArgKind::Implementation,
@@ -945,7 +945,7 @@ impl Parse for AttributeFrag {
945945
.map(AttributeArg::Selector)
946946
}
947947
"signature_topic" => {
948-
SignatureTopic::try_from(&name_value.value)
948+
SignatureTopicArg::try_from(&name_value.value)
949949
.map(AttributeArg::SignatureTopic)
950950
}
951951
"namespace" => {
@@ -1544,7 +1544,7 @@ mod tests {
15441544
#[ink(signature_topic = #s)]
15451545
},
15461546
Ok(test::Attribute::Ink(vec![AttributeArg::SignatureTopic(
1547-
SignatureTopic::from(&bytes),
1547+
SignatureTopicArg::from(&bytes),
15481548
)])),
15491549
);
15501550
}

crates/ink/ir/src/ir/event/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
utils::duplicate_config_err,
1818
};
1919

20-
use super::SignatureTopic;
20+
use super::SignatureTopicArg;
2121

2222
/// The configuration arguments to the `#[ink::event(..)]` attribute macro.
2323
#[derive(Debug, PartialEq, Eq)]
@@ -36,7 +36,7 @@ impl TryFrom<ast::AttributeArgs> for EventConfig {
3636

3737
fn try_from(args: ast::AttributeArgs) -> Result<Self, Self::Error> {
3838
let mut anonymous: Option<syn::LitBool> = None;
39-
let mut signature_topic: Option<SignatureTopic> = None;
39+
let mut signature_topic: Option<SignatureTopicArg> = None;
4040
for arg in args.into_iter() {
4141
if arg.name.is_ident("anonymous") {
4242
if let Some(lit_bool) = anonymous {
@@ -66,7 +66,7 @@ impl TryFrom<ast::AttributeArgs> for EventConfig {
6666
"event",
6767
));
6868
}
69-
signature_topic = Some(SignatureTopic::try_from(&arg.value)?);
69+
signature_topic = Some(SignatureTopicArg::try_from(&arg.value)?);
7070
} else {
7171
return Err(format_err_spanned!(
7272
arg,
@@ -84,7 +84,7 @@ impl TryFrom<ast::AttributeArgs> for EventConfig {
8484

8585
impl EventConfig {
8686
/// Construct a new [`EventConfig`].
87-
pub fn new(anonymous: bool, signature_topic: Option<SignatureTopic>) -> Self {
87+
pub fn new(anonymous: bool, signature_topic: Option<SignatureTopicArg>) -> Self {
8888
Self {
8989
anonymous,
9090
signature_topic: signature_topic.map(|s| s.signature_topic()),

crates/ink/ir/src/ir/event/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ mod config;
1616
mod signature_topic;
1717

1818
use config::EventConfig;
19-
use proc_macro2::TokenStream as TokenStream2;
19+
use proc_macro2::{
20+
Span,
21+
TokenStream as TokenStream2,
22+
};
2023
use quote::ToTokens;
2124
use syn::spanned::Spanned as _;
2225

2326
use crate::{
2427
error::ExtError,
2528
ir,
29+
utils::extract_cfg_attributes,
2630
};
2731

28-
pub use signature_topic::SignatureTopic;
32+
pub use signature_topic::SignatureTopicArg;
2933

3034
/// A checked ink! event with its configuration.
3135
#[derive(Debug, PartialEq, Eq)]
@@ -102,6 +106,11 @@ impl Event {
102106
pub fn signature_topic(&self) -> Option<[u8; 32]> {
103107
self.config.signature_topic()
104108
}
109+
110+
/// Returns a list of `cfg` attributes if any.
111+
pub fn get_cfg_attrs(&self, span: Span) -> Vec<TokenStream2> {
112+
extract_cfg_attributes(&self.item.attrs, span)
113+
}
105114
}
106115

107116
impl ToTokens for Event {

crates/ink/ir/src/ir/event/signature_topic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ use crate::ast;
2020
///
2121
/// Calculated with `blake2b("Event(field1_type,field2_type)")`.
2222
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
23-
pub struct SignatureTopic {
23+
pub struct SignatureTopicArg {
2424
topic: [u8; 32],
2525
}
2626

27-
impl SignatureTopic {
27+
impl SignatureTopicArg {
2828
pub fn signature_topic(&self) -> [u8; 32] {
2929
self.topic
3030
}
3131
}
3232

33-
impl From<&[u8; 32]> for SignatureTopic {
33+
impl From<&[u8; 32]> for SignatureTopicArg {
3434
fn from(value: &[u8; 32]) -> Self {
3535
Self { topic: *value }
3636
}
3737
}
3838

39-
impl TryFrom<&ast::MetaValue> for SignatureTopic {
39+
impl TryFrom<&ast::MetaValue> for SignatureTopicArg {
4040
type Error = syn::Error;
4141

4242
fn try_from(value: &ast::MetaValue) -> Result<Self, Self::Error> {

crates/ink/ir/src/ir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub use self::{
7272
contract::Contract,
7373
event::{
7474
Event,
75-
SignatureTopic,
75+
SignatureTopicArg,
7676
},
7777
ink_test::InkTest,
7878
item::{

crates/ink/macro/src/tests/event.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ fn unit_struct_works() {
2828
event_derive {
2929
#[derive(scale::Encode)]
3030
struct UnitStruct;
31-
32-
impl ::ink::env::GetSignatureTopic for UnitStruct {
33-
fn signature_topic() -> Option<[u8; 32]> {
34-
Some([0; 32])
35-
}
36-
}
3731
}
3832
expands to {
3933
const _: () = {
@@ -59,7 +53,7 @@ fn unit_struct_works() {
5953
}
6054
}
6155
};
62-
}
56+
} no_build
6357
}
6458
}
6559

@@ -108,12 +102,6 @@ fn struct_with_fields_no_topics() {
108102
field_2: u64,
109103
field_3: u128,
110104
}
111-
112-
impl ::ink::env::GetSignatureTopic for Event {
113-
fn signature_topic() -> Option<[u8; 32]> {
114-
Some([0; 32])
115-
}
116-
}
117105
}
118106
expands to {
119107
const _: () = {
@@ -139,7 +127,7 @@ fn struct_with_fields_no_topics() {
139127
}
140128
}
141129
};
142-
}
130+
} no_build
143131
}
144132
}
145133

@@ -174,7 +162,7 @@ fn struct_with_fields_and_some_topics() {
174162
Event { field_2 : __binding_1 , field_3 : __binding_2 , .. } => {
175163
builder
176164
.build::<Self>()
177-
.push_topic(<Self as GetSignatureTopic>::signature_topic().as_ref())
165+
.push_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic().as_ref())
178166
.push_topic(::ink::as_option!(__binding_1))
179167
.push_topic(::ink::as_option!(__binding_2))
180168
.finish()

crates/ink/macro/src/tests/event_metadata.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ fn unit_struct_works() {
2020
event_metadata_derive {
2121
#[derive(ink::Event, scale::Encode)]
2222
struct UnitStruct;
23-
24-
impl ::ink::env::GetSignatureTopic for UnitStruct {
25-
fn signature_topic() -> Option<[u8; 32]> {
26-
Some([0; 32])
27-
}
28-
}
29-
3023
}
3124
expands to {
3225
const _: () = {
@@ -48,7 +41,7 @@ fn unit_struct_works() {
4841
}
4942
}
5043
};
51-
}
44+
} no_build
5245
}
5346
}
5447

@@ -62,12 +55,6 @@ fn struct_with_fields_no_topics() {
6255
field_2: u64,
6356
field_3: u128,
6457
}
65-
66-
impl ::ink::env::GetSignatureTopic for Event {
67-
fn signature_topic() -> Option<[u8; 32]> {
68-
Some([0; 32])
69-
}
70-
}
7158
}
7259
expands to {
7360
const _: () = {
@@ -105,7 +92,7 @@ fn struct_with_fields_no_topics() {
10592
}
10693
}
10794
};
108-
}
95+
} no_build
10996
}
11097
}
11198

@@ -121,12 +108,6 @@ fn struct_with_fields_and_some_topics() {
121108
#[ink(topic)]
122109
field_3: u128,
123110
}
124-
125-
impl ::ink::env::GetSignatureTopic for Event {
126-
fn signature_topic() -> Option<[u8; 32]> {
127-
Some([0; 32])
128-
}
129-
}
130111
}
131112
expands to {
132113
const _: () = {
@@ -164,6 +145,6 @@ fn struct_with_fields_and_some_topics() {
164145
}
165146
}
166147
};
167-
}
148+
} no_build
168149
}
169150
}

crates/ink/macro/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::storage::{
2626
storage_layout_derive,
2727
};
2828

29+
#[cfg(test)]
2930
#[macro_export]
3031
macro_rules! test_derive {
3132
($name:path { $($i:tt)* } expands to { $($o:tt)* }) => {

0 commit comments

Comments
 (0)