diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 0dafe408c7f..20cd5e949e5 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_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), @@ -539,6 +540,21 @@ fn type_as_slice( }) } +// fn as_str(self) -> Option +fn type_as_str( + 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..0b6f8d5f77d 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_str + +#include_code as_str noir_stdlib/src/meta/typ.nr rust + +If this is a `str` type, returns the length `N` 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..12dc91a4925 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_str)] +// docs:start:as_str + fn as_str(self) -> Option {} + // docs:end:as_str + #[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..0b15c5605b3 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_str + let str_type = quote { str<10> }.as_type(); + let constant = str_type.as_str().unwrap(); + assert_eq(constant.as_constant().unwrap(), 10); } }