diff --git a/sway-core/src/abi_generation/abi_str.rs b/sway-core/src/abi_generation/abi_str.rs index 577a8bdc5d6..3c94fc3a731 100644 --- a/sway-core/src/abi_generation/abi_str.rs +++ b/sway-core/src/abi_generation/abi_str.rs @@ -5,6 +5,7 @@ use crate::{language::CallPath, Engines, TypeArgument, TypeId, TypeInfo}; pub struct AbiStrContext { pub program_name: Option, pub abi_with_callpaths: bool, + pub abi_with_fully_specified_types: bool, } impl TypeId { @@ -36,13 +37,24 @@ impl TypeId { assert_eq!(fields.len(), resolved_fields.len()); let field_strs = fields .iter() - .map(|_| "_".to_string()) + .map(|f| { + if ctx.abi_with_fully_specified_types { + type_engine.get(f.type_id).abi_str(ctx, engines) + } else { + "_".to_string() + } + }) .collect::>(); format!("({})", field_strs.join(", ")) } - (TypeInfo::Array(_, count), TypeInfo::Array(_, resolved_count)) => { + (TypeInfo::Array(type_arg, count), TypeInfo::Array(_, resolved_count)) => { assert_eq!(count.val(), resolved_count.val()); - format!("[_; {}]", count.val()) + let inner_type = if ctx.abi_with_fully_specified_types { + type_engine.get(type_arg.type_id).abi_str(ctx, engines) + } else { + "_".to_string() + }; + format!("[{}; {}]", inner_type, count.val()) } (TypeInfo::Custom { .. }, _) => { format!("generic {}", type_engine.get(*self).abi_str(ctx, engines)) @@ -57,6 +69,7 @@ impl TypeInfo { pub fn abi_str(&self, ctx: &AbiStrContext, engines: &Engines) -> String { use TypeInfo::*; let decl_engine = engines.de(); + let type_engine = engines.te(); match self { Unknown => "unknown".into(), Never => "never".into(), @@ -91,11 +104,45 @@ impl TypeInfo { ErrorRecovery(_) => "unknown due to error".into(), Enum(decl_ref) => { let decl = decl_engine.get_enum(decl_ref); - format!("enum {}", call_path_display(ctx, &decl.call_path)) + let type_params = + if !ctx.abi_with_fully_specified_types || decl.type_parameters.is_empty() { + "".into() + } else { + format!( + "<{}>", + decl.type_parameters + .iter() + .map(|p| type_engine.get(p.type_id).abi_str(ctx, engines)) + .collect::>() + .join(",") + ) + }; + format!( + "enum {}{}", + call_path_display(ctx, &decl.call_path), + type_params + ) } Struct(decl_ref) => { let decl = decl_engine.get_struct(decl_ref); - format!("struct {}", call_path_display(ctx, &decl.call_path)) + let type_params = + if !ctx.abi_with_fully_specified_types || decl.type_parameters.is_empty() { + "".into() + } else { + format!( + "<{}>", + decl.type_parameters + .iter() + .map(|p| type_engine.get(p.type_id).abi_str(ctx, engines)) + .collect::>() + .join(",") + ) + }; + format!( + "struct {}{}", + call_path_display(ctx, &decl.call_path), + type_params + ) } ContractCaller { abi_name, .. } => { format!("contract caller {abi_name}") diff --git a/sway-core/src/abi_generation/fuel_abi.rs b/sway-core/src/abi_generation/fuel_abi.rs index 81d4b3d8ef3..03bb5cddc4e 100644 --- a/sway-core/src/abi_generation/fuel_abi.rs +++ b/sway-core/src/abi_generation/fuel_abi.rs @@ -16,7 +16,11 @@ pub struct AbiContext<'a> { } impl<'a> AbiContext<'a> { - fn to_str_context(&self, engines: &Engines) -> AbiStrContext { + fn to_str_context( + &self, + engines: &Engines, + abi_with_fully_specified_types: bool, + ) -> AbiStrContext { AbiStrContext { program_name: self .program @@ -25,6 +29,7 @@ impl<'a> AbiContext<'a> { .module_id(engines) .read(engines, |m| m.name.clone().map(|v| v.as_str().to_string())), abi_with_callpaths: self.abi_with_callpaths, + abi_with_fully_specified_types, } } } @@ -96,7 +101,11 @@ fn generate_logged_types( .iter() .map(|(_, type_id)| program_abi::TypeDeclaration { type_id: type_id.index(), - type_field: type_id.get_abi_type_str(&ctx.to_str_context(engines), engines, *type_id), + type_field: type_id.get_abi_type_str( + &ctx.to_str_context(engines, false), + engines, + *type_id, + ), components: type_id.get_abi_type_components(ctx, engines, types, *type_id), type_parameters: type_id.get_abi_type_parameters(ctx, engines, types, *type_id), }) @@ -146,7 +155,11 @@ fn generate_messages_types( .iter() .map(|(_, type_id)| program_abi::TypeDeclaration { type_id: type_id.index(), - type_field: type_id.get_abi_type_str(&ctx.to_str_context(engines), engines, *type_id), + type_field: type_id.get_abi_type_str( + &ctx.to_str_context(engines, false), + engines, + *type_id, + ), components: type_id.get_abi_type_components(ctx, engines, types, *type_id), type_parameters: type_id.get_abi_type_parameters(ctx, engines, types, *type_id), }) @@ -186,7 +199,7 @@ fn generate_configurables( }| program_abi::TypeDeclaration { type_id: type_ascription.type_id.index(), type_field: type_ascription.type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, type_ascription.type_id, ), @@ -281,7 +294,7 @@ impl TypeId { .map(|x| program_abi::TypeDeclaration { type_id: x.type_argument.initial_type_id.index(), type_field: x.type_argument.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, x.type_argument.type_id, ), @@ -329,7 +342,7 @@ impl TypeId { .map(|x| program_abi::TypeDeclaration { type_id: x.type_argument.initial_type_id.index(), type_field: x.type_argument.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, x.type_argument.type_id, ), @@ -373,7 +386,7 @@ impl TypeId { let elem_abi_ty = program_abi::TypeDeclaration { type_id: elem_ty.initial_type_id.index(), type_field: elem_ty.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, elem_ty.type_id, ), @@ -416,7 +429,7 @@ impl TypeId { .map(|x| program_abi::TypeDeclaration { type_id: x.initial_type_id.index(), type_field: x.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, x.type_id, ), @@ -465,7 +478,7 @@ impl TypeId { .map(|(v, p)| program_abi::TypeDeclaration { type_id: v.initial_type_id.index(), type_field: v.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, p.type_id, ), @@ -530,7 +543,7 @@ impl TypeId { .map(|(v, p)| program_abi::TypeDeclaration { type_id: v.initial_type_id.index(), type_field: v.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, p.type_id, ), @@ -567,7 +580,7 @@ impl TypeId { .map(|v| program_abi::TypeDeclaration { type_id: v.type_id.index(), type_field: v.type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, v.type_id, ), @@ -607,7 +620,7 @@ impl TypeId { .map(|v| program_abi::TypeDeclaration { type_id: v.type_id.index(), type_field: v.type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, v.type_id, ), @@ -656,7 +669,7 @@ impl TyFunctionDecl { .map(|x| program_abi::TypeDeclaration { type_id: x.type_argument.initial_type_id.index(), type_field: x.type_argument.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, x.type_argument.type_id, ), @@ -679,7 +692,7 @@ impl TyFunctionDecl { let output_type = program_abi::TypeDeclaration { type_id: self.return_type.initial_type_id.index(), type_field: self.return_type.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, self.return_type.type_id, ), @@ -763,7 +776,7 @@ impl TypeParameter { let type_parameter = program_abi::TypeDeclaration { type_id: self.initial_type_id.index(), type_field: self.initial_type_id.get_abi_type_str( - &ctx.to_str_context(engines), + &ctx.to_str_context(engines, false), engines, self.type_id, ), diff --git a/sway-core/src/language/ty/expression/intrinsic_function.rs b/sway-core/src/language/ty/expression/intrinsic_function.rs index a9f69c7a101..e068b53be00 100644 --- a/sway-core/src/language/ty/expression/intrinsic_function.rs +++ b/sway-core/src/language/ty/expression/intrinsic_function.rs @@ -122,6 +122,7 @@ impl CollectTypesMetadata for TyIntrinsicFunctionKind { &AbiStrContext { program_name: Some(ctx.program_name.clone()), abi_with_callpaths: true, + abi_with_fully_specified_types: true, }, ctx.engines, logged_type,