Skip to content

Commit

Permalink
fix: initialise strings as u8 array (#3682)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #3635 

## Summary\*

String literals were initialised as field array instead of array of u8.

## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** 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.
  • Loading branch information
guipublic authored Dec 5, 2023
1 parent 8b3a68f commit 8da40b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ impl<'a> FunctionContext<'a> {
}

fn codegen_string(&mut self, string: &str) -> Values {
let elements =
vecmap(string.as_bytes(), |byte| self.builder.field_constant(*byte as u128).into());
let elements = vecmap(string.as_bytes(), |byte| {
self.builder.numeric_constant(*byte as u128, Type::unsigned(8)).into()
});
let typ = Self::convert_non_tuple_type(&ast::Type::String(elements.len() as u64));
self.codegen_array(elements, typ)
}
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/regression_3635/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_3635"
version = "0.1.0"
type = "bin"
authors = [""]

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

fn main() {
let x: u8 = 0x61;
let y: u8 = "a".as_bytes()[0];
assert_eq(x, y);
assert_eq(x >> 1, y >> 1);
}

0 comments on commit 8da40b7

Please sign in to comment.