Skip to content

Commit e3f8398

Browse files
authored
chore: Rename StructDefinition to TypeDefinition (#7614)
1 parent ba5beb6 commit e3f8398

File tree

36 files changed

+215
-212
lines changed

36 files changed

+215
-212
lines changed

compiler/noirc_frontend/src/elaborator/comptime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl<'context> Elaborator<'context> {
533533

534534
for (struct_id, struct_def) in types {
535535
let attributes = &struct_def.struct_def.attributes;
536-
let item = Value::StructDefinition(*struct_id);
536+
let item = Value::TypeDefinition(*struct_id);
537537
let context = AttributeContext::new(struct_def.module_id);
538538
self.collect_comptime_attributes_on_item(
539539
attributes,

compiler/noirc_frontend/src/hir/comptime/display.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl Display for ValuePrinter<'_, '_> {
417417
write!(f, "&[{}]", values.join(", "))
418418
}
419419
Value::Quoted(tokens) => display_quoted(tokens, 0, self.interner, f),
420-
Value::StructDefinition(id) => {
420+
Value::TypeDefinition(id) => {
421421
let def = self.interner.get_type(*id);
422422
let def = def.borrow();
423423
write!(f, "{}", def.name)

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

+63-65
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use builtin_helpers::{
55
block_expression_to_value, byte_array_type, check_argument_count,
66
check_function_not_yet_resolved, check_one_argument, check_three_arguments,
77
check_two_arguments, get_bool, get_expr, get_field, get_format_string, get_function_def,
8-
get_module, get_quoted, get_slice, get_struct, get_trait_constraint, get_trait_def,
9-
get_trait_impl, get_tuple, get_type, get_typed_expr, get_u32, get_unresolved_type,
8+
get_module, get_quoted, get_slice, get_trait_constraint, get_trait_def, get_trait_impl,
9+
get_tuple, get_type, get_type_id, get_typed_expr, get_u32, get_unresolved_type,
1010
has_named_attribute, hir_pattern_to_tokens, mutate_func_meta_type, parse, quote_ident,
1111
replace_func_meta_parameters, replace_func_meta_return_type,
1212
};
@@ -182,24 +182,6 @@ impl Interpreter<'_, '_> {
182182
"static_assert" => static_assert(interner, arguments, location, call_stack),
183183
"str_as_bytes" => str_as_bytes(interner, arguments, location),
184184
"str_as_ctstring" => str_as_ctstring(interner, arguments, location),
185-
"struct_def_add_attribute" => struct_def_add_attribute(interner, arguments, location),
186-
"struct_def_add_generic" => struct_def_add_generic(interner, arguments, location),
187-
"struct_def_as_type" => struct_def_as_type(interner, arguments, location),
188-
"struct_def_eq" => struct_def_eq(arguments, location),
189-
"struct_def_fields" => struct_def_fields(interner, arguments, location, call_stack),
190-
"struct_def_fields_as_written" => {
191-
struct_def_fields_as_written(interner, arguments, location)
192-
}
193-
"struct_def_generics" => {
194-
struct_def_generics(interner, arguments, return_type, location)
195-
}
196-
"struct_def_has_named_attribute" => {
197-
struct_def_has_named_attribute(interner, arguments, location)
198-
}
199-
"struct_def_hash" => struct_def_hash(arguments, location),
200-
"struct_def_module" => struct_def_module(self, arguments, location),
201-
"struct_def_name" => struct_def_name(interner, arguments, location),
202-
"struct_def_set_fields" => struct_def_set_fields(interner, arguments, location),
203185
"to_be_radix" => to_be_radix(arguments, return_type, location),
204186
"to_le_radix" => to_le_radix(arguments, return_type, location),
205187
"to_be_bits" => to_be_bits(arguments, return_type, location),
@@ -223,8 +205,24 @@ impl Interpreter<'_, '_> {
223205
}
224206
"type_as_slice" => type_as_slice(arguments, return_type, location),
225207
"type_as_str" => type_as_str(arguments, return_type, location),
226-
"type_as_struct" => type_as_struct(arguments, return_type, location),
208+
"type_as_data_type" => type_as_data_type(arguments, return_type, location),
227209
"type_as_tuple" => type_as_tuple(arguments, return_type, location),
210+
"type_def_add_attribute" => type_def_add_attribute(interner, arguments, location),
211+
"type_def_add_generic" => type_def_add_generic(interner, arguments, location),
212+
"type_def_as_type" => type_def_as_type(interner, arguments, location),
213+
"type_def_eq" => type_def_eq(arguments, location),
214+
"type_def_fields" => type_def_fields(interner, arguments, location, call_stack),
215+
"type_def_fields_as_written" => {
216+
type_def_fields_as_written(interner, arguments, location)
217+
}
218+
"type_def_generics" => type_def_generics(interner, arguments, return_type, location),
219+
"type_def_has_named_attribute" => {
220+
type_def_has_named_attribute(interner, arguments, location)
221+
}
222+
"type_def_hash" => type_def_hash(arguments, location),
223+
"type_def_module" => type_def_module(self, arguments, location),
224+
"type_def_name" => type_def_name(interner, arguments, location),
225+
"type_def_set_fields" => type_def_set_fields(interner, arguments, location),
228226
"type_eq" => type_eq(arguments, location),
229227
"type_get_trait_impl" => {
230228
type_get_trait_impl(interner, arguments, return_type, location)
@@ -379,7 +377,7 @@ fn str_as_ctstring(
379377
}
380378

381379
// fn add_attribute<let N: u32>(self, attribute: str<N>)
382-
fn struct_def_add_attribute(
380+
fn type_def_add_attribute(
383381
interner: &mut NodeInterner,
384382
arguments: Vec<(Value, Location)>,
385383
location: Location,
@@ -396,16 +394,16 @@ fn struct_def_add_attribute(
396394
});
397395
};
398396

399-
let struct_id = get_struct(self_argument)?;
400-
interner.update_type_attributes(struct_id, |attributes| {
397+
let type_id = get_type_id(self_argument)?;
398+
interner.update_type_attributes(type_id, |attributes| {
401399
attributes.push(attribute);
402400
});
403401

404402
Ok(Value::Unit)
405403
}
406404

407405
// fn add_generic<let N: u32>(self, generic_name: str<N>)
408-
fn struct_def_add_generic(
406+
fn type_def_add_generic(
409407
interner: &NodeInterner,
410408
arguments: Vec<(Value, Location)>,
411409
location: Location,
@@ -429,7 +427,7 @@ fn struct_def_add_generic(
429427
});
430428
};
431429

432-
let struct_id = get_struct(self_argument)?;
430+
let struct_id = get_type_id(self_argument)?;
433431
let the_struct = interner.get_type(struct_id);
434432
let mut the_struct = the_struct.borrow_mut();
435433
let name = Rc::new(generic_name);
@@ -455,35 +453,35 @@ fn struct_def_add_generic(
455453
}
456454

457455
/// fn as_type(self) -> Type
458-
fn struct_def_as_type(
456+
fn type_def_as_type(
459457
interner: &NodeInterner,
460458
arguments: Vec<(Value, Location)>,
461459
location: Location,
462460
) -> IResult<Value> {
463461
let argument = check_one_argument(arguments, location)?;
464-
let struct_id = get_struct(argument)?;
465-
let struct_def_rc = interner.get_type(struct_id);
466-
let struct_def = struct_def_rc.borrow();
462+
let struct_id = get_type_id(argument)?;
463+
let type_def_rc = interner.get_type(struct_id);
464+
let type_def = type_def_rc.borrow();
467465

468-
let generics = vecmap(&struct_def.generics, |generic| {
466+
let generics = vecmap(&type_def.generics, |generic| {
469467
Type::NamedGeneric(generic.type_var.clone(), generic.name.clone())
470468
});
471469

472-
drop(struct_def);
473-
Ok(Value::Type(Type::DataType(struct_def_rc, generics)))
470+
drop(type_def);
471+
Ok(Value::Type(Type::DataType(type_def_rc, generics)))
474472
}
475473

476474
/// fn generics(self) -> [(Type, Option<Type>)]
477-
fn struct_def_generics(
475+
fn type_def_generics(
478476
interner: &NodeInterner,
479477
arguments: Vec<(Value, Location)>,
480478
return_type: Type,
481479
location: Location,
482480
) -> IResult<Value> {
483481
let argument = check_one_argument(arguments, location)?;
484-
let struct_id = get_struct(argument)?;
485-
let struct_def = interner.get_type(struct_id);
486-
let struct_def = struct_def.borrow();
482+
let type_id = get_type_id(argument)?;
483+
let type_def = interner.get_type(type_id);
484+
let type_def = type_def.borrow();
487485

488486
let expected = Type::Slice(Box::new(Type::Tuple(vec![
489487
Type::Quoted(QuotedType::Type),
@@ -502,7 +500,7 @@ fn struct_def_generics(
502500
_ => return Err(InterpreterError::TypeMismatch { expected, actual, location }),
503501
};
504502

505-
let generics = struct_def
503+
let generics = type_def
506504
.generics
507505
.iter()
508506
.map(|generic| {
@@ -519,39 +517,39 @@ fn struct_def_generics(
519517
Ok(Value::Slice(generics, slice_item_type))
520518
}
521519

522-
fn struct_def_hash(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
523-
hash_item(arguments, location, get_struct)
520+
fn type_def_hash(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
521+
hash_item(arguments, location, get_type_id)
524522
}
525523

526-
fn struct_def_eq(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
527-
eq_item(arguments, location, get_struct)
524+
fn type_def_eq(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
525+
eq_item(arguments, location, get_type_id)
528526
}
529527

530528
// fn has_named_attribute<let N: u32>(self, name: str<N>) -> bool {}
531-
fn struct_def_has_named_attribute(
529+
fn type_def_has_named_attribute(
532530
interner: &NodeInterner,
533531
arguments: Vec<(Value, Location)>,
534532
location: Location,
535533
) -> IResult<Value> {
536534
let (self_argument, name) = check_two_arguments(arguments, location)?;
537-
let struct_id = get_struct(self_argument)?;
535+
let type_id = get_type_id(self_argument)?;
538536

539537
let name = get_str(interner, name)?;
540538

541-
Ok(Value::Bool(has_named_attribute(&name, interner.type_attributes(&struct_id))))
539+
Ok(Value::Bool(has_named_attribute(&name, interner.type_attributes(&type_id))))
542540
}
543541

544542
/// fn fields(self, generic_args: [Type]) -> [(Quoted, Type)]
545-
/// Returns (name, type) pairs of each field of this StructDefinition.
543+
/// Returns (name, type) pairs of each field of this TypeDefinition.
546544
/// Applies the given generic arguments to each field.
547-
fn struct_def_fields(
545+
fn type_def_fields(
548546
interner: &mut NodeInterner,
549547
arguments: Vec<(Value, Location)>,
550548
location: Location,
551549
call_stack: &im::Vector<Location>,
552550
) -> IResult<Value> {
553551
let (typ, generic_args) = check_two_arguments(arguments, location)?;
554-
let struct_id = get_struct(typ)?;
552+
let struct_id = get_type_id(typ)?;
555553
let struct_def = interner.get_type(struct_id);
556554
let struct_def = struct_def.borrow();
557555

@@ -565,7 +563,7 @@ fn struct_def_fields(
565563
let s = if expected == 1 { "" } else { "s" };
566564
let was_were = if actual == 1 { "was" } else { "were" };
567565
let message = Some(format!(
568-
"`StructDefinition::fields` expected {expected} generic{s} for `{}` but {actual} {was_were} given",
566+
"`TypeDefinition::fields` expected {expected} generic{s} for `{}` but {actual} {was_were} given",
569567
struct_def.name
570568
));
571569
let location = args_location;
@@ -591,16 +589,16 @@ fn struct_def_fields(
591589
}
592590

593591
/// fn fields_as_written(self) -> [(Quoted, Type)]
594-
/// Returns (name, type) pairs of each field of this StructDefinition.
592+
/// Returns (name, type) pairs of each field of this TypeDefinition.
595593
///
596594
/// Note that any generic arguments won't be applied: if you need them to be, use `fields`.
597-
fn struct_def_fields_as_written(
595+
fn type_def_fields_as_written(
598596
interner: &mut NodeInterner,
599597
arguments: Vec<(Value, Location)>,
600598
location: Location,
601599
) -> IResult<Value> {
602600
let argument = check_one_argument(arguments, location)?;
603-
let struct_id = get_struct(argument)?;
601+
let struct_id = get_type_id(argument)?;
604602
let struct_def = interner.get_type(struct_id);
605603
let struct_def = struct_def.borrow();
606604

@@ -623,25 +621,25 @@ fn struct_def_fields_as_written(
623621
}
624622

625623
// fn module(self) -> Module
626-
fn struct_def_module(
624+
fn type_def_module(
627625
interpreter: &Interpreter,
628626
arguments: Vec<(Value, Location)>,
629627
location: Location,
630628
) -> IResult<Value> {
631629
let self_argument = check_one_argument(arguments, location)?;
632-
let struct_id = get_struct(self_argument)?;
630+
let struct_id = get_type_id(self_argument)?;
633631
let parent = struct_id.parent_module_id(interpreter.elaborator.def_maps);
634632
Ok(Value::ModuleDefinition(parent))
635633
}
636634

637635
// fn name(self) -> Quoted
638-
fn struct_def_name(
636+
fn type_def_name(
639637
interner: &NodeInterner,
640638
arguments: Vec<(Value, Location)>,
641639
location: Location,
642640
) -> IResult<Value> {
643641
let self_argument = check_one_argument(arguments, location)?;
644-
let struct_id = get_struct(self_argument)?;
642+
let struct_id = get_type_id(self_argument)?;
645643
let the_struct = interner.get_type(struct_id);
646644

647645
let name = Token::Ident(the_struct.borrow().name.to_string());
@@ -650,14 +648,14 @@ fn struct_def_name(
650648
}
651649

652650
/// fn set_fields(self, new_fields: [(Quoted, Type)]) {}
653-
/// Returns (name, type) pairs of each field of this StructDefinition
654-
fn struct_def_set_fields(
651+
/// Returns (name, type) pairs of each field of this TypeDefinition
652+
fn type_def_set_fields(
655653
interner: &mut NodeInterner,
656654
arguments: Vec<(Value, Location)>,
657655
location: Location,
658656
) -> IResult<Value> {
659657
let (the_struct, fields) = check_two_arguments(arguments, location)?;
660-
let struct_id = get_struct(the_struct)?;
658+
let struct_id = get_type_id(the_struct)?;
661659

662660
let struct_def = interner.get_type(struct_id);
663661
let mut struct_def = struct_def.borrow_mut();
@@ -1076,16 +1074,16 @@ fn type_as_str(
10761074
})
10771075
}
10781076

1079-
// fn as_struct(self) -> Option<(StructDefinition, [Type])>
1080-
fn type_as_struct(
1077+
// fn as_data_type(self) -> Option<(TypeDefinition, [Type])>
1078+
fn type_as_data_type(
10811079
arguments: Vec<(Value, Location)>,
10821080
return_type: Type,
10831081
location: Location,
10841082
) -> IResult<Value> {
10851083
type_as(arguments, return_type, location, |typ| {
10861084
if let Type::DataType(struct_type, generics) = typ {
10871085
Some(Value::Tuple(vec![
1088-
Value::StructDefinition(struct_type.borrow().id),
1086+
Value::TypeDefinition(struct_type.borrow().id),
10891087
Value::Slice(
10901088
generics.into_iter().map(Value::Type).collect(),
10911089
Type::Slice(Box::new(Type::Quoted(QuotedType::Type))),
@@ -2841,7 +2839,7 @@ fn module_functions(
28412839
Ok(Value::Slice(func_ids, slice_type))
28422840
}
28432841

2844-
// fn structs(self) -> [StructDefinition]
2842+
// fn structs(self) -> [TypeDefinition]
28452843
fn module_structs(
28462844
interpreter: &Interpreter,
28472845
arguments: Vec<(Value, Location)>,
@@ -2856,14 +2854,14 @@ fn module_structs(
28562854
.iter()
28572855
.filter_map(|module_def_id| {
28582856
if let ModuleDefId::TypeId(id) = module_def_id {
2859-
Some(Value::StructDefinition(*id))
2857+
Some(Value::TypeDefinition(*id))
28602858
} else {
28612859
None
28622860
}
28632861
})
28642862
.collect();
28652863

2866-
let slice_type = Type::Slice(Box::new(Type::Quoted(QuotedType::StructDefinition)));
2864+
let slice_type = Type::Slice(Box::new(Type::Quoted(QuotedType::TypeDefinition)));
28672865
Ok(Value::Slice(struct_ids, slice_type))
28682866
}
28692867

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ pub(crate) fn get_module((value, location): (Value, Location)) -> IResult<Module
328328
}
329329
}
330330

331-
pub(crate) fn get_struct((value, location): (Value, Location)) -> IResult<TypeId> {
331+
pub(crate) fn get_type_id((value, location): (Value, Location)) -> IResult<TypeId> {
332332
match value {
333-
Value::StructDefinition(id) => Ok(id),
334-
_ => type_mismatch(value, Type::Quoted(QuotedType::StructDefinition), location),
333+
Value::TypeDefinition(id) => Ok(id),
334+
_ => type_mismatch(value, Type::Quoted(QuotedType::TypeDefinition), location),
335335
}
336336
}
337337

compiler/noirc_frontend/src/hir/comptime/value.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub enum Value {
6565
Array(Vector<Value>, Type),
6666
Slice(Vector<Value>, Type),
6767
Quoted(Rc<Vec<LocatedToken>>),
68-
StructDefinition(TypeId),
68+
TypeDefinition(TypeId),
6969
TraitConstraint(TraitId, TraitGenerics),
7070
TraitDefinition(TraitId),
7171
TraitImpl(TraitImplId),
@@ -150,7 +150,7 @@ impl Value {
150150
Value::Array(_, typ) => return Cow::Borrowed(typ),
151151
Value::Slice(_, typ) => return Cow::Borrowed(typ),
152152
Value::Quoted(_) => Type::Quoted(QuotedType::Quoted),
153-
Value::StructDefinition(_) => Type::Quoted(QuotedType::StructDefinition),
153+
Value::TypeDefinition(_) => Type::Quoted(QuotedType::TypeDefinition),
154154
Value::Pointer(element, auto_deref, mutable) => {
155155
if *auto_deref {
156156
element.borrow().get_type().into_owned()
@@ -321,7 +321,7 @@ impl Value {
321321
}
322322
Value::TypedExpr(..)
323323
| Value::Pointer(..)
324-
| Value::StructDefinition(_)
324+
| Value::TypeDefinition(_)
325325
| Value::TraitConstraint(..)
326326
| Value::TraitDefinition(_)
327327
| Value::TraitImpl(_)
@@ -459,7 +459,7 @@ impl Value {
459459
Value::TypedExpr(TypedExpr::StmtId(..))
460460
| Value::Expr(..)
461461
| Value::Pointer(..)
462-
| Value::StructDefinition(_)
462+
| Value::TypeDefinition(_)
463463
| Value::TraitConstraint(..)
464464
| Value::TraitDefinition(_)
465465
| Value::TraitImpl(_)

0 commit comments

Comments
 (0)