Skip to content

Commit

Permalink
Extract check_can_mutate_function
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Aug 6, 2024
1 parent 64f3ffa commit 2ee0102
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::{
Visibility,
},
hir::comptime::{errors::IResult, value::add_token_spans, InterpreterError, Value},
hir_def::function::FunctionBody,
hir_def::function::{FuncMeta, FunctionBody},
macros_api::{ModuleDefId, NodeInterner, Signedness},
node_interner::DefinitionKind,
node_interner::{DefinitionKind, FuncId},
parser,
token::{SpannedToken, Token},
QuotedType, Shared, Type,
Expand Down Expand Up @@ -730,14 +730,7 @@ fn function_def_set_body(
let body_argument_location = body_argument.1;

let func_id = get_function_def(self_argument)?;

let func_meta = interpreter.elaborator.interner.function_meta(&func_id);
match func_meta.function_body {
FunctionBody::Unresolved(_, _, _) => (),
FunctionBody::Resolving | FunctionBody::Resolved => {
return Err(InterpreterError::CannotMutateFunction { location })
}
}
check_can_mutate_function(interpreter, func_id, location)?;

let body_tokens = get_quoted(body_argument)?;
let mut body_quoted = add_token_spans(body_tokens.clone(), body_argument_location.span);
Expand Down Expand Up @@ -773,13 +766,7 @@ fn function_def_set_parameters(
let parameters_argument_location = parameters_argument.1;

let func_id = get_function_def(self_argument)?;
let func_meta = interpreter.elaborator.interner.function_meta(&func_id);
match func_meta.function_body {
FunctionBody::Unresolved(_, _, _) => (),
FunctionBody::Resolving | FunctionBody::Resolved => {
return Err(InterpreterError::CannotMutateFunction { location })
}
}
check_can_mutate_function(interpreter, func_id, location)?;

let (input_parameters, _type) =
get_slice(interpreter.elaborator.interner, parameters_argument)?;
Expand Down Expand Up @@ -858,13 +845,7 @@ fn function_def_set_return_type(
let return_type = get_type(return_type_argument)?;

let func_id = get_function_def(self_argument)?;
let func_meta = interpreter.elaborator.interner.function_meta(&func_id);
match func_meta.function_body {
FunctionBody::Unresolved(_, _, _) => (),
FunctionBody::Resolving | FunctionBody::Resolved => {
return Err(InterpreterError::CannotMutateFunction { location })
}
}
let func_meta = check_can_mutate_function(interpreter, func_id, location)?;

let parameter_types = func_meta.parameters.iter().map(|(_, typ, _)| typ.clone()).collect();

Expand Down Expand Up @@ -1059,3 +1040,17 @@ pub(crate) fn extract_option_generic_type(typ: Type) -> Type {

generics.pop().expect("Expected Option to have a T generic type")
}

fn check_can_mutate_function<'a>(
interpreter: &'a Interpreter,
func_id: FuncId,
location: Location,
) -> Result<&'a FuncMeta, InterpreterError> {
let func_meta = interpreter.elaborator.interner.function_meta(&func_id);
match func_meta.function_body {
FunctionBody::Unresolved(_, _, _) => Ok(func_meta),
FunctionBody::Resolving | FunctionBody::Resolved => {
return Err(InterpreterError::CannotMutateFunction { location })
}
}
}

0 comments on commit 2ee0102

Please sign in to comment.