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: Stop cloning FuncMeta #3968

Merged
merged 3 commits into from
Jan 8, 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
2 changes: 1 addition & 1 deletion compiler/noirc_driver/src/abi_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(super) fn compute_function_abi(
) -> (Vec<AbiParameter>, Option<AbiType>) {
let func_meta = context.def_interner.function_meta(func_id);

let (parameters, return_type) = func_meta.into_function_signature();
let (parameters, return_type) = func_meta.function_signature();
let parameters = into_abi_params(context, parameters);
let return_type = return_type.map(|typ| AbiType::from_type(context, &typ));
(parameters, return_type)
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@
.collect()
}

// TODO(vitkov): Move this out of here and into type_check

Check warning on line 458 in compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (vitkov)
pub(crate) fn check_methods_signatures(
resolver: &mut Resolver,
impl_methods: &Vec<(FileId, FuncId)>,
Expand Down Expand Up @@ -542,6 +542,7 @@

// TODO: This is not right since it may bind generic return types
trait_method.return_type().unify(&resolved_return_type, &mut typecheck_errors, || {
let impl_method = resolver.interner.function_meta(func_id);
let ret_type_span = impl_method.return_type.get_type().span;
let expr_span = ret_type_span.expect("return type must always have a span");

Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Context<'_> {
None
}

pub fn function_meta(&self, func_id: &FuncId) -> FuncMeta {
pub fn function_meta(&self, func_id: &FuncId) -> &FuncMeta {
self.def_interner.function_meta(func_id)
}

Expand Down
9 changes: 5 additions & 4 deletions compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ impl<'interner> TypeChecker<'interner> {
// Automatically add `&mut` if the method expects a mutable reference and
// the object is not already one.
if *func_id != FuncId::dummy_id() {
let func_meta = self.interner.function_meta(func_id);
let function_type =
self.interner.function_meta(func_id).typ.clone();
self.try_add_mutable_reference_to_object(
&mut method_call,
&func_meta.typ,
&function_type,
&mut args,
);
}
Expand Down Expand Up @@ -561,7 +562,7 @@ impl<'interner> TypeChecker<'interner> {

let func_meta = self.interner.function_meta(&func_id);
let param_len = func_meta.parameters.len();
(func_meta.typ, param_len)
(func_meta.typ.clone(), param_len)
}
HirMethodReference::TraitMethodId(method) => {
let the_trait = self.interner.get_trait(method.trait_id);
Expand Down Expand Up @@ -916,7 +917,7 @@ impl<'interner> TypeChecker<'interner> {
&self.current_function.expect("unexpected method outside a function"),
);

for constraint in func_meta.trait_constraints {
for constraint in &func_meta.trait_constraints {
if *object_type == constraint.typ {
if let Some(the_trait) = self.interner.try_get_trait(constraint.trait_id) {
for (method_index, method) in the_trait.methods.iter().enumerate() {
Expand Down
17 changes: 11 additions & 6 deletions compiler/noirc_frontend/src/hir/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,23 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
type_checker.current_function = Some(func_id);

let meta = type_checker.interner.function_meta(&func_id);
let parameters = meta.parameters.clone();
let expected_return_type = meta.return_type.clone();
let expected_trait_constraints = meta.trait_constraints.clone();
let name_span = meta.name.location.span;

let mut errors = Vec::new();

// Temporarily add any impls in this function's `where` clause to scope
for constraint in &meta.trait_constraints {
for constraint in &expected_trait_constraints {
let object = constraint.typ.clone();
let trait_id = constraint.trait_id;

if !type_checker.interner.add_assumed_trait_implementation(object, trait_id) {
if let Some(the_trait) = type_checker.interner.try_get_trait(trait_id) {
let trait_name = the_trait.name.to_string();
let typ = constraint.typ.clone();
let span = meta.name.location.span;
let span = name_span;
errors.push(TypeCheckError::UnneededTraitConstraint { trait_name, typ, span });
}
}
Expand All @@ -70,7 +75,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
// Bind each parameter to its annotated type.
// This is locally obvious, but it must be bound here so that the
// Definition object of the parameter in the NodeInterner is given the correct type.
for param in meta.parameters.into_iter() {
for param in parameters {
type_checker.bind_pattern(&param.0, param.1);
}

Expand All @@ -93,7 +98,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
errors.append(&mut type_checker.errors);

// Now remove all the `where` clause constraints we added
for constraint in &meta.trait_constraints {
for constraint in &expected_trait_constraints {
interner.remove_assumed_trait_implementations_for_trait(constraint.trait_id);
}

Expand All @@ -107,7 +112,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
expected: declared_return_type.clone(),
actual: function_last_type,
span: func_span,
source: Source::Return(meta.return_type, expr_span),
source: Source::Return(expected_return_type, expr_span),
};
errors.push(error);
}
Expand All @@ -122,7 +127,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
expected: declared_return_type.clone(),
actual: function_last_type.clone(),
span: func_span,
source: Source::Return(meta.return_type, expr_span),
source: Source::Return(expected_return_type, expr_span),
};

if empty_function {
Expand Down
18 changes: 4 additions & 14 deletions compiler/noirc_frontend/src/hir_def/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,12 @@ impl FuncMeta {
}
}

pub fn into_function_signature(self) -> FunctionSignature {
// Doesn't use `self.return_type()` so we aren't working with references and don't need a `clone()`
let return_type = match self.typ {
Type::Function(_, ret, _env) => *ret,
Type::Forall(_, typ) => match *typ {
Type::Function(_, ret, _env) => *ret,
_ => unreachable!(),
},
_ => unreachable!(),
};
let return_type = match return_type {
pub fn function_signature(&self) -> FunctionSignature {
let return_type = match self.return_type() {
Type::Unit => None,
typ => Some(typ),
typ => Some(typ.clone()),
};

(self.parameters.0, return_type)
(self.parameters.0.clone(), return_type)
}

/// Gives the (uninstantiated) return type of this function.
Expand Down
31 changes: 16 additions & 15 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{
use crate::{
hir_def::{
expr::*,
function::{FuncMeta, FunctionSignature, Parameters},
function::{FunctionSignature, Parameters},
stmt::{HirAssignStatement, HirLValue, HirLetStatement, HirPattern, HirStatement},
types,
},
Expand Down Expand Up @@ -106,14 +106,14 @@ pub fn monomorphize(main: node_interner::FuncId, interner: &NodeInterner) -> Pro
}

let functions = vecmap(monomorphizer.finished_functions, |(_, f)| f);
let FuncMeta { return_distinctness, return_visibility, .. } = interner.function_meta(&main);
let meta = interner.function_meta(&main);

Program::new(
functions,
function_sig,
return_distinctness,
meta.return_distinctness,
monomorphizer.return_location,
return_visibility,
meta.return_visibility,
)
}

Expand Down Expand Up @@ -217,7 +217,7 @@ impl<'interner> Monomorphizer<'interner> {
},
);
let main_meta = self.interner.function_meta(&main_id);
main_meta.into_function_signature()
main_meta.function_signature()
}

fn function(&mut self, f: node_interner::FuncId, id: FuncId) {
Expand All @@ -237,7 +237,7 @@ impl<'interner> Monomorphizer<'interner> {
_ => meta.return_type(),
});

let parameters = self.parameters(meta.parameters);
let parameters = self.parameters(&meta.parameters);

let body = self.expr(body_expr_id);
let unconstrained = modifiers.is_unconstrained
Expand All @@ -254,17 +254,17 @@ impl<'interner> Monomorphizer<'interner> {

/// Monomorphize each parameter, expanding tuple/struct patterns into multiple parameters
/// and binding any generic types found.
fn parameters(&mut self, params: Parameters) -> Vec<(ast::LocalId, bool, String, ast::Type)> {
fn parameters(&mut self, params: &Parameters) -> Vec<(ast::LocalId, bool, String, ast::Type)> {
let mut new_params = Vec::with_capacity(params.len());
for parameter in params {
self.parameter(parameter.0, &parameter.1, &mut new_params);
for (parameter, typ, _) in &params.0 {
self.parameter(parameter, typ, &mut new_params);
}
new_params
}

fn parameter(
&mut self,
param: HirPattern,
param: &HirPattern,
typ: &HirType,
new_params: &mut Vec<(ast::LocalId, bool, String, ast::Type)>,
) {
Expand All @@ -276,19 +276,20 @@ impl<'interner> Monomorphizer<'interner> {
new_params.push((new_id, definition.mutable, name, self.convert_type(typ)));
self.define_local(ident.id, new_id);
}
HirPattern::Mutable(pattern, _) => self.parameter(*pattern, typ, new_params),
HirPattern::Mutable(pattern, _) => self.parameter(pattern, typ, new_params),
HirPattern::Tuple(fields, _) => {
let tuple_field_types = unwrap_tuple_type(typ);

for (field, typ) in fields.into_iter().zip(tuple_field_types) {
for (field, typ) in fields.iter().zip(tuple_field_types) {
self.parameter(field, &typ, new_params);
}
}
HirPattern::Struct(_, fields, _) => {
let struct_field_types = unwrap_struct_type(typ);
assert_eq!(struct_field_types.len(), fields.len());

let mut fields = btree_map(fields, |(name, field)| (name.0.contents, field));
let mut fields =
btree_map(fields, |(name, field)| (name.0.contents.clone(), field));

// Iterate over `struct_field_types` since `unwrap_struct_type` will always
// return the fields in the order defined by the struct type.
Expand Down Expand Up @@ -1183,7 +1184,7 @@ impl<'interner> Monomorphizer<'interner> {
let parameters =
vecmap(lambda.parameters, |(pattern, typ)| (pattern, typ, Visibility::Private)).into();

let parameters = self.parameters(parameters);
let parameters = self.parameters(&parameters);
let body = self.expr(lambda.body);

let id = self.next_function_id();
Expand Down Expand Up @@ -1234,7 +1235,7 @@ impl<'interner> Monomorphizer<'interner> {
let parameters =
vecmap(lambda.parameters, |(pattern, typ)| (pattern, typ, Visibility::Private)).into();

let mut converted_parameters = self.parameters(parameters);
let mut converted_parameters = self.parameters(&parameters);

let id = self.next_function_id();
let name = lambda_name.to_owned();
Expand Down
8 changes: 4 additions & 4 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,12 +768,12 @@ impl NodeInterner {
}

/// Returns the interned meta data corresponding to `func_id`
pub fn function_meta(&self, func_id: &FuncId) -> FuncMeta {
self.func_meta.get(func_id).cloned().expect("ice: all function ids should have metadata")
pub fn function_meta(&self, func_id: &FuncId) -> &FuncMeta {
self.func_meta.get(func_id).expect("ice: all function ids should have metadata")
}

pub fn try_function_meta(&self, func_id: &FuncId) -> Option<FuncMeta> {
self.func_meta.get(func_id).cloned()
pub fn try_function_meta(&self, func_id: &FuncId) -> Option<&FuncMeta> {
self.func_meta.get(func_id)
}

pub fn function_ident(&self, func_id: &FuncId) -> crate::Ident {
Expand Down
Loading