Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let LSP work will with code generated by macros #5665

Merged
merged 8 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,25 @@
file_id: FileId,
context: &HirContext,
) -> Result<SortedModule, (MacroError, FileId)> {
let empty_spans = context.def_interner.is_in_lsp_mode();

// Usage -> mut ast -> aztec_library::transform(&mut ast)
// Covers all functions in the ast
for submodule in ast.submodules.iter_mut().filter(|submodule| submodule.is_contract) {
if transform_module(&file_id, &mut submodule.contents, submodule.name.0.contents.as_str())
.map_err(|err| (err.into(), file_id))?
if transform_module(
&file_id,
&mut submodule.contents,
submodule.name.0.contents.as_str(),
empty_spans,
)
.map_err(|err| (err.into(), file_id))?
{
check_for_aztec_dependency(crate_id, context)?;
}
}

generate_event_impls(&mut ast).map_err(|err| (err.into(), file_id))?;
generate_note_interface_impl(&mut ast).map_err(|err| (err.into(), file_id))?;
generate_event_impls(&mut ast, empty_spans).map_err(|err| (err.into(), file_id))?;
generate_note_interface_impl(&mut ast, empty_spans).map_err(|err| (err.into(), file_id))?;

Ok(ast)
}
Expand All @@ -85,6 +92,7 @@
file_id: &FileId,
module: &mut SortedModule,
module_name: &str,
empty_spans: bool,
) -> Result<bool, AztecMacroError> {
let mut has_transformed_module = false;

Expand All @@ -99,7 +107,7 @@
if !check_for_storage_implementation(module, storage_struct_name) {
generate_storage_implementation(module, storage_struct_name)?;
}
generate_storage_layout(module, storage_struct_name.clone(), module_name)?;
generate_storage_layout(module, storage_struct_name.clone(), module_name, empty_spans)?;
}

let has_initializer = module.functions.iter().any(|func| {
Expand All @@ -126,7 +134,7 @@
} else if is_custom_attribute(&secondary_attribute, "aztec(initializer)") {
is_initializer = true;
insert_init_check = false;
} else if is_custom_attribute(&secondary_attribute, "aztec(noinitcheck)") {

Check warning on line 137 in aztec_macros/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (noinitcheck)
insert_init_check = false;
} else if is_custom_attribute(&secondary_attribute, "aztec(internal)") {
is_internal = true;
Expand All @@ -144,7 +152,7 @@
let stub_src = stub_function(fn_type, func, is_static);
stubs.push((stub_src, Location { file: *file_id, span: func.name_ident().span() }));

export_fn_abi(&mut module.types, func)?;
export_fn_abi(&mut module.types, func, empty_spans)?;
transform_function(
fn_type,
func,
Expand Down Expand Up @@ -200,7 +208,7 @@
});
}

generate_contract_interface(module, module_name, &stubs, storage_defined)?;
generate_contract_interface(module, module_name, &stubs, storage_defined, empty_spans)?;
}

Ok(has_transformed_module)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
use noirc_frontend::{
graph::CrateId,
macros_api::{FileId, HirContext},
parse_program, Type,
Type,
};

use crate::utils::parse_utils::parse_program;
use crate::utils::{
errors::AztecMacroError,
hir_utils::{
collect_crate_functions, collect_traits, fetch_notes, get_contract_module_data,
get_global_numberic_const, get_serialized_length, inject_fn,

Check warning on line 14 in aztec_macros/src/transforms/compute_note_hash_and_optionally_a_nullifier.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (numberic)
},
};

Expand All @@ -19,9 +20,9 @@
crate_id: &CrateId,
context: &HirContext,
) -> bool {
collect_crate_functions(crate_id, context).iter().any(|funct_id| {

Check warning on line 23 in aztec_macros/src/transforms/compute_note_hash_and_optionally_a_nullifier.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (funct)
let func_data = context.def_interner.function_meta(funct_id);

Check warning on line 24 in aztec_macros/src/transforms/compute_note_hash_and_optionally_a_nullifier.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (funct)
let func_name = context.def_interner.function_name(funct_id);

Check warning on line 25 in aztec_macros/src/transforms/compute_note_hash_and_optionally_a_nullifier.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (funct)
func_name == "compute_note_hash_and_optionally_a_nullifier"
&& func_data.parameters.len() == 6
&& func_data.parameters.0.first().is_some_and(| (_, typ, _) | match typ {
Expand Down Expand Up @@ -67,7 +68,7 @@
let traits: Vec<_> = collect_traits(context);

// Get MAX_NOTE_FIELDS_LENGTH global to check if the notes in our contract are too long.
let max_note_length_const = get_global_numberic_const(context, "MAX_NOTE_FIELDS_LENGTH")

Check warning on line 71 in aztec_macros/src/transforms/compute_note_hash_and_optionally_a_nullifier.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (numberic)
.map_err(|err| {
(
AztecMacroError::CouldNotImplementComputeNoteHashAndOptionallyANullifier {
Expand Down Expand Up @@ -125,8 +126,12 @@
notes_and_lengths.iter().map(|(note_type, _)| note_type.clone()).collect::<Vec<_>>();

// We can now generate a version of compute_note_hash_and_optionally_a_nullifier tailored for the contract in this crate.
let func =
generate_compute_note_hash_and_optionally_a_nullifier(&note_types, max_note_length);
let empty_spans = context.def_interner.is_in_lsp_mode();
let func = generate_compute_note_hash_and_optionally_a_nullifier(
&note_types,
max_note_length,
empty_spans,
);

// And inject the newly created function into the contract.

Expand All @@ -149,11 +154,12 @@
fn generate_compute_note_hash_and_optionally_a_nullifier(
note_types: &[String],
max_note_length: u128,
empty_spans: bool,
) -> NoirFunction {
let function_source =
generate_compute_note_hash_and_optionally_a_nullifier_source(note_types, max_note_length);

let (function_ast, errors) = parse_program(&function_source);
let (function_ast, errors) = parse_program(&function_source, empty_spans);
if !errors.is_empty() {
dbg!(errors.clone());
}
Expand Down
5 changes: 3 additions & 2 deletions aztec_macros/src/transforms/contract_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use noirc_frontend::ast::{Ident, NoirFunction, UnresolvedTypeData};
use noirc_frontend::{
graph::CrateId,
macros_api::{FieldElement, FileId, HirContext, HirExpression, HirLiteral, HirStatement},
parse_program,
parser::SortedModule,
Type,
};

use tiny_keccak::{Hasher, Keccak};

use crate::utils::parse_utils::parse_program;
use crate::utils::{
errors::AztecMacroError,
hir_utils::{collect_crate_structs, get_contract_module_data, signature_of_type},
Expand Down Expand Up @@ -203,6 +203,7 @@ pub fn generate_contract_interface(
module_name: &str,
stubs: &[(String, Location)],
has_storage_layout: bool,
empty_spans: bool,
) -> Result<(), AztecMacroError> {
let storage_layout_getter = format!(
"#[contract_library_method]
Expand Down Expand Up @@ -253,7 +254,7 @@ pub fn generate_contract_interface(
if has_storage_layout { format!("#[contract_library_method]\n{}", storage_layout_getter) } else { "".to_string() }
);

let (contract_interface_ast, errors) = parse_program(&contract_interface);
let (contract_interface_ast, errors) = parse_program(&contract_interface, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotGenerateContractInterface { secondary_message: Some("Failed to parse Noir macro code during contract interface generation. This is either a bug in the compiler or the Noir macro code".to_string()), });
Expand Down
60 changes: 40 additions & 20 deletions aztec_macros/src/transforms/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ use noirc_frontend::token::SecondaryAttribute;
use noirc_frontend::{
graph::CrateId,
macros_api::{FileId, HirContext},
parse_program,
parser::SortedModule,
};

use crate::utils::hir_utils::collect_crate_structs;
use crate::utils::parse_utils::parse_program;
use crate::utils::{ast_utils::is_custom_attribute, errors::AztecMacroError};

// Automatic implementation of most of the methods in the EventInterface trait, guiding the user with meaningful error messages in case some
// methods must be implemented manually.
pub fn generate_event_impls(module: &mut SortedModule) -> Result<(), AztecMacroError> {
pub fn generate_event_impls(
module: &mut SortedModule,
empty_spans: bool,
) -> Result<(), AztecMacroError> {
// Find structs annotated with #[aztec(event)]
// Why doesn't this work ? Events are not tagged and do not appear, it seems only going through the submodule works
// let annotated_event_structs = module
Expand Down Expand Up @@ -56,28 +59,39 @@ pub fn generate_event_impls(module: &mut SortedModule) -> Result<(), AztecMacroE
));
}

let mut event_interface_trait_impl =
generate_trait_impl_stub_event_interface(event_type.as_str(), event_byte_len)?;
let mut event_interface_trait_impl = generate_trait_impl_stub_event_interface(
event_type.as_str(),
event_byte_len,
empty_spans,
)?;
event_interface_trait_impl.items.push(TraitImplItem::Function(
generate_fn_get_event_type_id(event_type.as_str(), event_len)?,
generate_fn_get_event_type_id(event_type.as_str(), event_len, empty_spans)?,
));
event_interface_trait_impl.items.push(TraitImplItem::Function(
generate_fn_private_to_be_bytes(event_type.as_str(), event_byte_len)?,
generate_fn_private_to_be_bytes(event_type.as_str(), event_byte_len, empty_spans)?,
));
event_interface_trait_impl.items.push(TraitImplItem::Function(
generate_fn_to_be_bytes(event_type.as_str(), event_byte_len)?,
generate_fn_to_be_bytes(event_type.as_str(), event_byte_len, empty_spans)?,
));
event_interface_trait_impl
.items
.push(TraitImplItem::Function(generate_fn_emit(event_type.as_str())?));
.push(TraitImplItem::Function(generate_fn_emit(event_type.as_str(), empty_spans)?));
submodule.contents.trait_impls.push(event_interface_trait_impl);

let serialize_trait_impl =
generate_trait_impl_serialize(event_type.as_str(), event_len, &event_fields)?;
let serialize_trait_impl = generate_trait_impl_serialize(
event_type.as_str(),
event_len,
&event_fields,
empty_spans,
)?;
submodule.contents.trait_impls.push(serialize_trait_impl);

let deserialize_trait_impl =
generate_trait_impl_deserialize(event_type.as_str(), event_len, &event_fields)?;
let deserialize_trait_impl = generate_trait_impl_deserialize(
event_type.as_str(),
event_len,
&event_fields,
empty_spans,
)?;
submodule.contents.trait_impls.push(deserialize_trait_impl);
}
}
Expand All @@ -88,6 +102,7 @@ pub fn generate_event_impls(module: &mut SortedModule) -> Result<(), AztecMacroE
fn generate_trait_impl_stub_event_interface(
event_type: &str,
byte_length: u32,
empty_spans: bool,
) -> Result<NoirTraitImpl, AztecMacroError> {
let byte_length_without_randomness = byte_length - 32;
let trait_impl_source = format!(
Expand All @@ -98,7 +113,7 @@ impl dep::aztec::event::event_interface::EventInterface<{byte_length}, {byte_len
)
.to_string();

let (parsed_ast, errors) = parse_program(&trait_impl_source);
let (parsed_ast, errors) = parse_program(&trait_impl_source, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementEventInterface {
Expand All @@ -116,6 +131,7 @@ fn generate_trait_impl_serialize(
event_type: &str,
event_len: u32,
event_fields: &[(String, String)],
empty_spans: bool,
) -> Result<NoirTraitImpl, AztecMacroError> {
let field_names = event_fields
.iter()
Expand Down Expand Up @@ -143,7 +159,7 @@ fn generate_trait_impl_serialize(
)
.to_string();

let (parsed_ast, errors) = parse_program(&trait_impl_source);
let (parsed_ast, errors) = parse_program(&trait_impl_source, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementEventInterface {
Expand All @@ -161,6 +177,7 @@ fn generate_trait_impl_deserialize(
event_type: &str,
event_len: u32,
event_fields: &[(String, String)],
empty_spans: bool,
) -> Result<NoirTraitImpl, AztecMacroError> {
let field_names: Vec<String> = event_fields
.iter()
Expand Down Expand Up @@ -189,7 +206,7 @@ fn generate_trait_impl_deserialize(
)
.to_string();

let (parsed_ast, errors) = parse_program(&trait_impl_source);
let (parsed_ast, errors) = parse_program(&trait_impl_source, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementEventInterface {
Expand All @@ -206,6 +223,7 @@ fn generate_trait_impl_deserialize(
fn generate_fn_get_event_type_id(
event_type: &str,
field_length: u32,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let from_signature_input =
std::iter::repeat("Field").take(field_length as usize).collect::<Vec<_>>().join(",");
Expand All @@ -218,7 +236,7 @@ fn generate_fn_get_event_type_id(
)
.to_string();

let (function_ast, errors) = parse_program(&function_source);
let (function_ast, errors) = parse_program(&function_source, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementEventInterface {
Expand All @@ -235,6 +253,7 @@ fn generate_fn_get_event_type_id(
fn generate_fn_private_to_be_bytes(
event_type: &str,
byte_length: u32,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let function_source = format!(
"
Expand Down Expand Up @@ -264,7 +283,7 @@ fn generate_fn_private_to_be_bytes(
)
.to_string();

let (function_ast, errors) = parse_program(&function_source);
let (function_ast, errors) = parse_program(&function_source, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementEventInterface {
Expand All @@ -281,6 +300,7 @@ fn generate_fn_private_to_be_bytes(
fn generate_fn_to_be_bytes(
event_type: &str,
byte_length: u32,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let byte_length_without_randomness = byte_length - 32;
let function_source = format!(
Expand Down Expand Up @@ -308,7 +328,7 @@ fn generate_fn_to_be_bytes(
")
.to_string();

let (function_ast, errors) = parse_program(&function_source);
let (function_ast, errors) = parse_program(&function_source, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementEventInterface {
Expand All @@ -322,7 +342,7 @@ fn generate_fn_to_be_bytes(
Ok(noir_fn)
}

fn generate_fn_emit(event_type: &str) -> Result<NoirFunction, AztecMacroError> {
fn generate_fn_emit(event_type: &str, empty_spans: bool) -> Result<NoirFunction, AztecMacroError> {
let function_source = format!(
"
fn emit<Env>(self: {event_type}, _emit: fn[Env](Self) -> ()) {{
Expand All @@ -332,7 +352,7 @@ fn generate_fn_emit(event_type: &str) -> Result<NoirFunction, AztecMacroError> {
)
.to_string();

let (function_ast, errors) = parse_program(&function_source);
let (function_ast, errors) = parse_program(&function_source, empty_spans);
if !errors.is_empty() {
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementEventInterface {
Expand Down
6 changes: 4 additions & 2 deletions aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use noirc_frontend::ast::{
UnresolvedTypeData, Visibility,
};

use noirc_frontend::{macros_api::FieldElement, parse_program};
use noirc_frontend::macros_api::FieldElement;

use crate::utils::ast_utils::member_access;
use crate::utils::parse_utils::parse_program;
use crate::{
chained_dep, chained_path,
utils::{
Expand Down Expand Up @@ -132,6 +133,7 @@ pub fn transform_function(
pub fn export_fn_abi(
types: &mut Vec<NoirStruct>,
func: &NoirFunction,
empty_spans: bool,
) -> Result<(), AztecMacroError> {
let mut parameters_struct_source: Option<&str> = None;

Expand Down Expand Up @@ -198,7 +200,7 @@ pub fn export_fn_abi(

program.push_str(&export_struct_source);

let (ast, errors) = parse_program(&program);
let (ast, errors) = parse_program(&program, empty_spans);
if !errors.is_empty() {
return Err(AztecMacroError::CouldNotExportFunctionAbi {
span: None,
Expand Down
Loading
Loading