diff --git a/compiler/noirc_frontend/src/elaborator/comptime.rs b/compiler/noirc_frontend/src/elaborator/comptime.rs index 7091f7b261c..850bc620d6b 100644 --- a/compiler/noirc_frontend/src/elaborator/comptime.rs +++ b/compiler/noirc_frontend/src/elaborator/comptime.rs @@ -526,7 +526,7 @@ impl<'context> Elaborator<'context> { for (struct_id, struct_def) in types { let attributes = &struct_def.struct_def.attributes; - let item = Value::StructDefinition(*struct_id); + let item = Value::TypeDefinition(*struct_id); let context = AttributeContext::new(struct_def.module_id); self.collect_comptime_attributes_on_item( attributes, diff --git a/compiler/noirc_frontend/src/hir/comptime/display.rs b/compiler/noirc_frontend/src/hir/comptime/display.rs index af85027389e..bc2e0574086 100644 --- a/compiler/noirc_frontend/src/hir/comptime/display.rs +++ b/compiler/noirc_frontend/src/hir/comptime/display.rs @@ -417,7 +417,7 @@ impl Display for ValuePrinter<'_, '_> { write!(f, "&[{}]", values.join(", ")) } Value::Quoted(tokens) => display_quoted(tokens, 0, self.interner, f), - Value::StructDefinition(id) => { + Value::TypeDefinition(id) => { let def = self.interner.get_type(*id); let def = def.borrow(); write!(f, "{}", def.name) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 77dd98dce60..97a0b67f3e5 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -5,8 +5,8 @@ use builtin_helpers::{ block_expression_to_value, byte_array_type, check_argument_count, check_function_not_yet_resolved, check_one_argument, check_three_arguments, check_two_arguments, get_bool, get_expr, get_field, get_format_string, get_function_def, - get_module, get_quoted, get_slice, get_struct, get_trait_constraint, get_trait_def, - get_trait_impl, get_tuple, get_type, get_typed_expr, get_u32, get_unresolved_type, + get_module, get_quoted, get_slice, get_trait_constraint, get_trait_def, get_trait_impl, + get_tuple, get_type, get_type_id, get_typed_expr, get_u32, get_unresolved_type, has_named_attribute, hir_pattern_to_tokens, mutate_func_meta_type, parse, quote_ident, replace_func_meta_parameters, replace_func_meta_return_type, }; @@ -180,24 +180,6 @@ impl Interpreter<'_, '_> { "static_assert" => static_assert(interner, arguments, location, call_stack), "str_as_bytes" => str_as_bytes(interner, arguments, location), "str_as_ctstring" => str_as_ctstring(interner, arguments, location), - "struct_def_add_attribute" => struct_def_add_attribute(interner, arguments, location), - "struct_def_add_generic" => struct_def_add_generic(interner, arguments, location), - "struct_def_as_type" => struct_def_as_type(interner, arguments, location), - "struct_def_eq" => struct_def_eq(arguments, location), - "struct_def_fields" => struct_def_fields(interner, arguments, location, call_stack), - "struct_def_fields_as_written" => { - struct_def_fields_as_written(interner, arguments, location) - } - "struct_def_generics" => { - struct_def_generics(interner, arguments, return_type, location) - } - "struct_def_has_named_attribute" => { - struct_def_has_named_attribute(interner, arguments, location) - } - "struct_def_hash" => struct_def_hash(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_be_radix" => to_be_radix(arguments, return_type, location), "to_le_radix" => to_le_radix(arguments, return_type, location), "to_be_bits" => to_be_bits(arguments, return_type, location), @@ -221,8 +203,24 @@ impl Interpreter<'_, '_> { } "type_as_slice" => type_as_slice(arguments, return_type, location), "type_as_str" => type_as_str(arguments, return_type, location), - "type_as_struct" => type_as_struct(arguments, return_type, location), + "type_as_data_type" => type_as_data_type(arguments, return_type, location), "type_as_tuple" => type_as_tuple(arguments, return_type, location), + "type_def_add_attribute" => type_def_add_attribute(interner, arguments, location), + "type_def_add_generic" => type_def_add_generic(interner, arguments, location), + "type_def_as_type" => type_def_as_type(interner, arguments, location), + "type_def_eq" => type_def_eq(arguments, location), + "type_def_fields" => type_def_fields(interner, arguments, location, call_stack), + "type_def_fields_as_written" => { + type_def_fields_as_written(interner, arguments, location) + } + "type_def_generics" => type_def_generics(interner, arguments, return_type, location), + "type_def_has_named_attribute" => { + type_def_has_named_attribute(interner, arguments, location) + } + "type_def_hash" => type_def_hash(arguments, location), + "type_def_module" => type_def_module(self, arguments, location), + "type_def_name" => type_def_name(interner, arguments, location), + "type_def_set_fields" => type_def_set_fields(interner, arguments, location), "type_eq" => type_eq(arguments, location), "type_get_trait_impl" => { type_get_trait_impl(interner, arguments, return_type, location) @@ -377,7 +375,7 @@ fn str_as_ctstring( } // fn add_attribute(self, attribute: str) -fn struct_def_add_attribute( +fn type_def_add_attribute( interner: &mut NodeInterner, arguments: Vec<(Value, Location)>, location: Location, @@ -394,8 +392,8 @@ fn struct_def_add_attribute( }); }; - let struct_id = get_struct(self_argument)?; - interner.update_type_attributes(struct_id, |attributes| { + let type_id = get_type_id(self_argument)?; + interner.update_type_attributes(type_id, |attributes| { attributes.push(attribute); }); @@ -403,7 +401,7 @@ fn struct_def_add_attribute( } // fn add_generic(self, generic_name: str) -fn struct_def_add_generic( +fn type_def_add_generic( interner: &NodeInterner, arguments: Vec<(Value, Location)>, location: Location, @@ -427,7 +425,7 @@ fn struct_def_add_generic( }); }; - let struct_id = get_struct(self_argument)?; + let struct_id = get_type_id(self_argument)?; let the_struct = interner.get_type(struct_id); let mut the_struct = the_struct.borrow_mut(); let name = Rc::new(generic_name); @@ -453,35 +451,35 @@ fn struct_def_add_generic( } /// fn as_type(self) -> Type -fn struct_def_as_type( +fn type_def_as_type( interner: &NodeInterner, arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { let argument = check_one_argument(arguments, location)?; - let struct_id = get_struct(argument)?; - let struct_def_rc = interner.get_type(struct_id); - let struct_def = struct_def_rc.borrow(); + let struct_id = get_type_id(argument)?; + let type_def_rc = interner.get_type(struct_id); + let type_def = type_def_rc.borrow(); - let generics = vecmap(&struct_def.generics, |generic| { + let generics = vecmap(&type_def.generics, |generic| { Type::NamedGeneric(generic.type_var.clone(), generic.name.clone()) }); - drop(struct_def); - Ok(Value::Type(Type::DataType(struct_def_rc, generics))) + drop(type_def); + Ok(Value::Type(Type::DataType(type_def_rc, generics))) } /// fn generics(self) -> [(Type, Option)] -fn struct_def_generics( +fn type_def_generics( interner: &NodeInterner, arguments: Vec<(Value, Location)>, return_type: Type, location: Location, ) -> IResult { let argument = check_one_argument(arguments, location)?; - let struct_id = get_struct(argument)?; - let struct_def = interner.get_type(struct_id); - let struct_def = struct_def.borrow(); + let type_id = get_type_id(argument)?; + let type_def = interner.get_type(type_id); + let type_def = type_def.borrow(); let expected = Type::Slice(Box::new(Type::Tuple(vec![ Type::Quoted(QuotedType::Type), @@ -500,7 +498,7 @@ fn struct_def_generics( _ => return Err(InterpreterError::TypeMismatch { expected, actual, location }), }; - let generics = struct_def + let generics = type_def .generics .iter() .map(|generic| { @@ -517,39 +515,39 @@ fn struct_def_generics( Ok(Value::Slice(generics, slice_item_type)) } -fn struct_def_hash(arguments: Vec<(Value, Location)>, location: Location) -> IResult { - hash_item(arguments, location, get_struct) +fn type_def_hash(arguments: Vec<(Value, Location)>, location: Location) -> IResult { + hash_item(arguments, location, get_type_id) } -fn struct_def_eq(arguments: Vec<(Value, Location)>, location: Location) -> IResult { - eq_item(arguments, location, get_struct) +fn type_def_eq(arguments: Vec<(Value, Location)>, location: Location) -> IResult { + eq_item(arguments, location, get_type_id) } // fn has_named_attribute(self, name: str) -> bool {} -fn struct_def_has_named_attribute( +fn type_def_has_named_attribute( interner: &NodeInterner, arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { let (self_argument, name) = check_two_arguments(arguments, location)?; - let struct_id = get_struct(self_argument)?; + let type_id = get_type_id(self_argument)?; let name = get_str(interner, name)?; - Ok(Value::Bool(has_named_attribute(&name, interner.type_attributes(&struct_id)))) + Ok(Value::Bool(has_named_attribute(&name, interner.type_attributes(&type_id)))) } /// fn fields(self, generic_args: [Type]) -> [(Quoted, Type)] -/// Returns (name, type) pairs of each field of this StructDefinition. +/// Returns (name, type) pairs of each field of this TypeDefinition. /// Applies the given generic arguments to each field. -fn struct_def_fields( +fn type_def_fields( interner: &mut NodeInterner, arguments: Vec<(Value, Location)>, location: Location, call_stack: &im::Vector, ) -> IResult { let (typ, generic_args) = check_two_arguments(arguments, location)?; - let struct_id = get_struct(typ)?; + let struct_id = get_type_id(typ)?; let struct_def = interner.get_type(struct_id); let struct_def = struct_def.borrow(); @@ -563,7 +561,7 @@ fn struct_def_fields( let s = if expected == 1 { "" } else { "s" }; let was_were = if actual == 1 { "was" } else { "were" }; let message = Some(format!( - "`StructDefinition::fields` expected {expected} generic{s} for `{}` but {actual} {was_were} given", + "`TypeDefinition::fields` expected {expected} generic{s} for `{}` but {actual} {was_were} given", struct_def.name )); let location = args_location; @@ -589,16 +587,16 @@ fn struct_def_fields( } /// fn fields_as_written(self) -> [(Quoted, Type)] -/// Returns (name, type) pairs of each field of this StructDefinition. +/// Returns (name, type) pairs of each field of this TypeDefinition. /// /// Note that any generic arguments won't be applied: if you need them to be, use `fields`. -fn struct_def_fields_as_written( +fn type_def_fields_as_written( interner: &mut NodeInterner, arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { let argument = check_one_argument(arguments, location)?; - let struct_id = get_struct(argument)?; + let struct_id = get_type_id(argument)?; let struct_def = interner.get_type(struct_id); let struct_def = struct_def.borrow(); @@ -621,25 +619,25 @@ fn struct_def_fields_as_written( } // fn module(self) -> Module -fn struct_def_module( +fn type_def_module( interpreter: &Interpreter, arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { let self_argument = check_one_argument(arguments, location)?; - let struct_id = get_struct(self_argument)?; + let struct_id = get_type_id(self_argument)?; let parent = struct_id.parent_module_id(interpreter.elaborator.def_maps); Ok(Value::ModuleDefinition(parent)) } // fn name(self) -> Quoted -fn struct_def_name( +fn type_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 struct_id = get_type_id(self_argument)?; let the_struct = interner.get_type(struct_id); let name = Token::Ident(the_struct.borrow().name.to_string()); @@ -648,14 +646,14 @@ fn struct_def_name( } /// fn set_fields(self, new_fields: [(Quoted, Type)]) {} -/// Returns (name, type) pairs of each field of this StructDefinition -fn struct_def_set_fields( +/// Returns (name, type) pairs of each field of this TypeDefinition +fn type_def_set_fields( interner: &mut NodeInterner, arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { let (the_struct, fields) = check_two_arguments(arguments, location)?; - let struct_id = get_struct(the_struct)?; + let struct_id = get_type_id(the_struct)?; let struct_def = interner.get_type(struct_id); let mut struct_def = struct_def.borrow_mut(); @@ -1068,8 +1066,8 @@ fn type_as_str( }) } -// fn as_struct(self) -> Option<(StructDefinition, [Type])> -fn type_as_struct( +// fn as_data_type(self) -> Option<(TypeDefinition, [Type])> +fn type_as_data_type( arguments: Vec<(Value, Location)>, return_type: Type, location: Location, @@ -1077,7 +1075,7 @@ fn type_as_struct( type_as(arguments, return_type, location, |typ| { if let Type::DataType(struct_type, generics) = typ { Some(Value::Tuple(vec![ - Value::StructDefinition(struct_type.borrow().id), + Value::TypeDefinition(struct_type.borrow().id), Value::Slice( generics.into_iter().map(Value::Type).collect(), Type::Slice(Box::new(Type::Quoted(QuotedType::Type))), @@ -2806,7 +2804,7 @@ fn module_functions( Ok(Value::Slice(func_ids, slice_type)) } -// fn structs(self) -> [StructDefinition] +// fn structs(self) -> [TypeDefinition] fn module_structs( interpreter: &Interpreter, arguments: Vec<(Value, Location)>, @@ -2821,14 +2819,14 @@ fn module_structs( .iter() .filter_map(|module_def_id| { if let ModuleDefId::TypeId(id) = module_def_id { - Some(Value::StructDefinition(*id)) + Some(Value::TypeDefinition(*id)) } else { None } }) .collect(); - let slice_type = Type::Slice(Box::new(Type::Quoted(QuotedType::StructDefinition))); + let slice_type = Type::Slice(Box::new(Type::Quoted(QuotedType::TypeDefinition))); Ok(Value::Slice(struct_ids, slice_type)) } diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs index e552cf0c5a2..3c5ff519933 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs @@ -328,10 +328,10 @@ pub(crate) fn get_module((value, location): (Value, Location)) -> IResult IResult { +pub(crate) fn get_type_id((value, location): (Value, Location)) -> IResult { match value { - Value::StructDefinition(id) => Ok(id), - _ => type_mismatch(value, Type::Quoted(QuotedType::StructDefinition), location), + Value::TypeDefinition(id) => Ok(id), + _ => type_mismatch(value, Type::Quoted(QuotedType::TypeDefinition), location), } } diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index ebaaf27f4f6..f2cfa8f11a9 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -65,7 +65,7 @@ pub enum Value { Array(Vector, Type), Slice(Vector, Type), Quoted(Rc>), - StructDefinition(TypeId), + TypeDefinition(TypeId), TraitConstraint(TraitId, TraitGenerics), TraitDefinition(TraitId), TraitImpl(TraitImplId), @@ -150,7 +150,7 @@ impl Value { Value::Array(_, typ) => return Cow::Borrowed(typ), Value::Slice(_, typ) => return Cow::Borrowed(typ), Value::Quoted(_) => Type::Quoted(QuotedType::Quoted), - Value::StructDefinition(_) => Type::Quoted(QuotedType::StructDefinition), + Value::TypeDefinition(_) => Type::Quoted(QuotedType::TypeDefinition), Value::Pointer(element, auto_deref, mutable) => { if *auto_deref { element.borrow().get_type().into_owned() @@ -321,7 +321,7 @@ impl Value { } Value::TypedExpr(..) | Value::Pointer(..) - | Value::StructDefinition(_) + | Value::TypeDefinition(_) | Value::TraitConstraint(..) | Value::TraitDefinition(_) | Value::TraitImpl(_) @@ -459,7 +459,7 @@ impl Value { Value::TypedExpr(TypedExpr::StmtId(..)) | Value::Expr(..) | Value::Pointer(..) - | Value::StructDefinition(_) + | Value::TypeDefinition(_) | Value::TraitConstraint(..) | Value::TraitDefinition(_) | Value::TraitImpl(_) diff --git a/compiler/noirc_frontend/src/hir_def/types.rs b/compiler/noirc_frontend/src/hir_def/types.rs index d1c70a18259..f25a40197ab 100644 --- a/compiler/noirc_frontend/src/hir_def/types.rs +++ b/compiler/noirc_frontend/src/hir_def/types.rs @@ -311,8 +311,7 @@ pub enum QuotedType { TopLevelItem, Type, TypedExpr, - StructDefinition, - EnumDefinition, + TypeDefinition, TraitConstraint, TraitDefinition, TraitImpl, @@ -1099,8 +1098,7 @@ impl std::fmt::Display for QuotedType { QuotedType::TopLevelItem => write!(f, "TopLevelItem"), QuotedType::Type => write!(f, "Type"), QuotedType::TypedExpr => write!(f, "TypedExpr"), - QuotedType::StructDefinition => write!(f, "StructDefinition"), - QuotedType::EnumDefinition => write!(f, "EnumDefinition"), + QuotedType::TypeDefinition => write!(f, "TypeDefinition"), QuotedType::TraitDefinition => write!(f, "TraitDefinition"), QuotedType::TraitConstraint => write!(f, "TraitConstraint"), QuotedType::TraitImpl => write!(f, "TraitImpl"), diff --git a/compiler/noirc_frontend/src/lexer/token.rs b/compiler/noirc_frontend/src/lexer/token.rs index effdc827f2f..75108537de3 100644 --- a/compiler/noirc_frontend/src/lexer/token.rs +++ b/compiler/noirc_frontend/src/lexer/token.rs @@ -1116,6 +1116,7 @@ pub enum Keyword { TraitDefinition, TraitImpl, Type, + TypeDefinition, TypedExpr, TypeType, Unchecked, @@ -1177,6 +1178,7 @@ impl fmt::Display for Keyword { Keyword::TraitDefinition => write!(f, "TraitDefinition"), Keyword::TraitImpl => write!(f, "TraitImpl"), Keyword::Type => write!(f, "type"), + Keyword::TypeDefinition => write!(f, "TypeDefinition"), Keyword::TypedExpr => write!(f, "TypedExpr"), Keyword::TypeType => write!(f, "Type"), Keyword::Unchecked => write!(f, "unchecked"), @@ -1241,6 +1243,7 @@ impl Keyword { "TraitImpl" => Keyword::TraitImpl, "type" => Keyword::Type, "Type" => Keyword::TypeType, + "TypeDefinition" => Keyword::TypeDefinition, "TypedExpr" => Keyword::TypedExpr, "StructDefinition" => Keyword::StructDefinition, "unchecked" => Keyword::Unchecked, diff --git a/compiler/noirc_frontend/src/parser/errors.rs b/compiler/noirc_frontend/src/parser/errors.rs index 76e2958f668..2989bdca94c 100644 --- a/compiler/noirc_frontend/src/parser/errors.rs +++ b/compiler/noirc_frontend/src/parser/errors.rs @@ -76,8 +76,6 @@ pub enum ParserErrorReason { TraitVisibilityIgnored, #[error("Visibility is ignored on a trait impl method")] TraitImplVisibilityIgnored, - #[error("comptime keyword is deprecated")] - ComptimeDeprecated, #[error("This requires the unstable feature '{0}' which is not enabled")] ExperimentalFeature(UnstableFeature), #[error( @@ -115,6 +113,8 @@ pub enum ParserErrorReason { MissingSafetyComment, #[error("Missing parameters for function definition")] MissingParametersForFunctionDefinition, + #[error("`StructDefinition` is deprecated. It has been renamed to `TypeDefinition`")] + StructDefinitionDeprecated, } /// Represents a parsing error, or a parsing error in the making. @@ -254,15 +254,6 @@ impl<'a> From<&'a ParserError> for Diagnostic { diagnostic.deprecated = true; diagnostic } - ParserErrorReason::ComptimeDeprecated => { - let mut diagnostic = Diagnostic::simple_warning( - "Use of deprecated keyword 'comptime'".into(), - "The 'comptime' keyword has been deprecated. It can be removed without affecting your program".into(), - error.location(), - ) ; - diagnostic.deprecated = true; - diagnostic - } ParserErrorReason::InvalidBitSize(bit_size) => Diagnostic::simple_error( format!("Use of invalid bit size {}", bit_size), format!( @@ -315,6 +306,9 @@ impl<'a> From<&'a ParserError> for Diagnostic { let secondary = "Consider changing it to a regular `//` comment".to_string(); Diagnostic::simple_warning(primary, secondary, error.location()) } + ParserErrorReason::StructDefinitionDeprecated => { + Diagnostic::simple_warning(format!("{reason}"), String::new(), error.location()) + } other => { Diagnostic::simple_error(format!("{other}"), String::new(), error.location()) } diff --git a/compiler/noirc_frontend/src/parser/parser/types.rs b/compiler/noirc_frontend/src/parser/parser/types.rs index 1ccb68e2677..638bbde866d 100644 --- a/compiler/noirc_frontend/src/parser/parser/types.rs +++ b/compiler/noirc_frontend/src/parser/parser/types.rs @@ -236,11 +236,14 @@ impl Parser<'_> { if self.eat_keyword(Keyword::TypedExpr) { return Some(UnresolvedTypeData::Quoted(QuotedType::TypedExpr)); } + + let location = self.current_token_location; if self.eat_keyword(Keyword::StructDefinition) { - return Some(UnresolvedTypeData::Quoted(QuotedType::StructDefinition)); + self.push_error(ParserErrorReason::StructDefinitionDeprecated, location); + return Some(UnresolvedTypeData::Quoted(QuotedType::TypeDefinition)); } - if self.eat_keyword(Keyword::EnumDefinition) { - return Some(UnresolvedTypeData::Quoted(QuotedType::EnumDefinition)); + if self.eat_keyword(Keyword::TypeDefinition) { + return Some(UnresolvedTypeData::Quoted(QuotedType::TypeDefinition)); } if self.eat_keyword(Keyword::TraitConstraint) { return Some(UnresolvedTypeData::Quoted(QuotedType::TraitConstraint)); diff --git a/compiler/noirc_frontend/src/tests/metaprogramming.rs b/compiler/noirc_frontend/src/tests/metaprogramming.rs index a19ef17d835..9a660bc998b 100644 --- a/compiler/noirc_frontend/src/tests/metaprogramming.rs +++ b/compiler/noirc_frontend/src/tests/metaprogramming.rs @@ -96,7 +96,7 @@ fn unquoted_integer_as_integer_token() { #[test] fn allows_references_to_structs_generated_by_macros() { let src = r#" - comptime fn make_new_struct(_s: StructDefinition) -> Quoted { + comptime fn make_new_struct(_s: TypeDefinition) -> Quoted { quote { struct Bar {} } } @@ -117,7 +117,7 @@ fn errors_if_macros_inject_functions_with_name_collisions() { // This can't be tested using `check_errors` right now because the two secondary // errors land on the same span. let src = r#" - comptime fn make_colliding_functions(_s: StructDefinition) -> Quoted { + comptime fn make_colliding_functions(_s: TypeDefinition) -> Quoted { quote { fn foo() {} } diff --git a/compiler/noirc_frontend/src/tests/visibility.rs b/compiler/noirc_frontend/src/tests/visibility.rs index ee53dcbc84f..4ccfebf578b 100644 --- a/compiler/noirc_frontend/src/tests/visibility.rs +++ b/compiler/noirc_frontend/src/tests/visibility.rs @@ -519,7 +519,7 @@ fn errors_if_accessing_private_struct_member_inside_function_generated_at_compti bar_inner: Foo, } - comptime fn generate_inner_accessor(_s: StructDefinition) -> Quoted { + comptime fn generate_inner_accessor(_s: TypeDefinition) -> Quoted { quote { fn bar_get_foo_inner(x: Bar) -> Field { x.bar_inner.foo_inner diff --git a/docs/docs/noir/concepts/comptime.md b/docs/docs/noir/concepts/comptime.md index 9661dc1a6ca..b3b6d784fb2 100644 --- a/docs/docs/noir/concepts/comptime.md +++ b/docs/docs/noir/concepts/comptime.md @@ -222,7 +222,7 @@ When you use an attribute, the function with the same name will be called with t #[my_struct_attribute] struct Foo {} -comptime fn my_struct_attribute(s: StructDefinition) { +comptime fn my_struct_attribute(s: TypeDefinition) { println("Called my_struct_attribute!"); } @@ -308,10 +308,10 @@ The following is an incomplete list of some `comptime` types along with some use - Methods: - `fn parameters(self) -> [(Quoted, Type)]` - Returns a slice of `(name, type)` pairs for each parameter -- `StructDefinition`: A struct definition +- `TypeDefinition`: A struct or enum definition - Methods: - `fn as_type(self) -> Type` - - Returns this `StructDefinition` as a `Type`. Any generics are kept as-is + - Returns this `TypeDefinition` as a `Type`. Any generics are kept as-is - `fn generics(self) -> [Quoted]` - Return the name of each generic on this struct - `fn fields(self) -> [(Quoted, Type)]` diff --git a/docs/docs/noir/standard_library/meta/index.md b/docs/docs/noir/standard_library/meta/index.md index 76daa594b1f..4b7fe38fb90 100644 --- a/docs/docs/noir/standard_library/meta/index.md +++ b/docs/docs/noir/standard_library/meta/index.md @@ -45,7 +45,7 @@ comptime { #include_code derive noir_stdlib/src/meta/mod.nr rust -Attribute placed on struct definitions. +Attribute placed on type definitions. Creates a trait impl for each trait passed in as an argument. To do this, the trait must have a derive handler registered @@ -79,7 +79,7 @@ this to register their own functions to enable their traits to be derived by `derive`. Because this function requires a function as an argument which -should produce a trait impl for any given struct, users may find +should produce a trait impl for any given type definition, users may find it helpful to use a function like `std::meta::make_trait_impl` to help creating these impls. @@ -90,7 +90,7 @@ trait DoNothing { fn do_nothing(self); } -comptime fn derive_do_nothing(s: StructDefinition) -> Quoted { +comptime fn derive_do_nothing(s: TypeDefinition) -> Quoted { let typ = s.as_type(); quote { impl DoNothing for $typ { @@ -103,7 +103,7 @@ comptime fn derive_do_nothing(s: StructDefinition) -> Quoted { ``` As another example, `derive_eq` in the stdlib is used to derive the `Eq` -trait for any struct. It makes use of `make_trait_impl` to do this: +trait for any type definition. It makes use of `make_trait_impl` to do this: #include_code derive_eq noir_stdlib/src/cmp.nr rust @@ -122,7 +122,7 @@ Note that this function only works for traits which: If your trait fits these criteria then `make_trait_impl` is likely the easiest way to write your derive handler. The arguments are as follows: -- `s`: The struct to make the impl for +- `s`: The type definition to make the impl for - `trait_name`: The name of the trait to derive. E.g. `quote { Eq }`. - `function_signature`: The signature of the trait method to derive. E.g. `fn eq(self, other: Self) -> bool`. - `for_each_field`: An operation to be performed on each field. E.g. `|name| quote { (self.$name == other.$name) }`. diff --git a/docs/docs/noir/standard_library/meta/struct_def.md b/docs/docs/noir/standard_library/meta/struct_def.md index 18e722af31e..a179f6523be 100644 --- a/docs/docs/noir/standard_library/meta/struct_def.md +++ b/docs/docs/noir/standard_library/meta/struct_def.md @@ -1,30 +1,30 @@ --- -title: StructDefinition +title: TypeDefinition --- -`std::meta::struct_def` contains methods on the built-in `StructDefinition` type. -This type corresponds to `struct Name { field1: Type1, ... }` items in the source program. +`std::meta::type_def` contains methods on the built-in `TypeDefinition` type. +This type corresponds to `struct Name { field1: Type1, ... }` and `enum Name { Variant1(Fields1), ... }` items in the source program. ## Methods ### add_attribute -#include_code add_attribute noir_stdlib/src/meta/struct_def.nr rust +#include_code add_attribute noir_stdlib/src/meta/type_def.nr rust -Adds an attribute to the struct. +Adds an attribute to the data type. ### add_generic -#include_code add_generic noir_stdlib/src/meta/struct_def.nr rust +#include_code add_generic noir_stdlib/src/meta/type_def.nr rust -Adds an generic to the struct. Returns the new generic type. +Adds an generic to the type. Returns the new generic type. Errors if the given generic name isn't a single identifier or if -the struct already has a generic with the same name. +the type already has a generic with the same name. This method should be used carefully, if there is existing code referring -to the struct type it may be checked before this function is called and -see the struct with the original number of generics. This method should -thus be preferred to use on code generated from other macros and structs +to the type it may be checked before this function is called and +see the type with the original number of generics. This method should +thus be preferred to use on code generated from other macros and types that are not used in function signatures. Example: @@ -33,16 +33,16 @@ Example: ### as_type -#include_code as_type noir_stdlib/src/meta/struct_def.nr rust +#include_code as_type noir_stdlib/src/meta/type_def.nr rust -Returns this struct as a type in the source program. If this struct has +Returns this type definition as a type in the source program. If this definition has any generics, the generics are also included as-is. ### generics -#include_code generics noir_stdlib/src/meta/struct_def.nr rust +#include_code generics noir_stdlib/src/meta/type_def.nr rust -Returns each generic on this struct. Each generic is represented as a tuple containing the type, +Returns each generic on this type definition. Each generic is represented as a tuple containing the type, and an optional containing the numeric type if it's a numeric generic. Example: @@ -54,7 +54,7 @@ struct Foo { baz: Baz, } -comptime fn example(foo: StructDefinition) { +comptime fn example(foo: TypeDefinition) { assert_eq(foo.generics().len(), 3); // Fails because `T` isn't in scope @@ -69,45 +69,45 @@ comptime fn example(foo: StructDefinition) { ### fields -#include_code fields noir_stdlib/src/meta/struct_def.nr rust +#include_code fields noir_stdlib/src/meta/type_def.nr rust -Returns (name, type) pairs of each field in this struct. +Returns (name, type) pairs of each field in this struct type. Any generic types used in each field type is automatically substituted with the provided generic arguments. ### fields_as_written -#include_code fields_as_written noir_stdlib/src/meta/struct_def.nr rust +#include_code fields_as_written noir_stdlib/src/meta/type_def.nr rust -Returns (name, type) pairs of each field in this struct. Each type is as-is +Returns (name, type) pairs of each field in this struct type. Each type is as-is with any generic arguments unchanged. Unless the field types are not needed, -users should generally prefer to use `StructDefinition::fields` over this +users should generally prefer to use `TypeDefinition::fields` over this function if possible. ### has_named_attribute -#include_code has_named_attribute noir_stdlib/src/meta/struct_def.nr rust +#include_code has_named_attribute noir_stdlib/src/meta/type_def.nr rust -Returns true if this struct has a custom attribute with the given name. +Returns true if this type has a custom attribute with the given name. ### module -#include_code module noir_stdlib/src/meta/struct_def.nr rust +#include_code module noir_stdlib/src/meta/type_def.nr rust -Returns the module where the struct is defined. +Returns the module where the type is defined. ### name -#include_code name noir_stdlib/src/meta/struct_def.nr rust +#include_code name noir_stdlib/src/meta/type_def.nr rust -Returns the name of this struct +Returns the name of this type -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. +Note that the returned quoted value will be just the type name, it will +not be the full path to the type definition, nor will it include any generics. ### set_fields -#include_code set_fields noir_stdlib/src/meta/struct_def.nr rust +#include_code set_fields noir_stdlib/src/meta/type_def.nr rust Sets the fields of this struct to the given fields list where each element is a pair of the field's name and the field's type. Expects each field name @@ -126,7 +126,7 @@ Example: #[mangle_fields] struct Foo { x: Field } -comptime fn mangle_fields(s: StructDefinition) { +comptime fn mangle_fields(s: TypeDefinition) { s.set_fields(&[ (quote { a }, quote { u32 }.as_type()), (quote { b }, quote { i8 }.as_type()), @@ -137,10 +137,10 @@ comptime fn mangle_fields(s: StructDefinition) { ## Trait Implementations ```rust -impl Eq for StructDefinition -impl Hash for StructDefinition +impl Eq for TypeDefinition +impl Hash for TypeDefinition ``` -Note that each struct is assigned a unique ID internally and this is what is used for -equality and hashing. So even structs with identical generics and fields may not +Note that each type definition is assigned a unique ID internally and this is what is used for +equality and hashing. So even type definitions with identical generics and fields may not be equal in this sense if they were originally different items in the source program. diff --git a/docs/docs/noir/standard_library/meta/typ.md b/docs/docs/noir/standard_library/meta/typ.md index 455853bfea3..7925b361f9e 100644 --- a/docs/docs/noir/standard_library/meta/typ.md +++ b/docs/docs/noir/standard_library/meta/typ.md @@ -81,9 +81,9 @@ If this is a slice type, return the element type of the slice. If this is a `str` type, returns the length `N` as a type. -### as_struct +### as_data_type -#include_code as_struct noir_stdlib/src/meta/typ.nr rust +#include_code as_data_type noir_stdlib/src/meta/typ.nr rust If this is a struct type, returns the struct in addition to any generic arguments on this type. diff --git a/noir_stdlib/src/cmp.nr b/noir_stdlib/src/cmp.nr index 24621bb3793..3a7d1c7298d 100644 --- a/noir_stdlib/src/cmp.nr +++ b/noir_stdlib/src/cmp.nr @@ -8,7 +8,7 @@ pub trait Eq { // docs:end:eq-trait // docs:start:derive_eq -comptime fn derive_eq(s: StructDefinition) -> Quoted { +comptime fn derive_eq(s: TypeDefinition) -> Quoted { let signature = quote { fn eq(_self: Self, _other: Self) -> bool }; let for_each_field = |name| quote { (_self.$name == _other.$name) }; let body = |fields| { @@ -219,7 +219,7 @@ pub trait Ord { // docs:end:ord-trait // docs:start:derive_ord -comptime fn derive_ord(s: StructDefinition) -> Quoted { +comptime fn derive_ord(s: TypeDefinition) -> Quoted { let signature = quote { fn cmp(_self: Self, _other: Self) -> std::cmp::Ordering }; let for_each_field = |name| quote { if result == std::cmp::Ordering::equal() { diff --git a/noir_stdlib/src/default.nr b/noir_stdlib/src/default.nr index 229e5e92003..9f28c056272 100644 --- a/noir_stdlib/src/default.nr +++ b/noir_stdlib/src/default.nr @@ -7,7 +7,7 @@ pub trait Default { } // docs:end:default-trait -comptime fn derive_default(s: StructDefinition) -> Quoted { +comptime fn derive_default(s: TypeDefinition) -> Quoted { let name = quote { Default }; let signature = quote { fn default() -> Self }; let for_each_field = |name| quote { $name: Default::default() }; diff --git a/noir_stdlib/src/hash/mod.nr b/noir_stdlib/src/hash/mod.nr index 7a492d373cc..bd1c2b89179 100644 --- a/noir_stdlib/src/hash/mod.nr +++ b/noir_stdlib/src/hash/mod.nr @@ -139,7 +139,7 @@ pub trait Hash { } // docs:start:derive_hash -comptime fn derive_hash(s: StructDefinition) -> Quoted { +comptime fn derive_hash(s: TypeDefinition) -> Quoted { let name = quote { Hash }; let signature = quote { fn hash(_self: Self, _state: &mut H) where H: std::hash::Hasher }; let for_each_field = |name| quote { _self.$name.hash(_state); }; diff --git a/noir_stdlib/src/meta/mod.nr b/noir_stdlib/src/meta/mod.nr index 35ba05ba74d..425a7e5255f 100644 --- a/noir_stdlib/src/meta/mod.nr +++ b/noir_stdlib/src/meta/mod.nr @@ -4,11 +4,11 @@ pub mod format_string; pub mod function_def; pub mod module; pub mod op; -pub mod struct_def; pub mod trait_constraint; pub mod trait_def; pub mod trait_impl; pub mod typ; +pub mod type_def; pub mod typed_expr; pub mod quoted; pub mod unresolved_type; @@ -36,20 +36,20 @@ use crate::collections::umap::UHashMap; use crate::hash::BuildHasherDefault; use crate::hash::poseidon2::Poseidon2Hasher; -// A derive function is one that given a struct definition can +// A derive function is one that given a type definition can // create us a quoted trait impl from it. -pub type DeriveFunction = fn(StructDefinition) -> Quoted; +pub type DeriveFunction = fn(TypeDefinition) -> Quoted; // We'll keep a global HANDLERS map to keep track of the derive handler for each trait comptime mut global HANDLERS: UHashMap> = UHashMap::default(); -// Given a struct and a slice of traits to derive, create trait impls for each. +// Given a type definition and a slice of traits to derive, create trait impls for each. // This function is as simple as iterating over the slice, checking if we have a trait // handler registered for the given trait, calling it, and appending the result. // docs:start:derive #[varargs] -pub comptime fn derive(s: StructDefinition, traits: [TraitDefinition]) -> Quoted { +pub comptime fn derive(s: TypeDefinition, traits: [TraitDefinition]) -> Quoted { // docs:end:derive let mut result = quote {}; @@ -79,8 +79,8 @@ pub comptime fn derive_via(t: TraitDefinition, f: DeriveFunction) { /// 1. The impl only has one function, with the signature `function_signature` /// 2. The trait itself does not have any generics. /// -/// While these assumptions are met, `make_impl` will create an impl from a StructDefinition, -/// automatically filling in the required generics from the struct, along with the where clause. +/// While these assumptions are met, `make_impl` will create an impl from a TypeDefinition, +/// automatically filling in the required generics from the type, along with the where clause. /// The function body is created by mapping each field with `for_each_field` and joining the /// results with `join_fields_with`. The result of this is passed to the `body` function for /// any final processing - e.g. wrapping each field in a `StructConstructor { .. }` expression. @@ -88,7 +88,7 @@ pub comptime fn derive_via(t: TraitDefinition, f: DeriveFunction) { /// See `derive_eq` and `derive_default` for example usage. // docs:start:make_trait_impl pub comptime fn make_trait_impl( - s: StructDefinition, + s: TypeDefinition, trait_name: Quoted, function_signature: Quoted, for_each_field: fn[Env1](Quoted) -> Quoted, @@ -161,7 +161,7 @@ mod tests { y: [Field; 2], } - comptime fn derive_field_count(s: StructDefinition) -> Quoted { + comptime fn derive_field_count(s: TypeDefinition) -> Quoted { let typ = s.as_type(); let field_count = s.fields_as_written().len(); quote { @@ -180,7 +180,7 @@ mod tests { my_field: i32, } - comptime fn assert_field_is_type(s: StructDefinition, typ: Type) { + comptime fn assert_field_is_type(s: TypeDefinition, typ: Type) { // Assert the first field in `s` has type `typ` let fields = s.fields([]); assert_eq(fields[0].1, typ); @@ -194,7 +194,7 @@ mod tests { } #[varargs] - comptime fn assert_three_args(_s: StructDefinition, args: [Field]) { + comptime fn assert_three_args(_s: TypeDefinition, args: [Field]) { assert_eq(args.len(), 3); } // docs:end:annotation-varargs-example @@ -207,7 +207,7 @@ mod tests { fn do_nothing(self); } - comptime fn derive_do_nothing(s: StructDefinition) -> Quoted { + comptime fn derive_do_nothing(s: TypeDefinition) -> Quoted { // This is simplified since we don't handle generics or where clauses! // In a real example we'd likely also need to introduce each of // `s.generics()` as well as a trait constraint for each generic @@ -230,7 +230,7 @@ mod tests { // This helper function will generate our impl for us along with any // necessary where clauses and still provides a flexible interface // for us to work on each field on the struct. - comptime fn derive_do_nothing_alt(s: StructDefinition) -> Quoted { + comptime fn derive_do_nothing_alt(s: TypeDefinition) -> Quoted { let trait_name = quote { DoNothing }; let method_signature = quote { fn do_nothing(self) }; diff --git a/noir_stdlib/src/meta/module.nr b/noir_stdlib/src/meta/module.nr index 0298282761a..586b771b352 100644 --- a/noir_stdlib/src/meta/module.nr +++ b/noir_stdlib/src/meta/module.nr @@ -21,7 +21,7 @@ impl Module { #[builtin(module_structs)] // docs:start:structs - pub comptime fn structs(self) -> [StructDefinition] {} + pub comptime fn structs(self) -> [TypeDefinition] {} // docs:end:structs #[builtin(module_name)] diff --git a/noir_stdlib/src/meta/typ.nr b/noir_stdlib/src/meta/typ.nr index 8076c692ca5..a2f48dbfa00 100644 --- a/noir_stdlib/src/meta/typ.nr +++ b/noir_stdlib/src/meta/typ.nr @@ -109,11 +109,16 @@ impl Type { pub comptime fn as_str(self) -> Option {} // docs:end:as_str - /// If this is a struct type, returns the struct in addition to any generic arguments on this type. - #[builtin(type_as_struct)] - // docs:start:as_struct - pub comptime fn as_struct(self) -> Option<(StructDefinition, [Type])> {} - // docs:end:as_struct + #[deprecated("This method has been renamed to `as_data_type`")] + pub comptime fn as_struct(self) -> Option<(TypeDefinition, [Type])> { + self.as_data_type() + } + + /// If this is a struct or enum type, returns the type in addition to any generic arguments on this type. + #[builtin(type_as_data_type)] + // docs:start:as_data_type + pub comptime fn as_data_type(self) -> Option<(TypeDefinition, [Type])> {} + // docs:end:as_data_type /// If this is a tuple type, returns each element type of the tuple. #[builtin(type_as_tuple)] diff --git a/noir_stdlib/src/meta/struct_def.nr b/noir_stdlib/src/meta/type_def.nr similarity index 61% rename from noir_stdlib/src/meta/struct_def.nr rename to noir_stdlib/src/meta/type_def.nr index d561e326fbd..0e39a2890d0 100644 --- a/noir_stdlib/src/meta/struct_def.nr +++ b/noir_stdlib/src/meta/type_def.nr @@ -1,89 +1,89 @@ use crate::option::Option; -impl StructDefinition { - #[builtin(struct_def_add_attribute)] +impl TypeDefinition { + #[builtin(type_def_add_attribute)] // docs:start:add_attribute pub comptime fn add_attribute(self, attribute: str) {} // docs:end:add_attribute - #[builtin(struct_def_add_generic)] + #[builtin(type_def_add_generic)] // docs:start:add_generic pub comptime fn add_generic(self, generic_name: str) -> Type {} // docs:end:add_generic - /// Return a syntactic version of this struct definition as a type. + /// Return a syntactic version of this type definition as a type. /// For example, `as_type(quote { type Foo { ... } })` would return `Foo` - #[builtin(struct_def_as_type)] + #[builtin(type_def_as_type)] // docs:start:as_type pub comptime fn as_type(self) -> Type {} // docs:end:as_type - #[builtin(struct_def_has_named_attribute)] + #[builtin(type_def_has_named_attribute)] // docs:start:has_named_attribute pub comptime fn has_named_attribute(self, name: str) -> bool {} // docs:end:has_named_attribute - /// Return (type, option) pairs of each generic in this struct definition. + /// Return (type, option) pairs of each generic in this type definition. /// If a generic is numeric, the second element of the pair will contain the numeric type. - #[builtin(struct_def_generics)] + #[builtin(type_def_generics)] // docs:start:generics pub comptime fn generics(self) -> [(Type, Option)] {} // docs:end:generics - /// Returns (name, type) pairs of each field in this struct. + /// Returns (name, type) pairs of each field in this type. /// Any generic types used in each field type is automatically substituted with the /// provided generic arguments. - #[builtin(struct_def_fields)] + #[builtin(type_def_fields)] // docs:start:fields pub comptime fn fields(self, generic_args: [Type]) -> [(Quoted, Type)] {} // docs:end:fields - /// Returns (name, type) pairs of each field in this struct. Each type is as-is + /// Returns (name, type) pairs of each field in this type. Each type is as-is /// with any generic arguments unchanged. Unless the field types are not needed, - /// users should generally prefer to use `StructDefinition::fields` over this + /// users should generally prefer to use `TypeDefinition::fields` over this /// function if possible. - #[builtin(struct_def_fields_as_written)] + #[builtin(type_def_fields_as_written)] // docs:start:fields_as_written pub comptime fn fields_as_written(self) -> [(Quoted, Type)] {} // docs:end:fields_as_written - #[builtin(struct_def_module)] + #[builtin(type_def_module)] // docs:start:module pub comptime fn module(self) -> Module {} // docs:end:module - #[builtin(struct_def_name)] + #[builtin(type_def_name)] // docs:start:name pub comptime 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. + /// Sets the fields of this type to the given fields list. + /// All existing fields of the type will be overridden with the given fields. /// Each element of the fields list corresponds to the name and type of a field. /// Each name is expected to be a single identifier. - #[builtin(struct_def_set_fields)] + #[builtin(type_def_set_fields)] // docs:start:set_fields pub comptime fn set_fields(self, new_fields: [(Quoted, Type)]) {} // docs:end:set_fields } -impl crate::hash::Hash for StructDefinition { +impl crate::hash::Hash for TypeDefinition { comptime fn hash(self, state: &mut H) where H: crate::hash::Hasher, { - state.write(struct_def_hash(self)) + state.write(type_def_hash(self)) } } -impl crate::cmp::Eq for StructDefinition { +impl crate::cmp::Eq for TypeDefinition { comptime fn eq(self, other: Self) -> bool { - struct_def_eq(self, other) + type_def_eq(self, other) } } -#[builtin(struct_def_eq)] -comptime fn struct_def_eq(_first: StructDefinition, _second: StructDefinition) -> bool {} +#[builtin(type_def_eq)] +comptime fn type_def_eq(_first: TypeDefinition, _second: TypeDefinition) -> bool {} -#[builtin(struct_def_hash)] -comptime fn struct_def_hash(_struct: StructDefinition) -> Field {} +#[builtin(type_def_hash)] +comptime fn type_def_hash(_type: TypeDefinition) -> Field {} 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 d4fef84442d..91f9c3a52f4 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: StructDefinition) { +comptime fn fail_assert(_typ: TypeDefinition) { assert(false); } diff --git a/test_programs/compile_success_empty/attribute_args/src/main.nr b/test_programs/compile_success_empty/attribute_args/src/main.nr index 29690ba36c7..d8ba7b167e0 100644 --- a/test_programs/compile_success_empty/attribute_args/src/main.nr +++ b/test_programs/compile_success_empty/attribute_args/src/main.nr @@ -3,7 +3,7 @@ #[attr_with_varargs(1, 2, 3, 4)] pub struct Foo {} -comptime fn attr_with_args(s: StructDefinition, a: Field, b: Field) { +comptime fn attr_with_args(s: TypeDefinition, a: Field, b: Field) { // Ensure all variables are in scope. // We can't print them since that breaks the test runner. let _ = s; @@ -12,7 +12,7 @@ comptime fn attr_with_args(s: StructDefinition, a: Field, b: Field) { } #[varargs] -comptime fn attr_with_varargs(s: StructDefinition, t: [Field]) { +comptime fn attr_with_varargs(s: TypeDefinition, t: [Field]) { let _ = s; for _ in t {} assert(t.len() < 5); diff --git a/test_programs/compile_success_empty/attribute_order/src/main.nr b/test_programs/compile_success_empty/attribute_order/src/main.nr index 9d5ba32b58e..b834526a243 100644 --- a/test_programs/compile_success_empty/attribute_order/src/main.nr +++ b/test_programs/compile_success_empty/attribute_order/src/main.nr @@ -13,7 +13,7 @@ pub comptime fn assert_run_order_function(_f: FunctionDefinition, order: Field) attributes_run += 1; } -pub comptime fn assert_run_order_struct(_s: StructDefinition, order: Field) { +pub comptime fn assert_run_order_struct(_s: TypeDefinition, order: Field) { assert_eq(order, attributes_run); attributes_run += 1; } diff --git a/test_programs/compile_success_empty/attributes_struct/src/main.nr b/test_programs/compile_success_empty/attributes_struct/src/main.nr index 713e6321f4b..3f9dca12c8c 100644 --- a/test_programs/compile_success_empty/attributes_struct/src/main.nr +++ b/test_programs/compile_success_empty/attributes_struct/src/main.nr @@ -13,7 +13,7 @@ fn main() {} #[add_attribute] pub struct Foo {} -comptime fn add_attribute(s: StructDefinition) { +comptime fn add_attribute(s: TypeDefinition) { assert(!s.has_named_attribute("foo")); s.add_attribute("foo"); assert(s.has_named_attribute("foo")); 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 41ba76171a8..0496d59b570 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 @@ -9,14 +9,14 @@ pub struct I32AndField { z: i8, } -comptime fn my_comptime_fn(typ: StructDefinition) { +comptime fn my_comptime_fn(typ: TypeDefinition) { let _ = typ.as_type(); assert_eq(typ.generics().len(), 3); assert_eq(typ.fields_as_written().len(), 2); assert_eq(typ.name(), quote { MyType }); } -comptime fn mutate_struct_fields(s: StructDefinition) { +comptime fn mutate_struct_fields(s: TypeDefinition) { let fields = &[(quote {x}, quote {i32}.as_type()), (quote {y}, quote {Field}.as_type())]; s.set_fields(fields); } @@ -25,7 +25,7 @@ mod foo { #[attr] pub struct Foo {} - comptime fn attr(s: StructDefinition) { + comptime fn attr(s: TypeDefinition) { assert_eq(s.module().name(), quote { foo }); } @@ -33,7 +33,7 @@ mod foo { pub struct Bar {} // docs:start:add-generic-example - comptime fn add_generic(s: StructDefinition) { + comptime fn add_generic(s: TypeDefinition) { assert_eq(s.generics().len(), 0); let new_generic = s.add_generic("T"); @@ -49,7 +49,7 @@ mod foo { fn main() { comptime { let typ = quote { MyType }.as_type(); - let (struct_def, generics) = typ.as_struct().unwrap(); + let (struct_def, generics) = typ.as_data_type().unwrap(); let fields = struct_def.fields(generics); assert_eq(fields.len(), 2); diff --git a/test_programs/compile_success_empty/comptime_type/src/main.nr b/test_programs/compile_success_empty/comptime_type/src/main.nr index 6a2453ff0f2..9e1a50aca45 100644 --- a/test_programs/compile_success_empty/comptime_type/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type/src/main.nr @@ -94,12 +94,12 @@ fn main() { let bool_type = type_of(yes); assert(bool_type.is_bool()); - // Check Type::as_struct - assert(u8_type.as_struct().is_none()); + // Check Type::as_data_type + assert(u8_type.as_data_type().is_none()); let foo = Foo { x: 0 }; let foo_type = type_of(foo); - let (struct_definition, generics) = foo_type.as_struct().unwrap(); + let (struct_definition, generics) = foo_type.as_data_type().unwrap(); let fields = struct_definition.fields(generics); assert_eq(fields.len(), 1); 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 fe7a7140280..97a5eb3cd92 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: StructDefinition) -> Quoted { +comptime fn derive_default(typ: TypeDefinition) -> Quoted { let generics = typ.generics(); assert_eq( generics.len(), diff --git a/test_programs/compile_success_empty/regression_5671/src/main.nr b/test_programs/compile_success_empty/regression_5671/src/main.nr index 2bac98ef7c4..23a532b6607 100644 --- a/test_programs/compile_success_empty/regression_5671/src/main.nr +++ b/test_programs/compile_success_empty/regression_5671/src/main.nr @@ -4,7 +4,7 @@ struct MyOtherStruct { field2: B, } -comptime fn foo(_s: StructDefinition) -> Quoted { +comptime fn foo(_s: TypeDefinition) -> Quoted { quote { impl Eq for MyOtherStruct where A: Eq, B: Eq { fn eq(self, other: Self) -> bool { diff --git a/test_programs/compile_success_empty/unquote_multiple_items_from_annotation/src/main.nr b/test_programs/compile_success_empty/unquote_multiple_items_from_annotation/src/main.nr index 591c03de905..8cb44dc9f4e 100644 --- a/test_programs/compile_success_empty/unquote_multiple_items_from_annotation/src/main.nr +++ b/test_programs/compile_success_empty/unquote_multiple_items_from_annotation/src/main.nr @@ -6,7 +6,7 @@ fn main() { assert_eq(TWO, 2); } -comptime fn foo(_: StructDefinition) -> Quoted { +comptime fn foo(_: TypeDefinition) -> Quoted { quote { global ONE: Field = 1; global TWO: u32 = 2; diff --git a/test_programs/compile_success_empty/use_callers_scope/src/main.nr b/test_programs/compile_success_empty/use_callers_scope/src/main.nr index 26429997f0e..5b2f8e4b581 100644 --- a/test_programs/compile_success_empty/use_callers_scope/src/main.nr +++ b/test_programs/compile_success_empty/use_callers_scope/src/main.nr @@ -8,7 +8,7 @@ fn main() {} mod bar { #[use_callers_scope] - pub comptime fn struct_attr(_: StructDefinition) { + pub comptime fn struct_attr(_: TypeDefinition) { let _ = quote { Bar }.as_type(); } diff --git a/test_programs/execution_success/derive/src/main.nr b/test_programs/execution_success/derive/src/main.nr index f7d4f6b607a..5964b9d69a5 100644 --- a/test_programs/execution_success/derive/src/main.nr +++ b/test_programs/execution_success/derive/src/main.nr @@ -10,7 +10,7 @@ struct MyStruct { my_field: u32, } -comptime fn derive_do_nothing(s: StructDefinition) -> Quoted { +comptime fn derive_do_nothing(s: TypeDefinition) -> Quoted { let typ = s.as_type(); let generics = s.generics().map(|g| quote { $g }).join(quote {,}); quote { diff --git a/tooling/lsp/src/requests/completion/builtins.rs b/tooling/lsp/src/requests/completion/builtins.rs index ce5b5f35f46..430e97d3ad1 100644 --- a/tooling/lsp/src/requests/completion/builtins.rs +++ b/tooling/lsp/src/requests/completion/builtins.rs @@ -166,10 +166,11 @@ pub(super) fn keyword_builtin_type(keyword: &Keyword) -> Option<&'static str> { Keyword::FunctionDefinition => Some("FunctionDefinition"), Keyword::Module => Some("Module"), Keyword::Quoted => Some("Quoted"), - Keyword::StructDefinition => Some("StructDefinition"), + Keyword::StructDefinition => Some("TypeDefinition"), Keyword::TraitConstraint => Some("TraitConstraint"), Keyword::TraitDefinition => Some("TraitDefinition"), Keyword::TraitImpl => Some("TraitImpl"), + Keyword::TypeDefinition => Some("TypeDefinition"), Keyword::TypedExpr => Some("TypedExpr"), Keyword::TypeType => Some("Type"), Keyword::UnresolvedType => Some("UnresolvedType"), @@ -283,6 +284,7 @@ pub(super) fn keyword_builtin_function(keyword: &Keyword) -> Option { if let RequestedItems::OnlyAttributeFunctions(target) = requested_items { match target { AttributeTarget::Module => Some(Type::Quoted(QuotedType::Module)), - AttributeTarget::Struct => Some(Type::Quoted(QuotedType::StructDefinition)), - AttributeTarget::Enum => Some(Type::Quoted(QuotedType::EnumDefinition)), + AttributeTarget::Struct => Some(Type::Quoted(QuotedType::TypeDefinition)), + AttributeTarget::Enum => Some(Type::Quoted(QuotedType::TypeDefinition)), AttributeTarget::Trait => Some(Type::Quoted(QuotedType::TraitDefinition)), AttributeTarget::Function => Some(Type::Quoted(QuotedType::FunctionDefinition)), AttributeTarget::Let => { diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index 45a0b7e15e4..fb67a9c3ca7 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -326,7 +326,6 @@ mod tests { use nargo_toml::PackageSelection; use noirc_driver::{CompileOptions, CrateName}; use noirc_frontend::elaborator::UnstableFeature; - use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use crate::cli::test_cmd::formatters::diagnostic_to_string; use crate::cli::{ @@ -399,7 +398,8 @@ mod tests { assert!(!test_workspaces.is_empty(), "should find some test workspaces"); - test_workspaces.par_iter().for_each(|workspace| { + // This could be `.par_iter()` but then error messages are no longer reported + test_workspaces.iter().for_each(|workspace| { let (file_manager, parsed_files) = parse_workspace(workspace); let binary_packages = workspace.into_iter().filter(|package| package.is_binary());