Skip to content

Commit

Permalink
Improved consteval_int diagnostics. (#3921)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Aug 22, 2023
1 parent 7d0a3d4 commit 50f9cfa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
7 changes: 7 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 @@ -24,6 +24,8 @@ const e: felt252 = consteval_int![4 + 5];

const f: felt252 = consteval_int!{4 + 5};

const out_of_range: u8 = consteval_int!(120 + 160);

//! > expected_diagnostics
error: The name `e` is defined multiple times.
--> lib.cairo:11:7
Expand Down Expand Up @@ -135,6 +137,11 @@ error: Only literal constants are currently supported.
const f: felt252 = consteval_int!{4 + 5};
^*******************^

error: The value does not fit within the range of type core::integer::u8.
--> lib.cairo:15:26
const out_of_range: u8 = consteval_int!(120 + 160);
^***********************^

//! > ==========================================================================

//! > Test unknown macro
Expand Down
22 changes: 17 additions & 5 deletions crates/cairo-lang-semantic/src/inline_macros/consteval_int.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use cairo_lang_defs::plugin::{
InlineMacroExprPlugin, InlinePluginResult, PluginDiagnostic, PluginGeneratedFile,
};
use cairo_lang_filesystem::ids::{DiagnosticMapping, DiagnoticOrigin};
use cairo_lang_filesystem::span::{TextOffset, TextSpan, TextWidth};
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::{ast, TypedSyntaxNode};
use num_bigint::BigInt;
Expand All @@ -27,11 +29,21 @@ impl InlineMacroExprPlugin for ConstevalIntMacro {
}
let code = compute_constant_expr(db, &constant_expression.unwrap(), &mut diagnostics);
InlinePluginResult {
code: code.map(|x| PluginGeneratedFile {
name: "consteval_int_inline_macro".into(),
content: x.to_string(),
diagnostics_mappings: vec![],
aux_data: None,
code: code.map(|x| {
let content = x.to_string();
let span = TextSpan {
start: TextOffset::default(),
end: TextOffset::default().add_width(TextWidth::from_str(&content)),
};
PluginGeneratedFile {
name: "consteval_int_inline_macro".into(),
content,
diagnostics_mappings: vec![DiagnosticMapping {
span,
origin: DiagnoticOrigin::Span(syntax.as_syntax_node().span(db)),
}],
aux_data: None,
}
}),
diagnostics,
}
Expand Down

0 comments on commit 50f9cfa

Please sign in to comment.