From ba55adb1e1f6c63b016c6f5ad317c63fd5639f1f Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 30 Aug 2024 14:50:11 -0300 Subject: [PATCH 1/2] feat: add `Type::as_string` --- .../src/hir/comptime/interpreter/builtin.rs | 16 ++++++++++++++++ docs/docs/noir/standard_library/meta/typ.md | 6 ++++++ noir_stdlib/src/meta/typ.nr | 5 +++++ .../comptime_type/src/main.nr | 5 +++++ 4 files changed, 32 insertions(+) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 0dafe408c7f..6d3c92d8cd4 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -140,6 +140,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "type_as_constant" => type_as_constant(arguments, return_type, location), "type_as_integer" => type_as_integer(arguments, return_type, location), "type_as_slice" => type_as_slice(arguments, return_type, location), + "type_as_string" => type_as_string(arguments, return_type, location), "type_as_struct" => type_as_struct(arguments, return_type, location), "type_as_tuple" => type_as_tuple(arguments, return_type, location), "type_eq" => type_eq(arguments, location), @@ -539,6 +540,21 @@ fn type_as_slice( }) } +// fn as_string(self) -> Option +fn type_as_string( + arguments: Vec<(Value, Location)>, + return_type: Type, + location: Location, +) -> IResult { + type_as(arguments, return_type, location, |typ| { + if let Type::String(n) = typ { + Some(Value::Type(*n)) + } else { + None + } + }) +} + // fn as_struct(self) -> Option<(StructDefinition, [Type])> fn type_as_struct( arguments: Vec<(Value, Location)>, diff --git a/docs/docs/noir/standard_library/meta/typ.md b/docs/docs/noir/standard_library/meta/typ.md index bad6435e94a..e38be284401 100644 --- a/docs/docs/noir/standard_library/meta/typ.md +++ b/docs/docs/noir/standard_library/meta/typ.md @@ -45,6 +45,12 @@ if the type is signed, as well as the number of bits of this integer type. If this is a slice type, return the element type of the slice. +### as_string + +#include_code as_string noir_stdlib/src/meta/typ.nr rust + +If this is a string type, returns its length as a type. + ### as_struct #include_code as_struct noir_stdlib/src/meta/typ.nr rust diff --git a/noir_stdlib/src/meta/typ.nr b/noir_stdlib/src/meta/typ.nr index a3f35b28e43..35b5145b5ac 100644 --- a/noir_stdlib/src/meta/typ.nr +++ b/noir_stdlib/src/meta/typ.nr @@ -22,6 +22,11 @@ impl Type { fn as_slice(self) -> Option {} // docs:end:as_slice + #[builtin(type_as_string)] +// docs:start:as_string + fn as_string(self) -> Option {} + // docs:end:as_string + #[builtin(type_as_struct)] // docs:start:as_struct fn as_struct(self) -> Option<(StructDefinition, [Type])> {} diff --git a/test_programs/compile_success_empty/comptime_type/src/main.nr b/test_programs/compile_success_empty/comptime_type/src/main.nr index 6d98d1d173b..4b6eb6f3933 100644 --- a/test_programs/compile_success_empty/comptime_type/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type/src/main.nr @@ -110,6 +110,11 @@ fn main() { assert(!struct_does_not_implement_some_trait.implements(some_trait_field)); let _trait_impl = struct_implements_some_trait.get_trait_impl(some_trait_i32).unwrap(); + + // Check Type::as_string + let str_type = quote { str<10> }.as_type(); + let constant = str_type.as_string().unwrap(); + assert_eq(constant.as_constant().unwrap(), 10); } } From 97076d4c85f2d22e78f87b77aefc72a3a2e866bd Mon Sep 17 00:00:00 2001 From: jfecher Date: Fri, 30 Aug 2024 15:26:16 -0500 Subject: [PATCH 2/2] Apply suggestions from code review --- .../src/hir/comptime/interpreter/builtin.rs | 6 +++--- docs/docs/noir/standard_library/meta/typ.md | 6 +++--- noir_stdlib/src/meta/typ.nr | 8 ++++---- .../compile_success_empty/comptime_type/src/main.nr | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 6d3c92d8cd4..20cd5e949e5 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -140,7 +140,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "type_as_constant" => type_as_constant(arguments, return_type, location), "type_as_integer" => type_as_integer(arguments, return_type, location), "type_as_slice" => type_as_slice(arguments, return_type, location), - "type_as_string" => type_as_string(arguments, return_type, location), + "type_as_str" => type_as_str(arguments, return_type, location), "type_as_struct" => type_as_struct(arguments, return_type, location), "type_as_tuple" => type_as_tuple(arguments, return_type, location), "type_eq" => type_eq(arguments, location), @@ -540,8 +540,8 @@ fn type_as_slice( }) } -// fn as_string(self) -> Option -fn type_as_string( +// fn as_str(self) -> Option +fn type_as_str( arguments: Vec<(Value, Location)>, return_type: Type, location: Location, diff --git a/docs/docs/noir/standard_library/meta/typ.md b/docs/docs/noir/standard_library/meta/typ.md index e38be284401..0b6f8d5f77d 100644 --- a/docs/docs/noir/standard_library/meta/typ.md +++ b/docs/docs/noir/standard_library/meta/typ.md @@ -45,11 +45,11 @@ if the type is signed, as well as the number of bits of this integer type. If this is a slice type, return the element type of the slice. -### as_string +### as_str -#include_code as_string noir_stdlib/src/meta/typ.nr rust +#include_code as_str noir_stdlib/src/meta/typ.nr rust -If this is a string type, returns its length as a type. +If this is a `str` type, returns the length `N` as a type. ### as_struct diff --git a/noir_stdlib/src/meta/typ.nr b/noir_stdlib/src/meta/typ.nr index 35b5145b5ac..12dc91a4925 100644 --- a/noir_stdlib/src/meta/typ.nr +++ b/noir_stdlib/src/meta/typ.nr @@ -22,10 +22,10 @@ impl Type { fn as_slice(self) -> Option {} // docs:end:as_slice - #[builtin(type_as_string)] -// docs:start:as_string - fn as_string(self) -> Option {} - // docs:end:as_string + #[builtin(type_as_str)] +// docs:start:as_str + fn as_str(self) -> Option {} + // docs:end:as_str #[builtin(type_as_struct)] // docs:start:as_struct diff --git a/test_programs/compile_success_empty/comptime_type/src/main.nr b/test_programs/compile_success_empty/comptime_type/src/main.nr index 4b6eb6f3933..0b15c5605b3 100644 --- a/test_programs/compile_success_empty/comptime_type/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type/src/main.nr @@ -111,9 +111,9 @@ fn main() { let _trait_impl = struct_implements_some_trait.get_trait_impl(some_trait_i32).unwrap(); - // Check Type::as_string + // Check Type::as_str let str_type = quote { str<10> }.as_type(); - let constant = str_type.as_string().unwrap(); + let constant = str_type.as_str().unwrap(); assert_eq(constant.as_constant().unwrap(), 10); } }