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

fix: handle slice of array type #5297

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 14 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@
self.codegen_array_checked(elements, typ[1].clone())?;
Tree::Branch(vec![slice_length.into(), slice_contents])
}
ast::Type::Array(len, _) => {
// A literal slice can be an array or a slice,
// however we convert the array into a slice because printable types
// do not handle slice array properly.
Comment on lines +220 to +222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on this comment? A literal slice should only be a slice and never an array since we removed the polymorphism a while back

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the issue is with modulus_be_bytes and possibly other builtin functions returning arrays when they should return slices or vice-versa

let slice_type = if let Type::Array(first_type, _) = &typ[0] {
Type::Slice(first_type.clone())
} else {
typ[0].clone()
};
let slice_length = self.builder.length_constant(len as u128);

let slice_contents = self.codegen_array_checked(elements, slice_type)?;
Tree::Branch(vec![slice_length.into(), slice_contents])
}
_ => unreachable!("ICE: unexpected slice literal type, got {}", array.typ),
})
}
Expand Down Expand Up @@ -472,7 +486,7 @@
/// br loop_entry(v0)
/// loop_entry(i: Field):
/// v2 = lt i v1
/// brif v2, then: loop_body, else: loop_end

Check warning on line 489 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
/// loop_body():
/// v3 = ... codegen body ...
/// v4 = add 1, i
Expand Down Expand Up @@ -531,7 +545,7 @@
/// For example, the expression `if cond { a } else { b }` is codegen'd as:
///
/// v0 = ... codegen cond ...
/// brif v0, then: then_block, else: else_block

Check warning on line 548 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
/// then_block():
/// v1 = ... codegen a ...
/// br end_if(v1)
Expand All @@ -544,7 +558,7 @@
/// As another example, the expression `if cond { a }` is codegen'd as:
///
/// v0 = ... codegen cond ...
/// brif v0, then: then_block, else: end_block

Check warning on line 561 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
/// then_block:
/// v1 = ... codegen a ...
/// br end_if()
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ impl<'interner> Monomorphizer<'interner> {
let typ = Type::Array(bytes_as_expr.len() as u32, Box::new(int_type));

let arr_literal = ArrayLiteral { typ, contents: bytes_as_expr };
Expression::Literal(Literal::Array(arr_literal))
Expression::Literal(Literal::Slice(arr_literal))
}

fn queue_function(
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/regression_5245/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_5245"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
Empty file.
6 changes: 6 additions & 0 deletions test_programs/execution_success/regression_5245/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use dep::std::field::modulus_be_bytes;

fn main() -> pub [u8; 32] {
let bytes = dep::std::field::modulus_be_bytes();
bytes.as_array()
}
Loading