From e94c52a063ab05c2cfd944fccb3daf775ad2456b Mon Sep 17 00:00:00 2001 From: vezenovm Date: Tue, 1 Aug 2023 20:15:19 +0000 Subject: [PATCH 1/2] chore: make a more clear error for slices passed to std::println --- .../src/ssa_refactor/acir_gen/mod.rs | 2 +- .../src/monomorphization/mod.rs | 42 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs index 5253cb71875..4a7d2e46775 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs @@ -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(), }; diff --git a/crates/noirc_frontend/src/monomorphization/mod.rs b/crates/noirc_frontend/src/monomorphization/mod.rs index 963d16a311c..25de2ce19d6 100644 --- a/crates/noirc_frontend/src/monomorphization/mod.rs +++ b/crates/noirc_frontend/src/monomorphization/mod.rs @@ -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) { + if let HirType::Array(size, _) = typ { + if let HirType::NotConstant = **size { + unreachable!("Slices are not supported with println. 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. From da85b77b92f2394c981e6716a73bb97194a4a883 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Tue, 1 Aug 2023 20:20:19 +0000 Subject: [PATCH 2/2] fix up err message --- crates/noirc_frontend/src/monomorphization/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/noirc_frontend/src/monomorphization/mod.rs b/crates/noirc_frontend/src/monomorphization/mod.rs index 25de2ce19d6..dbe2ee080bf 100644 --- a/crates/noirc_frontend/src/monomorphization/mod.rs +++ b/crates/noirc_frontend/src/monomorphization/mod.rs @@ -758,7 +758,7 @@ impl<'interner> Monomorphizer<'interner> { fn append_abi_arg_inner(typ: &Type, arguments: &mut Vec) { if let HirType::Array(size, _) = typ { if let HirType::NotConstant = **size { - unreachable!("Slices are not supported with println. Convert the slice to an array before passing it to println"); + unreachable!("println does not support slices. Convert the slice to an array before passing it to println"); } } let abi_type = typ.as_abi_type();