Skip to content

Commit

Permalink
fix: use element_size() instead of computing it with division (#5939)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #5936

## Summary


## Additional Context

I was computing an array's element size in a way that could lead to
dividing by zero, but there was already a method to get the element
size.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: jfecher <jake@aztecprotocol.com>
  • Loading branch information
asterite and jfecher authored Sep 5, 2024
1 parent 9d2629d commit 6a45007
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
5 changes: 2 additions & 3 deletions compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,12 @@ fn handle_array_get_group(
next_out_of_bounds_index: &mut Option<usize>,
possible_index_out_of_bounds_indexes: &mut Vec<usize>,
) {
let Some(array_length) = function.dfg.try_get_array_length(*array) else {
if function.dfg.try_get_array_length(*array).is_none() {
// Nothing to do for slices
return;
};

let flattened_size = function.dfg.type_of_value(*array).flattened_size();
let element_size = flattened_size / array_length;
let element_size = function.dfg.type_of_value(*array).element_size();
if element_size <= 1 {
// Not a composite type
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "empty_composite_array_get"
type = "bin"
authors = [""]
compiler_version = ">=0.32.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
empty_input = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main(empty_input: [(Field, Field); 0]) {
let empty_array: [(Field, Field); 0] = [];
let _ = empty_input[0];
let _ = empty_array[0];
}

0 comments on commit 6a45007

Please sign in to comment.