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: Make a more clear error for slices passed to std::println #2113

Merged
merged 2 commits into from
Aug 1, 2023
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 crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ impl Context {
}
Intrinsic::ArrayLen => {
let len = match self.convert_value(arguments[0], dfg) {
AcirValue::Var(_, _) => unreachable!("Non-array passed to array.len() method"),
AcirValue::Var(_, _) => unreachable!("Non-array passed to array.len() method"),
AcirValue::Array(values) => (values.len() as u128).into(),
AcirValue::DynamicArray(array) => (array.len as u128).into(),
};
Expand Down
42 changes: 22 additions & 20 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,45 +727,47 @@ impl<'interner> Monomorphizer<'interner> {
HirExpression::Ident(ident) => {
let typ = self.interner.id_type(ident.id);
let typ: Type = typ.follow_bindings();
match &typ {
let is_fmt_str = match typ {
// A format string has many different possible types that need to be handled.
// Loop over each element in the format string to fetch each type's relevant metadata
Type::FmtString(_, elements) => {
match elements.as_ref() {
match *elements {
Type::Tuple(element_types) => {
for typ in element_types {
let abi_type = typ.as_abi_type();
let abi_as_string = serde_json::to_string(&abi_type)
.expect("ICE: expected Abi type to serialize");

arguments.push(ast::Expression::Literal(ast::Literal::Str(
abi_as_string,
)));
Self::append_abi_arg_inner(&typ, arguments);
}
}
_ => unreachable!(
"ICE: format string type should be a tuple but got a {elements}"
),
}

// The caller needs information as to whether it is handling a format string or a single type
arguments.push(ast::Expression::Literal(ast::Literal::Bool(true)));
true
}
_ => {
let abi_type = typ.as_abi_type();
let abi_as_string = serde_json::to_string(&abi_type)
.expect("ICE: expected Abi type to serialize");

arguments.push(ast::Expression::Literal(ast::Literal::Str(abi_as_string)));
// The caller needs information as to whether it is handling a format string or a single type
arguments.push(ast::Expression::Literal(ast::Literal::Bool(false)));
Self::append_abi_arg_inner(&typ, arguments);
false
}
}
};
// The caller needs information as to whether it is handling a format string or a single type
arguments.push(ast::Expression::Literal(ast::Literal::Bool(is_fmt_str)));
}
_ => unreachable!("logging expr {:?} is not supported", arguments[0]),
}
}

fn append_abi_arg_inner(typ: &Type, arguments: &mut Vec<ast::Expression>) {
if let HirType::Array(size, _) = typ {
if let HirType::NotConstant = **size {
unreachable!("println does not support slices. Convert the slice to an array before passing it to println");
}
}
let abi_type = typ.as_abi_type();
let abi_as_string =
serde_json::to_string(&abi_type).expect("ICE: expected Abi type to serialize");

arguments.push(ast::Expression::Literal(ast::Literal::Str(abi_as_string)));
}

/// Try to evaluate certain builtin functions (currently only 'array_len' and field modulus methods)
/// at their call site.
/// NOTE: Evaluating at the call site means we cannot track aliased functions.
Expand Down