diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index ae8237706cc..e0671d6f7ff 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -1270,7 +1270,7 @@ impl<'context> Elaborator<'context> { Interpreter::new(self.interner, &mut self.comptime_scopes, self.crate_id); let location = Location::new(span, self.file); - let arguments = vec![(Value::TypeDefinition(struct_id), location)]; + let arguments = vec![(Value::StructDefinition(struct_id), location)]; let value = interpreter .call_function(function, arguments, TypeBindings::new(), location) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 1c0c4e6f274..8523e13aeea 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -19,9 +19,9 @@ pub(super) fn call_builtin( "array_len" => array_len(&arguments), "as_slice" => as_slice(arguments), "slice_push_back" => slice_push_back(arguments), - "type_def_as_type" => type_def_as_type(interner, arguments), - "type_def_generics" => type_def_generics(interner, arguments), - "type_def_fields" => type_def_fields(interner, arguments), + "struct_def_as_type" => struct_def_as_type(interner, arguments), + "struct_def_generics" => struct_def_generics(interner, arguments), + "struct_def_fields" => struct_def_fields(interner, arguments), _ => { let item = format!("Comptime evaluation for builtin function {name}"); Err(InterpreterError::Unimplemented { item, location }) @@ -63,19 +63,19 @@ fn slice_push_back(mut arguments: Vec<(Value, Location)>) -> IResult { } /// fn as_type(self) -> Quoted -fn type_def_as_type( +fn struct_def_as_type( interner: &NodeInterner, mut arguments: Vec<(Value, Location)>, ) -> IResult { assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument"); - let (type_def, span) = match arguments.pop() { - Some((Value::TypeDefinition(id), location)) => (id, location.span), + let (struct_def, span) = match arguments.pop() { + Some((Value::StructDefinition(id), location)) => (id, location.span), other => { - unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}") + unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}") } }; - let struct_def = interner.get_struct(type_def); + let struct_def = interner.get_struct(struct_def); let struct_def = struct_def.borrow(); let make_token = |name| SpannedToken::new(Token::Ident(name), span); @@ -92,19 +92,19 @@ fn type_def_as_type( } /// fn generics(self) -> [Quoted] -fn type_def_generics( +fn struct_def_generics( interner: &NodeInterner, mut arguments: Vec<(Value, Location)>, ) -> IResult { assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument"); - let (type_def, span) = match arguments.pop() { - Some((Value::TypeDefinition(id), location)) => (id, location.span), + let (struct_def, span) = match arguments.pop() { + Some((Value::StructDefinition(id), location)) => (id, location.span), other => { - unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}") + unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}") } }; - let struct_def = interner.get_struct(type_def); + let struct_def = interner.get_struct(struct_def); let generics = struct_def .borrow() @@ -121,20 +121,20 @@ fn type_def_generics( } /// fn fields(self) -> [(Quoted, Quoted)] -/// Returns (name, type) pairs of each field of this TypeDefinition -fn type_def_fields( +/// Returns (name, type) pairs of each field of this StructDefinition +fn struct_def_fields( interner: &mut NodeInterner, mut arguments: Vec<(Value, Location)>, ) -> IResult { assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument"); - let (type_def, span) = match arguments.pop() { - Some((Value::TypeDefinition(id), location)) => (id, location.span), + let (struct_def, span) = match arguments.pop() { + Some((Value::StructDefinition(id), location)) => (id, location.span), other => { - unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}") + unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}") } }; - let struct_def = interner.get_struct(type_def); + let struct_def = interner.get_struct(struct_def); let struct_def = struct_def.borrow(); let make_token = |name| SpannedToken::new(Token::Ident(name), span); diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index c956cdb5796..adb13c4bfbc 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -44,7 +44,7 @@ pub enum Value { Array(Vector, Type), Slice(Vector, Type), Code(Rc), - TypeDefinition(StructId), + StructDefinition(StructId), } impl Value { @@ -74,7 +74,7 @@ impl Value { Value::Array(_, typ) => return Cow::Borrowed(typ), Value::Slice(_, typ) => return Cow::Borrowed(typ), Value::Code(_) => Type::Quoted(QuotedType::Quoted), - Value::TypeDefinition(_) => Type::Quoted(QuotedType::TypeDefinition), + Value::StructDefinition(_) => Type::Quoted(QuotedType::StructDefinition), Value::Pointer(element) => { let element = element.borrow().get_type().into_owned(); Type::MutableReference(Box::new(element)) @@ -192,7 +192,7 @@ impl Value { } }; } - Value::Pointer(_) | Value::TypeDefinition(_) => { + Value::Pointer(_) | Value::StructDefinition(_) => { return Err(InterpreterError::CannotInlineMacro { value: self, location }) } }; @@ -298,7 +298,7 @@ impl Value { HirExpression::Literal(HirLiteral::Slice(HirArrayLiteral::Standard(elements))) } Value::Code(block) => HirExpression::Unquote(unwrap_rc(block)), - Value::Pointer(_) | Value::TypeDefinition(_) => { + Value::Pointer(_) | Value::StructDefinition(_) => { return Err(InterpreterError::CannotInlineMacro { value: self, location }) } }; @@ -398,7 +398,7 @@ impl Display for Value { } write!(f, " }}") } - Value::TypeDefinition(_) => write!(f, "(type definition)"), + Value::StructDefinition(_) => write!(f, "(struct definition)"), } } } diff --git a/compiler/noirc_frontend/src/hir_def/types.rs b/compiler/noirc_frontend/src/hir_def/types.rs index 0a7797c2bfb..b529ca17887 100644 --- a/compiler/noirc_frontend/src/hir_def/types.rs +++ b/compiler/noirc_frontend/src/hir_def/types.rs @@ -214,7 +214,7 @@ pub enum QuotedType { Quoted, TopLevelItem, Type, - TypeDefinition, + StructDefinition, } /// A list of TypeVariableIds to bind to a type. Storing the @@ -1176,7 +1176,7 @@ impl std::fmt::Display for QuotedType { QuotedType::Quoted => write!(f, "Quoted"), QuotedType::TopLevelItem => write!(f, "TopLevelItem"), QuotedType::Type => write!(f, "Type"), - QuotedType::TypeDefinition => write!(f, "TypeDefinition"), + QuotedType::StructDefinition => write!(f, "StructDefinition"), } } } diff --git a/compiler/noirc_frontend/src/lexer/token.rs b/compiler/noirc_frontend/src/lexer/token.rs index f98343ba52f..41de13fb17e 100644 --- a/compiler/noirc_frontend/src/lexer/token.rs +++ b/compiler/noirc_frontend/src/lexer/token.rs @@ -913,7 +913,7 @@ pub enum Keyword { Trait, Type, TypeType, - TypeDefinition, + StructDefinition, Unchecked, Unconstrained, Use, @@ -961,7 +961,7 @@ impl fmt::Display for Keyword { Keyword::Trait => write!(f, "trait"), Keyword::Type => write!(f, "type"), Keyword::TypeType => write!(f, "Type"), - Keyword::TypeDefinition => write!(f, "TypeDefinition"), + Keyword::StructDefinition => write!(f, "StructDefinition"), Keyword::Unchecked => write!(f, "unchecked"), Keyword::Unconstrained => write!(f, "unconstrained"), Keyword::Use => write!(f, "use"), @@ -1012,7 +1012,7 @@ impl Keyword { "trait" => Keyword::Trait, "type" => Keyword::Type, "Type" => Keyword::TypeType, - "TypeDefinition" => Keyword::TypeDefinition, + "StructDefinition" => Keyword::StructDefinition, "unchecked" => Keyword::Unchecked, "unconstrained" => Keyword::Unconstrained, "use" => Keyword::Use, diff --git a/compiler/noirc_frontend/src/parser/parser/types.rs b/compiler/noirc_frontend/src/parser/parser/types.rs index 493ebd1fb2f..32929312d54 100644 --- a/compiler/noirc_frontend/src/parser/parser/types.rs +++ b/compiler/noirc_frontend/src/parser/parser/types.rs @@ -25,7 +25,7 @@ pub(super) fn parse_type_inner<'a>( bool_type(), string_type(), expr_type(), - type_definition_type(), + struct_definition_type(), top_level_item_type(), type_of_quoted_types(), quoted_type(), @@ -80,10 +80,10 @@ pub(super) fn expr_type() -> impl NoirParser { .map_with_span(|_, span| UnresolvedTypeData::Quoted(QuotedType::Expr).with_span(span)) } -/// This is the type `TypeDefinition` - the type of a quoted type definition -pub(super) fn type_definition_type() -> impl NoirParser { - keyword(Keyword::TypeDefinition).map_with_span(|_, span| { - UnresolvedTypeData::Quoted(QuotedType::TypeDefinition).with_span(span) +/// This is the type `StructDefinition` - the type of a quoted struct definition +pub(super) fn struct_definition_type() -> impl NoirParser { + keyword(Keyword::StructDefinition).map_with_span(|_, span| { + UnresolvedTypeData::Quoted(QuotedType::StructDefinition).with_span(span) }) } diff --git a/noir_stdlib/src/meta/type_def.nr b/noir_stdlib/src/meta/type_def.nr index b9354485921..c01aab4b141 100644 --- a/noir_stdlib/src/meta/type_def.nr +++ b/noir_stdlib/src/meta/type_def.nr @@ -1,16 +1,16 @@ -impl TypeDefinition { - /// Return a syntactic version of this type definition as a type. +impl StructDefinition { + /// Return a syntactic version of this struct definition as a type. /// For example, `as_type(quote { type Foo { ... } })` would return `Foo` - #[builtin(type_def_as_type)] + #[builtin(struct_def_as_type)] fn as_type(self) -> Quoted {} - /// Return each generic on this type. The names of these generics are unchanged + /// Return each generic on this struct. The names of these generics are unchanged /// so users may need to keep name collisions in mind if this is used directly in a macro. - #[builtin(type_def_generics)] + #[builtin(struct_def_generics)] fn generics(self) -> [Quoted] {} - /// Returns (name, type) pairs of each field in this type. Each type is as-is + /// Returns (name, type) pairs of each field in this struct. Each type is as-is /// with any generic arguments unchanged. - #[builtin(type_def_fields)] + #[builtin(struct_def_fields)] fn fields(self) -> [(Quoted, Quoted)] {} } diff --git a/test_programs/compile_failure/type_definition_annotation/src/main.nr b/test_programs/compile_failure/type_definition_annotation/src/main.nr index 91f9c3a52f4..d4fef84442d 100644 --- a/test_programs/compile_failure/type_definition_annotation/src/main.nr +++ b/test_programs/compile_failure/type_definition_annotation/src/main.nr @@ -1,7 +1,7 @@ #[fail_assert] struct Foo { x: Field } -comptime fn fail_assert(_typ: TypeDefinition) { +comptime fn fail_assert(_typ: StructDefinition) { assert(false); } diff --git a/test_programs/compile_success_empty/comptime_type_definition/src/main.nr b/test_programs/compile_success_empty/comptime_type_definition/src/main.nr index 025f6a0b0bf..cdfc9bd6b75 100644 --- a/test_programs/compile_success_empty/comptime_type_definition/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type_definition/src/main.nr @@ -6,7 +6,7 @@ struct MyType { field2: (B, C), } -comptime fn my_comptime_fn(typ: TypeDefinition) { +comptime fn my_comptime_fn(typ: StructDefinition) { let _ = typ.as_type(); assert_eq(typ.generics().len(), 3); assert_eq(typ.fields().len(), 2); diff --git a/test_programs/compile_success_empty/derive_impl/src/main.nr b/test_programs/compile_success_empty/derive_impl/src/main.nr index 9636e4c7383..5463a61d969 100644 --- a/test_programs/compile_success_empty/derive_impl/src/main.nr +++ b/test_programs/compile_success_empty/derive_impl/src/main.nr @@ -1,4 +1,4 @@ -comptime fn derive_default(typ: TypeDefinition) -> Quoted { +comptime fn derive_default(typ: StructDefinition) -> Quoted { let generics: [Quoted] = typ.generics(); assert_eq( generics.len(), 0, "derive_default: Deriving Default on generic types is currently unimplemented"