diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 4276f07641c..e6ef685e278 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -147,6 +147,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { struct_def_has_named_attribute(interner, arguments, location) } "struct_def_module" => struct_def_module(self, arguments, location), + "struct_def_name" => struct_def_name(interner, arguments, location), "struct_def_set_fields" => struct_def_set_fields(interner, arguments, location), "to_le_radix" => to_le_radix(arguments, return_type, location), "trait_constraint_eq" => trait_constraint_eq(interner, arguments, location), @@ -423,6 +424,20 @@ fn struct_def_module( Ok(Value::ModuleDefinition(parent)) } +// fn name(self) -> Quoted +fn struct_def_name( + interner: &NodeInterner, + arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult { + let self_argument = check_one_argument(arguments, location)?; + let struct_id = get_struct(self_argument)?; + let the_struct = interner.get_struct(struct_id); + + let name = Token::Ident(the_struct.borrow().name.to_string()); + Ok(Value::Quoted(Rc::new(vec![name]))) +} + /// fn set_fields(self, new_fields: [(Quoted, Type)]) {} /// Returns (name, type) pairs of each field of this StructDefinition fn struct_def_set_fields( diff --git a/docs/docs/noir/standard_library/meta/struct_def.md b/docs/docs/noir/standard_library/meta/struct_def.md index 221f7fc77e3..425f74a7484 100644 --- a/docs/docs/noir/standard_library/meta/struct_def.md +++ b/docs/docs/noir/standard_library/meta/struct_def.md @@ -62,6 +62,15 @@ Returns true if this struct has a custom attribute with the given name. Returns the module where the struct is defined. +### name + +#include_code name noir_stdlib/src/meta/struct_def.nr rust + +Returns the name of this struct + +Note that the returned quoted value will be just the struct name, it will +not be the full path to the struct, nor will it include any generics. + ### set_fields #include_code set_fields noir_stdlib/src/meta/struct_def.nr rust diff --git a/noir_stdlib/src/meta/struct_def.nr b/noir_stdlib/src/meta/struct_def.nr index aa71fd6696f..85832b00770 100644 --- a/noir_stdlib/src/meta/struct_def.nr +++ b/noir_stdlib/src/meta/struct_def.nr @@ -34,6 +34,11 @@ impl StructDefinition { fn module(self) -> Module {} // docs:end:module + #[builtin(struct_def_name)] + // docs:start:name + fn name(self) -> Quoted {} + // docs:end:name + /// Sets the fields of this struct to the given fields list. /// All existing fields of the struct will be overridden with the given fields. /// Each element of the fields list corresponds to the name and type of a field. diff --git a/test_programs/compile_success_empty/comptime_struct_definition/src/main.nr b/test_programs/compile_success_empty/comptime_struct_definition/src/main.nr index f2be2bd0d5c..758330cf7b7 100644 --- a/test_programs/compile_success_empty/comptime_struct_definition/src/main.nr +++ b/test_programs/compile_success_empty/comptime_struct_definition/src/main.nr @@ -13,6 +13,7 @@ comptime fn my_comptime_fn(typ: StructDefinition) { let _ = typ.as_type(); assert_eq(typ.generics().len(), 3); assert_eq(typ.fields().len(), 2); + assert_eq(typ.name(), quote { MyType }); } comptime fn mutate_struct_fields(s: StructDefinition) {