Skip to content

Commit e29d4b3

Browse files
asteritejfecher
andauthored
feat: add Type::as_string (#5871)
# Description ## Problem Part of #5668 ## Summary ## Additional Context ## Documentation Check one: - [ ] No documentation needed. - [x] 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>
1 parent a950195 commit e29d4b3

File tree

4 files changed

+32
-0
lines changed
  • compiler/noirc_frontend/src/hir/comptime/interpreter
  • docs/docs/noir/standard_library/meta
  • noir_stdlib/src/meta
  • test_programs/compile_success_empty/comptime_type/src

4 files changed

+32
-0
lines changed

compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

+16
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
144144
"type_as_constant" => type_as_constant(arguments, return_type, location),
145145
"type_as_integer" => type_as_integer(arguments, return_type, location),
146146
"type_as_slice" => type_as_slice(arguments, return_type, location),
147+
"type_as_str" => type_as_str(arguments, return_type, location),
147148
"type_as_struct" => type_as_struct(arguments, return_type, location),
148149
"type_as_tuple" => type_as_tuple(arguments, return_type, location),
149150
"type_eq" => type_eq(arguments, location),
@@ -543,6 +544,21 @@ fn type_as_slice(
543544
})
544545
}
545546

547+
// fn as_str(self) -> Option<Type>
548+
fn type_as_str(
549+
arguments: Vec<(Value, Location)>,
550+
return_type: Type,
551+
location: Location,
552+
) -> IResult<Value> {
553+
type_as(arguments, return_type, location, |typ| {
554+
if let Type::String(n) = typ {
555+
Some(Value::Type(*n))
556+
} else {
557+
None
558+
}
559+
})
560+
}
561+
546562
// fn as_struct(self) -> Option<(StructDefinition, [Type])>
547563
fn type_as_struct(
548564
arguments: Vec<(Value, Location)>,

docs/docs/noir/standard_library/meta/typ.md

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ if the type is signed, as well as the number of bits of this integer type.
4545

4646
If this is a slice type, return the element type of the slice.
4747

48+
### as_str
49+
50+
#include_code as_str noir_stdlib/src/meta/typ.nr rust
51+
52+
If this is a `str<N>` type, returns the length `N` as a type.
53+
4854
### as_struct
4955

5056
#include_code as_struct noir_stdlib/src/meta/typ.nr rust

noir_stdlib/src/meta/typ.nr

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ impl Type {
2222
fn as_slice(self) -> Option<Type> {}
2323
// docs:end:as_slice
2424

25+
#[builtin(type_as_str)]
26+
// docs:start:as_str
27+
fn as_str(self) -> Option<Type> {}
28+
// docs:end:as_str
29+
2530
#[builtin(type_as_struct)]
2631
// docs:start:as_struct
2732
fn as_struct(self) -> Option<(StructDefinition, [Type])> {}

test_programs/compile_success_empty/comptime_type/src/main.nr

+5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ fn main() {
110110
assert(!struct_does_not_implement_some_trait.implements(some_trait_field));
111111

112112
let _trait_impl = struct_implements_some_trait.get_trait_impl(some_trait_i32).unwrap();
113+
114+
// Check Type::as_str
115+
let str_type = quote { str<10> }.as_type();
116+
let constant = str_type.as_str().unwrap();
117+
assert_eq(constant.as_constant().unwrap(), 10);
113118
}
114119
}
115120

0 commit comments

Comments
 (0)