Skip to content

Commit f411839

Browse files
committed
Compile panic expression to ABI error codes
1 parent de1de34 commit f411839

File tree

30 files changed

+633
-156
lines changed

30 files changed

+633
-156
lines changed

Cargo.lock

Lines changed: 10 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ sway-ir-macros = { path = "sway-ir/sway-ir-macros", version = "0.68.1" }
8282
#
8383

8484
# Dependencies from the `fuel-abi-types` repository:
85-
fuel-abi-types = "0.8"
85+
fuel-abi-types = "0.10"
8686

8787
# Dependencies from the `fuel-core` repository:
8888
#
89-
# Although ALL verions are "X.Y", we need the complete semver for
89+
# Although ALL versions are "X.Y", we need the complete semver for
9090
# fuel-core-client as the GitHub Actions workflow parses this value to pull down
91-
# the correct tarball
91+
# the correct tarball.
9292
fuel-core-client = { version = "0.43.1", default-features = false }
9393
fuel-core-types = { version = "0.43", default-features = false }
9494

@@ -256,3 +256,9 @@ vte = "0.13"
256256
walkdir = "2.3"
257257
whoami = "1.5"
258258
wiremock = "0.6"
259+
260+
[patch.crates-io]
261+
fuels = { git = "https://github.com/FuelLabs/fuels-rs", branch = "feat/abi-errors" }
262+
fuels-core = { git = "https://github.com/FuelLabs/fuels-rs", branch = "feat/abi-errors" }
263+
fuels-accounts = { git = "https://github.com/FuelLabs/fuels-rs", branch = "feat/abi-errors" }
264+
fuels-code-gen = { git = "https://github.com/FuelLabs/fuels-rs", branch = "feat/abi-errors" }

forc-pkg/src/pkg.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,9 +1768,15 @@ pub fn compile(
17681768
metrics
17691769
);
17701770

1771+
let mut asm = match asm_res {
1772+
Err(_) => return fail(handler),
1773+
Ok(asm) => asm,
1774+
};
1775+
17711776
const ENCODING_V0: &str = "0";
17721777
const ENCODING_V1: &str = "1";
17731778
const SPEC_VERSION: &str = "1";
1779+
const SPEC_VERSION_ERROR_TYPE: &str = "1.1";
17741780

17751781
let mut program_abi = match pkg.target {
17761782
BuildTarget::Fuel => {
@@ -1782,6 +1788,7 @@ pub fn compile(
17821788
&handler,
17831789
&mut AbiContext {
17841790
program: typed_program,
1791+
panic_locations: &asm.panic_locations,
17851792
abi_with_callpaths: true,
17861793
type_ids_to_full_type_str: HashMap::<String, String>::new(),
17871794
},
@@ -1791,7 +1798,11 @@ pub fn compile(
17911798
} else {
17921799
ENCODING_V0.into()
17931800
},
1794-
SPEC_VERSION.into(),
1801+
if experimental.error_type {
1802+
SPEC_VERSION_ERROR_TYPE.into()
1803+
} else {
1804+
SPEC_VERSION.into()
1805+
}
17951806
),
17961807
Some(sway_build_config.clone()),
17971808
metrics
@@ -1805,11 +1816,8 @@ pub fn compile(
18051816
BuildTarget::EVM => {
18061817
// Merge the ABI output of ASM gen with ABI gen to handle internal constructors
18071818
// generated by the ASM backend.
1808-
let mut ops = match &asm_res {
1809-
Ok(ref asm) => match &asm.0.abi {
1810-
Some(ProgramABI::Evm(ops)) => ops.clone(),
1811-
_ => vec![],
1812-
},
1819+
let mut ops = match &asm.finalized_asm.abi {
1820+
Some(ProgramABI::Evm(ops)) => ops.clone(),
18131821
_ => vec![],
18141822
};
18151823

@@ -1828,20 +1836,13 @@ pub fn compile(
18281836
}
18291837
};
18301838

1831-
let entries = asm_res
1832-
.as_ref()
1833-
.map(|asm| asm.0.entries.clone())
1834-
.unwrap_or_default();
1835-
let entries = entries
1839+
let entries = asm
1840+
.finalized_asm
1841+
.entries
18361842
.iter()
18371843
.map(|finalized_entry| PkgEntry::from_finalized_entry(finalized_entry, engines))
18381844
.collect::<anyhow::Result<_>>()?;
18391845

1840-
let mut asm = match asm_res {
1841-
Err(_) => return fail(handler),
1842-
Ok(asm) => asm,
1843-
};
1844-
18451846
let bc_res = time_expr!(
18461847
pkg.name,
18471848
"compile asm to bytecode",
@@ -1976,12 +1977,16 @@ fn report_assembly_information(
19761977
data_section: sway_core::asm_generation::DataSectionInformation {
19771978
size: data_section_size,
19781979
used: compiled_asm
1979-
.0
1980+
.finalized_asm
19801981
.data_section
19811982
.iter_all_entries()
19821983
.map(|entry| calculate_entry_size(&entry))
19831984
.sum(),
1984-
value_pairs: compiled_asm.0.data_section.iter_all_entries().collect(),
1985+
value_pairs: compiled_asm
1986+
.finalized_asm
1987+
.data_section
1988+
.iter_all_entries()
1989+
.collect(),
19851990
},
19861991
};
19871992

sway-core/src/abi_generation/fuel_abi.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
use fuel_abi_types::abi::program::{
2-
self as program_abi, ConcreteTypeId, MetadataTypeId, TypeConcreteDeclaration,
2+
self as program_abi, ConcreteTypeId, ErrorDetails, ErrorPosition, MetadataTypeId,
3+
TypeConcreteDeclaration,
34
};
45
use sha2::{Digest, Sha256};
5-
use std::collections::{HashMap, HashSet};
6+
use std::collections::{BTreeMap, HashMap, HashSet};
67
use sway_error::handler::{ErrorEmitted, Handler};
78
use sway_types::Span;
89

910
use crate::{
1011
ast_elements::type_parameter::GenericTypeParameter,
1112
language::ty::{TyFunctionDecl, TyProgram, TyProgramKind},
1213
transform::Attributes,
13-
Engines, TypeId, TypeInfo,
14+
Engines, PanicLocation, TypeId, TypeInfo,
1415
};
1516

1617
use super::abi_str::AbiStrContext;
1718

1819
pub struct AbiContext<'a> {
1920
pub program: &'a TyProgram,
21+
pub panic_locations: &'a Vec<PanicLocation>,
2022
pub abi_with_callpaths: bool,
2123
pub type_ids_to_full_type_str: HashMap<String, String>,
2224
}
@@ -106,6 +108,7 @@ pub fn generate_program_abi(
106108
generate_messages_types(handler, ctx, engines, metadata_types, concrete_types)?;
107109
let configurables =
108110
generate_configurables(handler, ctx, engines, metadata_types, concrete_types)?;
111+
let error_codes = generate_error_codes(ctx.panic_locations)?;
109112
program_abi::ProgramABI {
110113
program_type: "contract".to_string(),
111114
spec_version,
@@ -116,6 +119,7 @@ pub fn generate_program_abi(
116119
logged_types: Some(logged_types),
117120
messages_types: Some(messages_types),
118121
configurables: Some(configurables),
122+
error_codes: Some(error_codes),
119123
}
120124
}
121125
TyProgramKind::Script { main_function, .. } => {
@@ -133,6 +137,7 @@ pub fn generate_program_abi(
133137
generate_messages_types(handler, ctx, engines, metadata_types, concrete_types)?;
134138
let configurables =
135139
generate_configurables(handler, ctx, engines, metadata_types, concrete_types)?;
140+
let error_codes = generate_error_codes(ctx.panic_locations)?;
136141
program_abi::ProgramABI {
137142
program_type: "script".to_string(),
138143
spec_version,
@@ -143,6 +148,7 @@ pub fn generate_program_abi(
143148
logged_types: Some(logged_types),
144149
messages_types: Some(messages_types),
145150
configurables: Some(configurables),
151+
error_codes: Some(error_codes),
146152
}
147153
}
148154
TyProgramKind::Predicate { main_function, .. } => {
@@ -160,6 +166,7 @@ pub fn generate_program_abi(
160166
generate_messages_types(handler, ctx, engines, metadata_types, concrete_types)?;
161167
let configurables =
162168
generate_configurables(handler, ctx, engines, metadata_types, concrete_types)?;
169+
let error_codes = generate_error_codes(ctx.panic_locations)?;
163170
program_abi::ProgramABI {
164171
program_type: "predicate".to_string(),
165172
spec_version,
@@ -170,14 +177,15 @@ pub fn generate_program_abi(
170177
logged_types: Some(logged_types),
171178
messages_types: Some(messages_types),
172179
configurables: Some(configurables),
180+
error_codes: Some(error_codes),
173181
}
174182
}
175183
TyProgramKind::Library { .. } => {
176184
let logged_types =
177185
generate_logged_types(handler, ctx, engines, metadata_types, concrete_types)?;
178186
let messages_types =
179187
generate_messages_types(handler, ctx, engines, metadata_types, concrete_types)?;
180-
188+
let error_codes = generate_error_codes(ctx.panic_locations)?;
181189
program_abi::ProgramABI {
182190
program_type: "library".to_string(),
183191
spec_version,
@@ -188,6 +196,7 @@ pub fn generate_program_abi(
188196
logged_types: Some(logged_types),
189197
messages_types: Some(messages_types),
190198
configurables: None,
199+
error_codes: Some(error_codes),
191200
}
192201
}
193202
};
@@ -564,11 +573,36 @@ fn generate_configurables(
564573
decl.type_ascription.type_id(),
565574
)?,
566575
offset: 0,
576+
indirect: false,
567577
})
568578
})
569579
.collect::<Result<Vec<_>, _>>()
570580
}
571581

582+
fn generate_error_codes(
583+
panic_locations: &[PanicLocation],
584+
) -> Result<BTreeMap<u64, ErrorDetails>, ErrorEmitted> {
585+
let mut res = BTreeMap::new();
586+
for panic_location in panic_locations.iter() {
587+
res.insert(
588+
panic_location.revert_code,
589+
ErrorDetails {
590+
pos: ErrorPosition {
591+
pkg: panic_location.loc.pkg.clone(),
592+
file: panic_location.loc.file.clone(),
593+
line: panic_location.loc.loc.line as u64,
594+
column: panic_location.loc.loc.col as u64,
595+
},
596+
log_id: panic_location
597+
.log_id
598+
.map(|log_id| log_id.hash_id.to_string()),
599+
msg: panic_location.msg.clone(),
600+
},
601+
);
602+
}
603+
Ok(res)
604+
}
605+
572606
impl TypeId {
573607
/// Return the type parameters of a given (potentially generic) type while considering what it
574608
/// actually resolves to. These parameters are essentially of type of `usize` which are

0 commit comments

Comments
 (0)