Skip to content

Commit

Permalink
chore: Make a more clear error for slices passed to std::println (#2113)
Browse files Browse the repository at this point in the history
* chore: make a more clear error for slices passed to std::println

* fix up err message
  • Loading branch information
vezenovm authored Aug 1, 2023
1 parent ce94cb4 commit 3a42368
Showing 1 changed file with 22 additions and 20 deletions.
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

0 comments on commit 3a42368

Please sign in to comment.