Skip to content

Commit 6adb490

Browse files
authored
Merge f84bca0 into 1b72a17
2 parents 1b72a17 + f84bca0 commit 6adb490

File tree

4 files changed

+30
-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_struct_definition/src

4 files changed

+30
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
147147
struct_def_has_named_attribute(interner, arguments, location)
148148
}
149149
"struct_def_module" => struct_def_module(self, arguments, location),
150+
"struct_def_name" => struct_def_name(interner, arguments, location),
150151
"struct_def_set_fields" => struct_def_set_fields(interner, arguments, location),
151152
"to_le_radix" => to_le_radix(arguments, return_type, location),
152153
"trait_constraint_eq" => trait_constraint_eq(interner, arguments, location),
@@ -423,6 +424,20 @@ fn struct_def_module(
423424
Ok(Value::ModuleDefinition(parent))
424425
}
425426

427+
// fn name(self) -> Quoted
428+
fn struct_def_name(
429+
interner: &NodeInterner,
430+
arguments: Vec<(Value, Location)>,
431+
location: Location,
432+
) -> IResult<Value> {
433+
let self_argument = check_one_argument(arguments, location)?;
434+
let struct_id = get_struct(self_argument)?;
435+
let the_struct = interner.get_struct(struct_id);
436+
437+
let name = Token::Ident(the_struct.borrow().name.to_string());
438+
Ok(Value::Quoted(Rc::new(vec![name])))
439+
}
440+
426441
/// fn set_fields(self, new_fields: [(Quoted, Type)]) {}
427442
/// Returns (name, type) pairs of each field of this StructDefinition
428443
fn struct_def_set_fields(

docs/docs/noir/standard_library/meta/struct_def.md

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ Returns true if this struct has a custom attribute with the given name.
6262

6363
Returns the module where the struct is defined.
6464

65+
### name
66+
67+
#include_code name noir_stdlib/src/meta/struct_def.nr rust
68+
69+
Returns the name of this struct
70+
71+
Note that the returned quoted value will be just the struct name, it will
72+
not be the full path to the struct, nor will it include any generics.
73+
6574
### set_fields
6675

6776
#include_code set_fields noir_stdlib/src/meta/struct_def.nr rust

noir_stdlib/src/meta/struct_def.nr

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ impl StructDefinition {
3434
fn module(self) -> Module {}
3535
// docs:end:module
3636

37+
#[builtin(struct_def_name)]
38+
// docs:start:name
39+
fn name(self) -> Quoted {}
40+
// docs:end:name
41+
3742
/// Sets the fields of this struct to the given fields list.
3843
/// All existing fields of the struct will be overridden with the given fields.
3944
/// Each element of the fields list corresponds to the name and type of a field.

test_programs/compile_success_empty/comptime_struct_definition/src/main.nr

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ comptime fn my_comptime_fn(typ: StructDefinition) {
1313
let _ = typ.as_type();
1414
assert_eq(typ.generics().len(), 3);
1515
assert_eq(typ.fields().len(), 2);
16+
assert_eq(typ.name(), quote { MyType });
1617
}
1718

1819
comptime fn mutate_struct_fields(s: StructDefinition) {

0 commit comments

Comments
 (0)