Skip to content

Commit 211f05c

Browse files
committed
Introduce experimental ABI type aliases option.
1 parent a37517b commit 211f05c

File tree

12 files changed

+38
-5
lines changed

12 files changed

+38
-5
lines changed

docs/book/src/reference/experimental_features.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,9 @@ fn conditionally_compiled() {
8282
log("This is compiled only if both `some_feature` and `some_other_feature` are enabled.");
8383
}
8484
```
85+
86+
## Tracking Experimental Features
87+
88+
- `abi_type_aliases` — keeps the JSON ABI emitter from expanding type aliases into their target type when serializing a contract's ABI, allowing the published JSON to preserve the original alias names.
89+
90+
See the tracking issue for this feature at https://github.com/FuelLabs/sway/issues/7486.

forc-pkg/src/pkg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ pub fn compile(
18141814
metadata_declaration_cache: HashMap::new(),
18151815
concrete_declaration_cache: HashMap::new(),
18161816
type_cache_enabled: false,
1817+
experimental,
18171818
},
18181819
engines,
18191820
if experimental.new_encoding {

sway-core/src/abi_generation/abi_str.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct AbiStrContext {
1111
pub abi_with_callpaths: bool,
1212
pub abi_with_fully_specified_types: bool,
1313
pub abi_root_type_without_generic_type_parameters: bool,
14+
pub abi_type_aliases: bool,
1415
}
1516

1617
impl TypeId {
@@ -26,7 +27,7 @@ impl TypeId {
2627
let self_abi_str = type_engine
2728
.get(*self)
2829
.abi_str(handler, ctx, engines, true)?;
29-
if self.is_generic_parameter(engines, resolved_type_id) {
30+
if self.is_generic_parameter(engines, resolved_type_id, ctx.abi_type_aliases) {
3031
Ok(format!("generic {self_abi_str}"))
3132
} else {
3233
match (

sway-core/src/abi_generation/evm_abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn generate_abi_program(program: &TyProgram, engines: &Engines) -> EvmAbiRes
2525
/// Gives back a string that represents the type, considering what it resolves to
2626
fn get_type_str(type_id: &TypeId, engines: &Engines, resolved_type_id: TypeId) -> String {
2727
let type_engine = engines.te();
28-
if type_id.is_generic_parameter(engines, resolved_type_id) {
28+
if type_id.is_generic_parameter(engines, resolved_type_id, false) {
2929
format!("generic {}", abi_str(&type_engine.get(*type_id), engines))
3030
} else {
3131
match (

sway-core/src/abi_generation/fuel_abi.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::{
2323
};
2424

2525
use super::abi_str::AbiStrContext;
26+
use sway_features::ExperimentalFeatures;
2627

2728
#[derive(Clone, Debug)]
2829
pub enum AbiNameDiagnosticSpan {
@@ -48,6 +49,7 @@ pub struct AbiContext<'a> {
4849
pub metadata_declaration_cache: HashMap<TypeCacheKey, program_abi::TypeMetadataDeclaration>,
4950
pub concrete_declaration_cache: HashMap<TypeCacheKey, program_abi::TypeConcreteDeclaration>,
5051
pub type_cache_enabled: bool,
52+
pub experimental: ExperimentalFeatures,
5153
}
5254

5355
impl AbiContext<'_> {
@@ -57,6 +59,7 @@ impl AbiContext<'_> {
5759
abi_with_callpaths: self.abi_with_callpaths,
5860
abi_with_fully_specified_types: false,
5961
abi_root_type_without_generic_type_parameters: true,
62+
abi_type_aliases: self.experimental.abi_type_aliases,
6063
}
6164
}
6265
}
@@ -163,6 +166,7 @@ impl TypeId {
163166
abi_with_callpaths: true,
164167
abi_with_fully_specified_types: true,
165168
abi_root_type_without_generic_type_parameters: false,
169+
abi_type_aliases: ctx.experimental.abi_type_aliases,
166170
};
167171
let type_str = self.get_abi_type_str(handler, &display_ctx, engines, resolved_type_id)?;
168172

@@ -908,7 +912,11 @@ impl TypeId {
908912
resolved_type_id: TypeId,
909913
metadata_types_to_add: &mut Vec<program_abi::TypeMetadataDeclaration>,
910914
) -> Result<Option<Vec<MetadataTypeId>>, ErrorEmitted> {
911-
match self.is_generic_parameter(engines, resolved_type_id) {
915+
match self.is_generic_parameter(
916+
engines,
917+
resolved_type_id,
918+
ctx.experimental.abi_type_aliases,
919+
) {
912920
true => Ok(None),
913921
false => resolved_type_id
914922
.get_type_parameters(engines)
@@ -1247,7 +1255,11 @@ impl TypeId {
12471255
}
12481256
}
12491257
TypeInfo::Custom { type_arguments, .. } => {
1250-
if !self.is_generic_parameter(engines, resolved_type_id) {
1258+
if !self.is_generic_parameter(
1259+
engines,
1260+
resolved_type_id,
1261+
ctx.experimental.abi_type_aliases,
1262+
) {
12511263
for (v, p) in type_arguments.clone().unwrap_or_default().iter().zip(
12521264
resolved_type_id
12531265
.get_type_parameters(engines)

sway-core/src/language/ty/expression/expression.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ impl CollectTypesMetadata for TyExpression {
384384
ctx.engines,
385385
logged_type_id,
386386
ctx.program_name.clone(),
387+
ctx.experimental.abi_type_aliases,
387388
)?);
388389

389390
// We still need to dive into the expression because it can have additional types to collect.

sway-core/src/language/ty/expression/intrinsic_function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl CollectTypesMetadata for TyIntrinsicFunctionKind {
9696
ctx.engines,
9797
logged_type_id,
9898
ctx.program_name.clone(),
99+
ctx.experimental.abi_type_aliases,
99100
)?;
100101
types_metadata.push(logged_type);
101102
}

sway-core/src/type_system/id.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,12 @@ impl TypeId {
266266
/// Indicates of a given type is generic or not. Rely on whether the type is `Custom` and
267267
/// consider the special case where the resolved type is a struct or enum with a name that
268268
/// matches the name of the `Custom`.
269-
pub(crate) fn is_generic_parameter(self, engines: &Engines, resolved_type_id: TypeId) -> bool {
269+
pub(crate) fn is_generic_parameter(
270+
self,
271+
engines: &Engines,
272+
resolved_type_id: TypeId,
273+
abi_type_aliases_enabled: bool,
274+
) -> bool {
270275
let type_engine = engines.te();
271276
let decl_engine = engines.de();
272277
match (&*type_engine.get(self), &*type_engine.get(resolved_type_id)) {

sway-core/src/types/collect_types_metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl TypeMetadata {
128128
engines: &Engines,
129129
type_id: TypeId,
130130
program_name: String,
131+
abi_type_aliases_enabled: bool,
131132
) -> Result<Self, ErrorEmitted> {
132133
Ok(TypeMetadata::LoggedType(
133134
LogId::new(type_id.get_abi_type_str(
@@ -137,6 +138,7 @@ impl TypeMetadata {
137138
abi_with_callpaths: true,
138139
abi_with_fully_specified_types: true,
139140
abi_root_type_without_generic_type_parameters: false,
141+
abi_type_aliases: abi_type_aliases_enabled,
140142
},
141143
engines,
142144
type_id,

sway-features/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ features! {
173173
"https://github.com/FuelLabs/sway/issues/6860",
174174
new_hashing = true,
175175
"https://github.com/FuelLabs/sway/issues/7256",
176+
abi_type_aliases = false,
177+
"https://github.com/FuelLabs/sway/issues/7486",
176178
}
177179

178180
#[derive(Clone, Debug, Default, Parser)]

0 commit comments

Comments
 (0)