Skip to content

Commit

Permalink
Improved array! macro diagnostics.
Browse files Browse the repository at this point in the history
commit-id:944d2078
  • Loading branch information
orizi committed Aug 22, 2023
1 parent 67b3c26 commit 9f35ca1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
34 changes: 34 additions & 0 deletions crates/cairo-lang-semantic/src/expr/test_data/inline_macros
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions crates/cairo-lang-semantic/src/inline_macros/array.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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![],
Expand Down

0 comments on commit 9f35ca1

Please sign in to comment.