Skip to content

Commit 89e5708

Browse files
authored
Fix and improve errors when the entry fns cannot be generated (#5824)
## Description This PR improves fix a problem when a type cannot be auto-impled and improve error messages for this case. This can be seen on `tests/types/contracts/type_inside_enum` in the Rust SDK repo. ``` Running `target/debug/forc build --path /home/xunilrj/github/fuels-rs/packages/fuels --experimental-new-encoding` thread 'main' panicked at sway-ir/src/constant.rs:172:14: Enums are aggregates. note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` Given that this PR also improve implementation of arrays for `AbiEncode` and `AbiDecode`, the entry function is actually generated. But in case of error, it will fail like: ``` error --> /home/xunilrj/.forc/git/checkouts/std-9be0d6062747ea7/5645f10143243e1fd64a55002cd5c09730636ece/sway-lib-core/src/codec.sw:2192:8 | 2190 | 2191 | pub trait AbiDecode { 2192 | fn abi_decode(ref mut buffer: BufferReader) -> Self; | ^^^^^^^^^^ No method named "abi_decode" found for type "SomeEnum". 2193 | } | ____ error --> <autogenerated>:5:61 | 3 | 4 | let args: (SomeEnum,) = decode_second_param::<(SomeEnum,)>(); 5 | let result_arr_inside_enum: raw_slice = encode::<SomeEnum>(__contract_entry_arr_inside_enum(args.0)); | ^^^^^^^^^^^^^^^^^^ Trait "AbiEncode" is not implemented for type "SomeEnum". 6 | __contract_ret(result_arr_inside_enum.ptr(), result_arr_inside_enum.len::<u8>()); 7 | } | ____ error --> <autogenerated>:24:61 | 22 | 23 | let args: (SomeEnum,) = decode_second_param::<(SomeEnum,)>(); 24 | let result_str_inside_enum: raw_slice = encode::<SomeEnum>(__contract_entry_str_inside_enum(args.0)); | ^^^^^^^^^^^^^^^^^^ Trait "AbiEncode" is not implemented for type "SomeEnum". 25 | __contract_ret(result_str_inside_enum.ptr(), result_str_inside_enum.len::<u8>()); 26 | } | ____ error: Could not generate the entry method because one of the arguments does not implement AbiEncode/AbiDecode ____ Aborting due to 4 errors. Error: Failed to compile type_inside_enum ```` ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent 80f3fd3 commit 89e5708

File tree

14 files changed

+3058
-1358
lines changed

14 files changed

+3058
-1358
lines changed

sway-core/src/ir_generation/const_eval.rs

+8-21
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use sway_types::{ident::Ident, integer_bits::IntegerBits, span::Spanned, Span};
3131
use sway_utils::mapped_stack::MappedStack;
3232

3333
enum ConstEvalError {
34-
CompileError(CompileError),
34+
CompileError,
3535
CannotBeEvaluatedToConst {
3636
// This is not used at the moment because we do not give detailed description of why a
3737
// const eval failed.
@@ -640,13 +640,7 @@ fn const_eval_typed_expr(
640640
if index < count {
641641
Some(items[index as usize].clone())
642642
} else {
643-
return Err(ConstEvalError::CompileError(
644-
CompileError::ArrayOutOfBounds {
645-
index,
646-
count,
647-
span: expr.span.clone(),
648-
},
649-
));
643+
return Err(ConstEvalError::CompileError);
650644
}
651645
}
652646
_ => {
@@ -657,10 +651,7 @@ fn const_eval_typed_expr(
657651
}
658652
}
659653
ty::TyExpressionVariant::Ref(_) | ty::TyExpressionVariant::Deref(_) => {
660-
return Err(ConstEvalError::CompileError(CompileError::Unimplemented(
661-
"Constant references are currently not supported.",
662-
expr.span.clone(),
663-
)));
654+
return Err(ConstEvalError::CompileError);
664655
}
665656
ty::TyExpressionVariant::Reassignment(_)
666657
| ty::TyExpressionVariant::FunctionParameter
@@ -993,7 +984,7 @@ fn const_eval_intrinsic(
993984
&targ.type_id,
994985
&targ.span,
995986
)
996-
.map_err(ConstEvalError::CompileError)?;
987+
.map_err(|_| ConstEvalError::CompileError)?;
997988
Ok(Some(Constant {
998989
ty: Type::get_uint64(lookup.context),
999990
value: ConstantValue::Uint(ir_type.size(lookup.context).in_bytes()),
@@ -1009,7 +1000,7 @@ fn const_eval_intrinsic(
10091000
&type_id,
10101001
&val.span,
10111002
)
1012-
.map_err(ConstEvalError::CompileError)?;
1003+
.map_err(|_| ConstEvalError::CompileError)?;
10131004
Ok(Some(Constant {
10141005
ty: Type::get_uint64(lookup.context),
10151006
value: ConstantValue::Uint(ir_type.size(lookup.context).in_bytes()),
@@ -1024,7 +1015,7 @@ fn const_eval_intrinsic(
10241015
&targ.type_id,
10251016
&targ.span,
10261017
)
1027-
.map_err(ConstEvalError::CompileError)?;
1018+
.map_err(|_| ConstEvalError::CompileError)?;
10281019
Ok(Some(Constant {
10291020
ty: Type::get_uint64(lookup.context),
10301021
value: ConstantValue::Uint(
@@ -1041,17 +1032,13 @@ fn const_eval_intrinsic(
10411032
&targ.type_id,
10421033
&targ.span,
10431034
)
1044-
.map_err(ConstEvalError::CompileError)?;
1035+
.map_err(|_| ConstEvalError::CompileError)?;
10451036
match ir_type.get_content(lookup.context) {
10461037
TypeContent::StringSlice | TypeContent::StringArray(_) => Ok(Some(Constant {
10471038
ty: Type::get_unit(lookup.context),
10481039
value: ConstantValue::Unit,
10491040
})),
1050-
_ => Err(ConstEvalError::CompileError(
1051-
CompileError::NonStrGenericType {
1052-
span: targ.span.clone(),
1053-
},
1054-
)),
1041+
_ => Err(ConstEvalError::CompileError),
10551042
}
10561043
}
10571044
Intrinsic::ToStrArray => {

0 commit comments

Comments
 (0)