From 8fb99419ba49ead5b93b9d62506297bca8e5ab81 Mon Sep 17 00:00:00 2001 From: glihm Date: Tue, 16 Jan 2024 23:32:00 -0600 Subject: [PATCH] fix: add more info on model deserialization fail (#1325) * feat: add more info on model deserialization fail * fix: fix cairo tests * fix: use trait path function call instead of prelude * fix: fix cairo tests * fix tests * fix test --- crates/dojo-lang/src/inline_macros/get.rs | 17 +- .../dojo-lang/src/manifest_test_data/manifest | 2 +- crates/dojo-lang/src/semantics/test_data/get | 337 +++++++++++++++++- 3 files changed, 332 insertions(+), 24 deletions(-) diff --git a/crates/dojo-lang/src/inline_macros/get.rs b/crates/dojo-lang/src/inline_macros/get.rs index c5e6e9d2cd..6b8020036a 100644 --- a/crates/dojo-lang/src/inline_macros/get.rs +++ b/crates/dojo-lang/src/inline_macros/get.rs @@ -103,8 +103,14 @@ impl InlineMacroExprPlugin for GetMacro { } let mut lookup_err_msg = format!("{} not found", model.to_string()); lookup_err_msg.truncate(CAIRO_ERR_MSG_LEN); - let mut deser_err_msg = format!("{} failed to deserialize", model.to_string()); - deser_err_msg.truncate(CAIRO_ERR_MSG_LEN); + // Currently, the main reason to have a deserialization to fail is by having + // the user providing the wrong keys length, which causes an invalid offset + // in the model deserialization. + let deser_err_msg = format!( + "\"Model `{}`: deserialization failed. Ensure the length of the keys tuple is \ + matching the number of #[key] fields in the model struct.\"", + model.to_string() + ); builder.add_str(&format!( "\n let mut __{model}_layout__ = core::array::ArrayTrait::new(); @@ -122,10 +128,11 @@ impl InlineMacroExprPlugin for GetMacro { core::array::serialize_array_helper(__{model}_values__, ref __{model}_model__); let mut __{model}_model_span__ = \ core::array::ArrayTrait::span(@__{model}_model__); - let __{model} = \ - core::option::OptionTrait::expect(core::serde::Serde::<{model}>::deserialize( + let __{model} = core::serde::Serde::<{model}>::deserialize( ref __{model}_model_span__ - ), '{deser_err_msg}');\n", + ); if core::option::OptionTrait::<{model}>::is_none(@__{model}) {{ \ + panic!({deser_err_msg}); }}; let __{model} = \ + core::option::OptionTrait::<{model}>::unwrap(__{model});\n", world.as_syntax_node().get_text(db), )); } diff --git a/crates/dojo-lang/src/manifest_test_data/manifest b/crates/dojo-lang/src/manifest_test_data/manifest index 76df306c7a..930a248e15 100644 --- a/crates/dojo-lang/src/manifest_test_data/manifest +++ b/crates/dojo-lang/src/manifest_test_data/manifest @@ -955,7 +955,7 @@ test_manifest_file { "name": "dojo_examples::actions::actions", "address": null, - "class_hash": "0x69c6bec7de74fc2404fe6b68ad8ece7be81ad6d861b38a8ba8fa583bfc3666b", + "class_hash": "0x352a6ab719469097c1f3f9db41552fc79a1bf09b83c8a0839052218461ecea9", "abi": [ { "type": "impl", diff --git a/crates/dojo-lang/src/semantics/test_data/get b/crates/dojo-lang/src/semantics/test_data/get index f08b775721..d8816cfa5e 100644 --- a/crates/dojo-lang/src/semantics/test_data/get +++ b/crates/dojo-lang/src/semantics/test_data/get @@ -420,27 +420,328 @@ Block( ), expr: FunctionCall( ExprFunctionCall { - function: core::option::OptionTraitImpl::::expect, + function: test::HealthSerde::deserialize, args: [ - Value( - FunctionCall( - ExprFunctionCall { - function: test::HealthSerde::deserialize, - args: [ - Reference( - LocalVarId(test::__Health_model_span__), - ), - ], - ty: core::option::Option::, - }, - ), + Reference( + LocalVarId(test::__Health_model_span__), ), + ], + ty: core::option::Option::, + }, + ), + }, + ), + Expr( + StatementExpr { + expr: If( + ExprIf { + condition: FunctionCall( + ExprFunctionCall { + function: core::option::OptionTraitImpl::::is_none, + args: [ + Value( + Snapshot( + ExprSnapshot { + inner: Var( + LocalVarId(test::__Health), + ), + ty: @core::option::Option::, + }, + ), + ), + ], + ty: core::bool, + }, + ), + if_block: Block( + ExprBlock { + statements: [ + Expr( + StatementExpr { + expr: Block( + ExprBlock { + statements: [ + Let( + StatementLet { + pattern: Variable( + __formatter_for_panic_macro__, + ), + expr: FunctionCall( + ExprFunctionCall { + function: core::fmt::FormatterDefault::default, + args: [], + ty: core::fmt::Formatter, + }, + ), + }, + ), + Expr( + StatementExpr { + expr: FunctionCall( + ExprFunctionCall { + function: core::result::ResultTraitImpl::<(), core::fmt::Error>::unwrap::, + args: [ + Value( + Block( + ExprBlock { + statements: [ + Expr( + StatementExpr { + expr: FunctionCall( + ExprFunctionCall { + function: core::byte_array::ByteArrayImpl::append_word, + args: [ + Reference( + LocalVarId(test::__formatter_for_panic_macro__)::buffer, + ), + Value( + Literal( + ExprLiteral { + value: 136816024508118168988575128292127322950586093870590548369522287650670210926, + ty: core::felt252, + }, + ), + ), + Value( + Literal( + ExprLiteral { + value: 31, + ty: core::integer::u32, + }, + ), + ), + ], + ty: (), + }, + ), + }, + ), + Expr( + StatementExpr { + expr: FunctionCall( + ExprFunctionCall { + function: core::byte_array::ByteArrayImpl::append_word, + args: [ + Reference( + LocalVarId(test::__formatter_for_panic_macro__)::buffer, + ), + Value( + Literal( + ExprLiteral { + value: 57245710417489827494872497475953274453670361818009142277461875365354283124, + ty: core::felt252, + }, + ), + ), + Value( + Literal( + ExprLiteral { + value: 31, + ty: core::integer::u32, + }, + ), + ), + ], + ty: (), + }, + ), + }, + ), + Expr( + StatementExpr { + expr: FunctionCall( + ExprFunctionCall { + function: core::byte_array::ByteArrayImpl::append_word, + args: [ + Reference( + LocalVarId(test::__formatter_for_panic_macro__)::buffer, + ), + Value( + Literal( + ExprLiteral { + value: 184450045146430526813493865095770350343962517127059696327898282247556767854, + ty: core::felt252, + }, + ), + ), + Value( + Literal( + ExprLiteral { + value: 31, + ty: core::integer::u32, + }, + ), + ), + ], + ty: (), + }, + ), + }, + ), + Expr( + StatementExpr { + expr: FunctionCall( + ExprFunctionCall { + function: core::byte_array::ByteArrayImpl::append_word, + args: [ + Reference( + LocalVarId(test::__formatter_for_panic_macro__)::buffer, + ), + Value( + Literal( + ExprLiteral { + value: 207476049689134978464526160084214572942096890522908346890176432476061507693, + ty: core::felt252, + }, + ), + ), + Value( + Literal( + ExprLiteral { + value: 31, + ty: core::integer::u32, + }, + ), + ), + ], + ty: (), + }, + ), + }, + ), + Expr( + StatementExpr { + expr: FunctionCall( + ExprFunctionCall { + function: core::byte_array::ByteArrayImpl::append_word, + args: [ + Reference( + LocalVarId(test::__formatter_for_panic_macro__)::buffer, + ), + Value( + Literal( + ExprLiteral { + value: 34474207625732236007199437870, + ty: core::felt252, + }, + ), + ), + Value( + Literal( + ExprLiteral { + value: 12, + ty: core::integer::u32, + }, + ), + ), + ], + ty: (), + }, + ), + }, + ), + ], + tail: Some( + EnumVariantCtor( + ExprEnumVariantCtor { + variant: ConcreteVariant { + concrete_enum_id: ConcreteEnumLongId { + enum_id: EnumId( + 5, + ), + generic_args: [ + Type( + TypeId( + 3, + ), + ), + Type( + TypeId( + 328, + ), + ), + ], + }, + id: VariantId(core::result::Ok), + ty: (), + idx: 0, + }, + value_expr: Tuple( + ExprTuple { + items: [], + ty: (), + }, + ), + ty: core::result::Result::<(), core::fmt::Error>, + }, + ), + ), + ty: core::result::Result::<(), core::fmt::Error>, + }, + ), + ), + ], + ty: (), + }, + ), + }, + ), + ], + tail: Some( + FunctionCall( + ExprFunctionCall { + function: core::panics::panic_with_byte_array, + args: [ + Value( + Snapshot( + ExprSnapshot { + inner: MemberAccess( + ExprMemberAccess { + expr: Var( + LocalVarId(test::__formatter_for_panic_macro__), + ), + concrete_struct_id: core::fmt::Formatter, + member: MemberId(core::fmt::buffer), + ty: core::byte_array::ByteArray, + }, + ), + ty: @core::byte_array::ByteArray, + }, + ), + ), + ], + ty: core::never, + }, + ), + ), + ty: core::never, + }, + ), + }, + ), + ], + tail: None, + ty: (), + }, + ), + else_block: None, + ty: (), + }, + ), + }, + ), + Let( + StatementLet { + pattern: Variable( + __Health, + ), + expr: FunctionCall( + ExprFunctionCall { + function: core::option::OptionTraitImpl::::unwrap, + args: [ Value( - Literal( - ExprLiteral { - value: 7624190543980043505746182176517533098884860453715398488377751337573, - ty: core::felt252, - }, + Var( + LocalVarId(test::__Health), ), ), ],