Skip to content

Commit fff7701

Browse files
committed
feat: implement str_as_bytes in the comptime interpreter (noir-lang/noir#5887)
feat: use visibility (noir-lang/noir#5856) chore: use `new_let` more widely (noir-lang/noir#5882) feat: Sync from aztec-packages (noir-lang/noir#5883) feat!: return arrays instead of slices from `to_be_radix` functions (noir-lang/noir#5851) chore: add a span to track timing of brillig gen (noir-lang/noir#5835) feat: add `Expr::as_assert_eq` (noir-lang/noir#5880) feat: LSP diagnostics now have "unnecessary" and "deprecated" tags (noir-lang/noir#5878) feat: LSP code actions to import or qualify unresolved paths (noir-lang/noir#5876)
2 parents 0dab2aa + 7d9d1fa commit fff7701

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

.noir-sync-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e349f30b60a473e2068afafb6fae4a4ea50d185b
1+
45344bfe1148a2f592c2e432744d3fb3d46340cc

noir/noir-repo/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

+27
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
126126
"slice_push_back" => slice_push_back(interner, arguments, location),
127127
"slice_push_front" => slice_push_front(interner, arguments, location),
128128
"slice_remove" => slice_remove(interner, arguments, location, call_stack),
129+
"str_as_bytes" => str_as_bytes(interner, arguments, location),
129130
"struct_def_as_type" => struct_def_as_type(interner, arguments, location),
130131
"struct_def_fields" => struct_def_fields(interner, arguments, location),
131132
"struct_def_generics" => struct_def_generics(interner, arguments, location),
@@ -242,6 +243,32 @@ fn slice_push_back(
242243
Ok(Value::Slice(values, typ))
243244
}
244245

246+
fn str_as_bytes(
247+
interner: &NodeInterner,
248+
arguments: Vec<(Value, Location)>,
249+
location: Location,
250+
) -> IResult<Value> {
251+
let (string, string_location) = check_one_argument(arguments, location)?;
252+
253+
match string {
254+
Value::String(string) => {
255+
let string_as_bytes = string.as_bytes();
256+
let bytes_vector: Vec<Value> = string_as_bytes.iter().cloned().map(Value::U8).collect();
257+
let byte_array_type = Type::Array(
258+
Box::new(Type::Constant(string_as_bytes.len() as u32)),
259+
Box::new(Type::Integer(Signedness::Unsigned, IntegerBitSize::Eight)),
260+
);
261+
Ok(Value::Array(bytes_vector.into(), byte_array_type))
262+
}
263+
value => {
264+
let type_var = Box::new(interner.next_type_variable());
265+
let expected = Type::Array(type_var.clone(), type_var);
266+
let actual = value.get_type().into_owned();
267+
Err(InterpreterError::TypeMismatch { expected, actual, location: string_location })
268+
}
269+
}
270+
}
271+
245272
/// fn as_type(self) -> Type
246273
fn struct_def_as_type(
247274
interner: &NodeInterner,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "comptime_str_as_bytes"
3+
type = "bin"
4+
authors = [""]
5+
compiler_version = ">=0.24.0"
6+
7+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
comptime
3+
{
4+
let hello_world_string = "hello world";
5+
let hello_world_bytes: [u8; 11] = [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64];
6+
7+
assert_eq(hello_world_string.as_bytes(), hello_world_bytes);
8+
}
9+
}

0 commit comments

Comments
 (0)