Skip to content

Commit

Permalink
fix(println): Enable printing of arrays/strings >2 in fmt strings (#2947
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vezenovm authored Oct 2, 2023
1 parent 8f76fe5 commit 309fa70
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
23 changes: 17 additions & 6 deletions compiler/noirc_printable_type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,23 @@ fn convert_fmt_string_inputs(
{
let printable_type = fetch_printable_type(printable_value)?;
let type_size = printable_type.field_count() as usize;

let mut input_values_as_fields = input_and_printable_values[i..(i + type_size)]
.iter()
.flat_map(|param| vecmap(param.values(), |value| value.to_field()));

let value = decode_value(&mut input_values_as_fields, &printable_type);
let value = match printable_type {
PrintableType::Array { .. } | PrintableType::String { .. } => {
// Arrays and strings are represented in a single value vector rather than multiple separate input values
let mut input_values_as_fields = input_and_printable_values[i]
.values()
.into_iter()
.map(|value| value.to_field());
decode_value(&mut input_values_as_fields, &printable_type)
}
_ => {
// We must use a flat map here as each value in a struct will be in a separate input value
let mut input_values_as_fields = input_and_printable_values[i..(i + type_size)]
.iter()
.flat_map(|param| vecmap(param.values(), |value| value.to_field()));
decode_value(&mut input_values_as_fields, &printable_type)
}
};

output.push((value, printable_type));
}
Expand Down
18 changes: 18 additions & 0 deletions tooling/nargo_cli/tests/execution_success/debug_logs/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ fn main(x : Field, y : pub Field) {

let struct_string = if x != 5 { f"{foo}" } else { f"{bar}" };
std::println(struct_string);

regression_2906();
}

fn string_identity(string: fmtstr<14, (Field, Field)>) -> fmtstr<14, (Field, Field)> {
Expand All @@ -62,3 +64,19 @@ struct fooStruct {
my_struct: myStruct,
foo: Field,
}

fn regression_2906() {
let array_two_vals = [1, 2];
dep::std::println(f"array_two_vals: {array_two_vals}");

let label_two_vals = "12";
dep::std::println(f"label_two_vals: {label_two_vals}");

let array_five_vals = [1, 2, 3, 4, 5];
dep::std::println(f"array_five_vals: {array_five_vals}");

let label_five_vals = "12345";
dep::std::println(f"label_five_vals: {label_five_vals}");

dep::std::println(f"array_five_vals: {array_five_vals}, label_five_vals: {label_five_vals}");
}

0 comments on commit 309fa70

Please sign in to comment.