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

chore: remove panic for unsupported types in aztec contract interface #4074

Merged
merged 3 commits into from
Jan 18, 2024
Merged
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
41 changes: 29 additions & 12 deletions aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum AztecMacroError {
AztecComputeNoteHashAndNullifierNotFound { span: Span },
AztecContractHasTooManyFunctions { span: Span },
AztecContractConstructorMissing { span: Span },
UnsupportedFunctionArgumentType { span: Span, typ: UnresolvedTypeData },
}

impl From<AztecMacroError> for MacroError {
Expand All @@ -65,6 +66,11 @@ impl From<AztecMacroError> for MacroError {
secondary_message: None,
span: Some(span),
},
AztecMacroError::UnsupportedFunctionArgumentType { span, typ } => MacroError {
primary_message: format!("Provided parameter type `{typ:?}` is not supported in Aztec contract interface"),
secondary_message: None,
span: Some(span),
},
}
}
}
Expand Down Expand Up @@ -341,11 +347,14 @@ fn transform_module(

for func in module.functions.iter_mut() {
for secondary_attribute in func.def.attributes.secondary.clone() {
let crate_graph = &context.crate_graph[crate_id];
if is_custom_attribute(&secondary_attribute, "aztec(private)") {
transform_function("Private", func, storage_defined);
transform_function("Private", func, storage_defined)
.map_err(|err| (err.into(), crate_graph.root_file_id))?;
has_transformed_module = true;
} else if is_custom_attribute(&secondary_attribute, "aztec(public)") {
transform_function("Public", func, storage_defined);
transform_function("Public", func, storage_defined)
.map_err(|err| (err.into(), crate_graph.root_file_id))?;
has_transformed_module = true;
}
}
Expand Down Expand Up @@ -384,7 +393,11 @@ fn transform_module(
/// - A new Input that is provided for a kernel app circuit, named: {Public/Private}ContextInputs
/// - Hashes all of the function input variables
/// - This instantiates a helper function
fn transform_function(ty: &str, func: &mut NoirFunction, storage_defined: bool) {
fn transform_function(
ty: &str,
func: &mut NoirFunction,
storage_defined: bool,
) -> Result<(), AztecMacroError> {
let context_name = format!("{}Context", ty);
let inputs_name = format!("{}ContextInputs", ty);
let return_type_name = format!("{}CircuitPublicInputs", ty);
Expand All @@ -396,7 +409,7 @@ fn transform_function(ty: &str, func: &mut NoirFunction, storage_defined: bool)
}

// Insert the context creation as the first action
let create_context = create_context(&context_name, &func.def.parameters);
let create_context = create_context(&context_name, &func.def.parameters)?;
func.def.body.0.splice(0..0, (create_context).iter().cloned());

// Add the inputs to the params
Expand All @@ -423,6 +436,8 @@ fn transform_function(ty: &str, func: &mut NoirFunction, storage_defined: bool)
"Public" => func.def.is_open = true,
_ => (),
}

Ok(())
}

/// Transform Unconstrained
Expand Down Expand Up @@ -621,7 +636,7 @@ fn create_inputs(ty: &str) -> Param {
/// let mut context = PrivateContext::new(inputs, hasher.hash());
/// }
/// ```
fn create_context(ty: &str, params: &[Param]) -> Vec<Statement> {
fn create_context(ty: &str, params: &[Param]) -> Result<Vec<Statement>, AztecMacroError> {
let mut injected_expressions: Vec<Statement> = vec![];

// `let mut hasher = Hasher::new();`
Expand All @@ -637,7 +652,7 @@ fn create_context(ty: &str, params: &[Param]) -> Vec<Statement> {
injected_expressions.push(let_hasher);

// Iterate over each of the function parameters, adding to them to the hasher
params.iter().for_each(|Param { pattern, typ, span: _, visibility: _ }| {
for Param { pattern, typ, span, .. } in params {
match pattern {
Pattern::Identifier(identifier) => {
// Match the type to determine the padding to do
Expand Down Expand Up @@ -666,16 +681,18 @@ fn create_context(ty: &str, params: &[Param]) -> Vec<Statement> {
},
)
}
_ => panic!(
"[Aztec Noir] Provided parameter type: {:?} is not supported",
unresolved_type
),
_ => {
return Err(AztecMacroError::UnsupportedFunctionArgumentType {
typ: unresolved_type.clone(),
span: *span,
})
}
};
injected_expressions.push(expression);
}
_ => todo!(), // Maybe unreachable?
}
});
}

// Create the inputs to the context
let inputs_expression = variable("inputs");
Expand All @@ -697,7 +714,7 @@ fn create_context(ty: &str, params: &[Param]) -> Vec<Statement> {
injected_expressions.push(let_context);

// Return all expressions that will be injected by the hasher
injected_expressions
Ok(injected_expressions)
}

/// Abstract Return Type
Expand Down
Loading