From 82b2a6380367fcb9f767489f806a170bf9ad6d79 Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Wed, 16 Aug 2023 16:57:47 +0300 Subject: [PATCH] Improved array! macro diagnostics. commit-id:944d2078 --- .../src/expr/test_data/inline_macros | 34 +++++++++++++++++++ .../src/inline_macros/array.rs | 24 +++++++------ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/crates/cairo-lang-semantic/src/expr/test_data/inline_macros b/crates/cairo-lang-semantic/src/expr/test_data/inline_macros index 07b98c5c23a..b16e798bef8 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/inline_macros +++ b/crates/cairo-lang-semantic/src/expr/test_data/inline_macros @@ -144,6 +144,40 @@ const out_of_range: u8 = consteval_int!(120 + 160); //! > ========================================================================== +//! > Test bad array! macros + +//! > test_runner_name +test_function_diagnostics + +//! > function +fn foo() { + let x = array!(0); + let x = array![0_felt252, 1_u8]; +} + +//! > function_name +foo + +//! > module_code + +//! > expected_diagnostics +error: Plugin diagnostic: Macro array does not support this bracket type + --> lib.cairo:2:13 + let x = array!(0); + ^*******^ + +error: Inline macro `array` failed. + --> lib.cairo:2:13 + let x = array!(0); + ^*******^ + +error: Plugin diagnostic: Unexpected argument type. Expected: "core::felt252", found: "core::integer::u8". + --> lib.cairo:3:31 + let x = array![0_felt252, 1_u8]; + ^**^ + +//! > ========================================================================== + //! > Test unknown macro //! > test_runner_name diff --git a/crates/cairo-lang-semantic/src/inline_macros/array.rs b/crates/cairo-lang-semantic/src/inline_macros/array.rs index d60e5b2bf9f..d70df02e3a6 100644 --- a/crates/cairo-lang-semantic/src/inline_macros/array.rs +++ b/crates/cairo-lang-semantic/src/inline_macros/array.rs @@ -1,3 +1,4 @@ +use cairo_lang_defs::patcher::PatchBuilder; use cairo_lang_defs::plugin::{InlineMacroExprPlugin, InlinePluginResult, PluginGeneratedFile}; use cairo_lang_syntax::node::db::SyntaxGroup; use cairo_lang_syntax::node::{ast, TypedSyntaxNode}; @@ -15,25 +16,28 @@ impl InlineMacroExprPlugin for ArrayMacro { let ast::WrappedExprList::BracketedExprList(args) = syntax.arguments(db) else { return unsupported_bracket_diagnostic(db, syntax); }; + let mut builder = PatchBuilder::new(db); let args = args.expressions(db).elements(db); - let mut code = "{ - let mut __array_builder_macro_result__ = ArrayTrait::new();" - .to_string(); + builder.add_str( + "{ + let mut __array_builder_macro_result__ = ArrayTrait::new();", + ); for arg in args { - code.push_str(&format!( - "\n array::ArrayTrait::append(ref __array_builder_macro_result__, {});", - arg.as_syntax_node().get_text(db) - )); + builder.add_str( + "\n array::ArrayTrait::append(ref __array_builder_macro_result__, ", + ); + builder.add_node(arg.as_syntax_node()); + builder.add_str(");"); } - code.push_str( + builder.add_str( "\n __array_builder_macro_result__ }", ); InlinePluginResult { code: Some(PluginGeneratedFile { name: "array_inline_macro".into(), - content: code, - diagnostics_mappings: vec![], + content: builder.code, + diagnostics_mappings: builder.diagnostics_mappings, aux_data: None, }), diagnostics: vec![],