From b6b0682fe5340994306e9c5ee95c2286f9038467 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 20 Feb 2025 13:30:26 +0000 Subject: [PATCH 1/3] fix: need for imports of ghost traits --- .../aztec-nr/address-note/src/address_note.nr | 3 --- .../aztec-nr/aztec/src/macros/utils.nr | 14 +++++++--- .../aztec-nr/value-note/src/value_note.nr | 3 --- .../src/subscription_note.nr | 3 --- .../src/types/card_note.nr | 3 --- .../nft_contract/src/types/nft_note.nr | 3 --- .../src/public_key_note.nr | 3 --- .../spam_contract/src/types/token_note.nr | 3 --- .../src/types/token_note.nr | 3 --- .../crates/types/src/meta/mod.nr | 27 +++++++++++++++++-- 10 files changed, 36 insertions(+), 29 deletions(-) diff --git a/noir-projects/aztec-nr/address-note/src/address_note.nr b/noir-projects/aztec-nr/address-note/src/address_note.nr index 9cf95850d662..d3e8fc4f5861 100644 --- a/noir-projects/aztec-nr/address-note/src/address_note.nr +++ b/noir-projects/aztec-nr/address-note/src/address_note.nr @@ -13,9 +13,6 @@ use dep::aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - // docs:start:address_note_def // docs:start:address_note_struct // Stores an address diff --git a/noir-projects/aztec-nr/aztec/src/macros/utils.nr b/noir-projects/aztec-nr/aztec/src/macros/utils.nr index 594362e3eb93..97b892ee3612 100644 --- a/noir-projects/aztec-nr/aztec/src/macros/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/macros/utils.nr @@ -256,9 +256,17 @@ pub(crate) comptime fn is_note(typ: Type) -> bool { }) } -/// Returns the typed expression of a trait method implementation. This is preferred over directly inlining with -/// `$typ::target_method()` in a quote, as direct inlining would result in missing import warnings in the generated -/// code (specifically, warnings that the trait implementation is not in scope). +/// Returns the typed expression of a trait method implementation. +/// +/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote, +/// as direct inlining would result in missing import warnings in the generated code (specifically, +/// warnings that the trait implementation is not in scope). +/// +/// # Note +/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate +/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context +/// of the protocol circuits types crate, making it impossible to compile code for traits from that crate +/// (e.g. NoteInterface). pub(crate) comptime fn get_trait_impl_method( typ: Type, target_trait: Quoted, diff --git a/noir-projects/aztec-nr/value-note/src/value_note.nr b/noir-projects/aztec-nr/value-note/src/value_note.nr index 7fc36ad89643..c70d425873c4 100644 --- a/noir-projects/aztec-nr/value-note/src/value_note.nr +++ b/noir-projects/aztec-nr/value-note/src/value_note.nr @@ -13,9 +13,6 @@ use dep::aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - pub(crate) global VALUE_NOTE_LEN: u32 = 3; // 3 plus a header. // docs:start:value-note-def diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr index 03dbc46e892e..3009cbf6a87e 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr @@ -8,9 +8,6 @@ use dep::aztec::{ protocol_types::{address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER}, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - #[note] pub struct SubscriptionNote { owner: AztecAddress, diff --git a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr index f582ca02473f..d7253738e053 100644 --- a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr +++ b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr @@ -9,9 +9,6 @@ use dep::aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - // docs:start:state_vars-CardNote global CARD_NOTE_LEN: u32 = 3; // 3 plus a header. diff --git a/noir-projects/noir-contracts/contracts/nft_contract/src/types/nft_note.nr b/noir-projects/noir-contracts/contracts/nft_contract/src/types/nft_note.nr index 86913add9100..90469e127669 100644 --- a/noir-projects/noir-contracts/contracts/nft_contract/src/types/nft_note.nr +++ b/noir-projects/noir-contracts/contracts/nft_contract/src/types/nft_note.nr @@ -10,9 +10,6 @@ use dep::aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - // docs:start:nft_note #[partial_note(quote { token_id})] #[derive(Eq)] diff --git a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr index 5f3cfbfe1dea..24e0971d1dc8 100644 --- a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr +++ b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr @@ -9,9 +9,6 @@ use aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - // Stores a public key composed of two fields // TODO: Do we need to include a nonce, in case we want to read/nullify/recreate with the same pubkey value? #[note] diff --git a/noir-projects/noir-contracts/contracts/spam_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/spam_contract/src/types/token_note.nr index 6bf71efb3224..78962d7029ed 100644 --- a/noir-projects/noir-contracts/contracts/spam_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/spam_contract/src/types/token_note.nr @@ -10,9 +10,6 @@ use dep::aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - trait OwnedNote { fn new(amount: U128, owner: AztecAddress) -> Self; fn get_amount(self) -> U128; diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr index b6a6e913a995..4efc3de70867 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr @@ -10,9 +10,6 @@ use dep::aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - trait OwnedNote { fn new(amount: U128, owner: AztecAddress) -> Self; fn get_amount(self) -> U128; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr b/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr index eaf7cd438131..9467b126a783 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr @@ -1,5 +1,26 @@ use super::traits::{Deserialize, Packable, Serialize}; +/// Returns the typed expression of a trait method implementation. +/// +/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote, +/// as direct inlining would result in missing import warnings in the generated code (specifically, +/// warnings that the trait implementation is not in scope). +/// +/// # Note +/// A copy of this function exists in `aztec-nr/aztec/src/macros/utils.nr`. We maintain separate copies +/// because importing it there from here would cause the `target_trait` to be interpreted in the context +/// of the protocol circuits types crate, making it impossible to compile code for traits from that crate +/// (e.g. NoteInterface). +comptime fn get_trait_impl_method( + typ: Type, + target_trait: Quoted, + target_method: Quoted, +) -> TypedExpr { + let trait_constraint = target_trait.as_trait_constraint(); + typ.get_trait_impl(trait_constraint).unwrap().methods().filter(|m| m.name() == target_method)[0] + .as_typed_expr() +} + /// Generates code that deserializes a struct, primitive type, array or string from a field array. /// /// # Parameters @@ -91,7 +112,8 @@ pub comptime fn generate_deserialize_from_fields( let packed_fields = packed_fields_quotes.join(quote {,}); // Now we call unpack on the type - result = quote { Packable::unpack([ $packed_fields ]) }; + let unpack_method = get_trait_impl_method(typ, quote { Packable<_> }, quote { unpack }); + result = quote { $unpack_method([ $packed_fields ]) }; consumed_counter = packed_len; } else if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() { @@ -476,9 +498,10 @@ pub comptime fn derive_packable_and_get_packed_len(s: StructDefinition) -> (Quot let field_packings = fields.join(quote {,}); let packed_len = fields.len(); + let packable_trait: TraitConstraint = quote { Packable<$packed_len> }.as_trait_constraint(); ( quote { - impl Packable<$packed_len> for $typ { + impl $packable_trait for $typ { fn pack(self) -> [Field; $packed_len] { $aux_vars_for_packing [ $field_packings ] From 8d617bd3d0ea89396de46a7a7f28c9b3cc22d827 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 20 Feb 2025 13:36:12 +0000 Subject: [PATCH 2/3] fix --- noir-projects/aztec-nr/uint-note/src/uint_note.nr | 3 --- 1 file changed, 3 deletions(-) diff --git a/noir-projects/aztec-nr/uint-note/src/uint_note.nr b/noir-projects/aztec-nr/uint-note/src/uint_note.nr index 0a8ca726aeda..9ab7254fabc1 100644 --- a/noir-projects/aztec-nr/uint-note/src/uint_note.nr +++ b/noir-projects/aztec-nr/uint-note/src/uint_note.nr @@ -10,9 +10,6 @@ use dep::aztec::{ }, }; -// TODO(#12008): Remove the need for the manual import of `Packable` trait here. This is a bug in macros. -use aztec::protocol_types::traits::Packable; - // docs:start:UintNote #[partial_note(quote {value})] #[derive(Eq, Serialize)] From 8cfed2cb9bb5f95a4ada0c518182cc2c4d2c5268 Mon Sep 17 00:00:00 2001 From: benesjan Date: Fri, 21 Feb 2025 08:55:48 +0000 Subject: [PATCH 3/3] comment fixes --- noir-projects/aztec-nr/aztec/src/macros/utils.nr | 2 +- .../noir-protocol-circuits/crates/types/src/meta/mod.nr | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/macros/utils.nr b/noir-projects/aztec-nr/aztec/src/macros/utils.nr index 97b892ee3612..aabd6be83fb2 100644 --- a/noir-projects/aztec-nr/aztec/src/macros/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/macros/utils.nr @@ -265,7 +265,7 @@ pub(crate) comptime fn is_note(typ: Type) -> bool { /// # Note /// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate /// copies because importing it here from there would cause the `target_trait` to be interpreted in the context -/// of the protocol circuits types crate, making it impossible to compile code for traits from that crate +/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate /// (e.g. NoteInterface). pub(crate) comptime fn get_trait_impl_method( typ: Type, diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr b/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr index 9467b126a783..0025d4658315 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr @@ -9,8 +9,7 @@ use super::traits::{Deserialize, Packable, Serialize}; /// # Note /// A copy of this function exists in `aztec-nr/aztec/src/macros/utils.nr`. We maintain separate copies /// because importing it there from here would cause the `target_trait` to be interpreted in the context -/// of the protocol circuits types crate, making it impossible to compile code for traits from that crate -/// (e.g. NoteInterface). +/// of this crate, making it impossible to compile code for traits from that crate (e.g. NoteInterface). comptime fn get_trait_impl_method( typ: Type, target_trait: Quoted,