diff --git a/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr b/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr index 5bf3f7e0975..d84be844d8e 100644 --- a/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr +++ b/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr @@ -1,4 +1,4 @@ // Example that uses the distinct keyword -fn main(x: pub Field) -> distinct pub [Field; 3] { - [x+1, x, x] +fn main(x: pub Field) -> distinct pub [Field;2] { + [x+1, x] } diff --git a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json index 77dc3d9fe1c..ad8ebce2475 100644 --- a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json +++ b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json @@ -1 +1 @@ -{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"x":[1]},"return_type":{"kind":"array","length":3,"type":{"kind":"field"}},"return_witnesses":[2,1,3]},"bytecode":"H4sIAAAAAAAA/9VPOQ7AIAzj6IMSkkCy9StFhf8/oQtISB3LUi8+Bss+nHPBvTGzczB8A/qliyAzt5IaEl6QrKoAS82KiqJyJyVqylqsWgFDpoZdjPooCxt3/eVz3LcL5l+/cFx0GP4Bi8kGuhwCAAA=","proving_key":null,"verification_key":null} \ No newline at end of file +{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"x":[1]},"return_type":{"kind":"array","length":2,"type":{"kind":"field"}},"return_witnesses":[3,4]},"bytecode":"H4sIAAAAAAAA/9WRQQ6AIAwEEfU/LW2lvfkVifj/JxgSTEg8Cgf3srftTnd1zs3uLV99rw7fhFOTRbAx5xgyEh4QLKkAS9oUFUXlDEqUlTVasgiGTBkvMbpqmO/YaySz78g89+sFf9l5GcA8Nf6wl9+WWzcyyYCuDAMAAA==","proving_key":null,"verification_key":null} \ No newline at end of file diff --git a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr index 57b9214a4e1..d79dfba9359 100644 Binary files a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr and b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr differ diff --git a/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs b/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs index a9bdcd7b43b..aca809a85fa 100644 --- a/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs @@ -24,11 +24,7 @@ use crate::brillig::{brillig_gen::brillig_fn::FunctionContext as BrilligFunction use crate::errors::{InternalError, RuntimeError}; pub(crate) use acir_ir::generated_acir::GeneratedAcir; use acvm::{ - acir::{ - brillig::Opcode, - circuit::opcodes::BlockId, - native_types::{Expression, Witness}, - }, + acir::{brillig::Opcode, circuit::opcodes::BlockId, native_types::Expression}, FieldElement, }; use iter_extended::{try_vecmap, vecmap}; @@ -123,19 +119,17 @@ impl Ssa { match abi_distinctness { AbiDistinctness::Distinct => { - let mut distinct_return_witness: Vec = - Vec::with_capacity(generated_acir.return_witnesses.len()); - - for mut return_witness in generated_acir.return_witnesses.clone() { - // If witness has already been used then create a new one - // to guarantee that the return witnesses are distinct - if distinct_return_witness.contains(&return_witness) { - return_witness = generated_acir - .create_witness_for_expression(&Expression::from(return_witness)); - } - - distinct_return_witness.push(return_witness); - } + // Create a witness for each return witness we have + // to guarantee that the return witnesses are distinct + let distinct_return_witness: Vec<_> = generated_acir + .return_witnesses + .clone() + .into_iter() + .map(|return_witness| { + generated_acir + .create_witness_for_expression(&Expression::from(return_witness)) + }) + .collect(); generated_acir.return_witnesses = distinct_return_witness; Ok(generated_acir) @@ -1127,7 +1121,7 @@ impl Context { #[cfg(test)] mod tests { - use std::{collections::HashMap, rc::Rc}; + use std::{rc::Rc, collections::HashMap}; use acvm::{ acir::{