-
Notifications
You must be signed in to change notification settings - Fork 556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a consteval_int
macro to compute integers at compile-time
#3132
Conversation
a2174eb
to
a240473
Compare
a240473
to
1551c72
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 0 of 7 files reviewed, 1 unresolved discussion (waiting on @wraitii)
tests/bug_samples/issue2964.cairo
line 23 at r1 (raw file):
// This assumes that Drop implies Destruct and Copy implies Clone let mut a = GenericStruct { x: SimpleStruct { x: 1, y: 2 }, y: SimpleStruct { x: 1, y: 2 } }; a.x.x = 24;
What is this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This alters the behaviour of the code. for example 255_u8+1-1 will work with your plugin, but fail without.
I'd rather this be explicit with an inline macro, or at the very least an annotation above the const item, that explains this does operations in the integers realm.
Reviewable status: 0 of 7 files reviewed, 3 unresolved discussions (waiting on @wraitii)
crates/cairo-lang-plugins/src/plugins/arithmetic.rs
line 73 at r1 (raw file):
Some(rec_compute(db, &bin_expr.lhs(db))? - rec_compute(db, &bin_expr.rhs(db))?) } ast::BinaryOperator::Div(_) => {
This might be wrong. For example, felt division is incorrect here. Are u sure u wanna support div?
crates/cairo-lang-plugins/src/plugins/arithmetic.rs
line 76 at r1 (raw file):
Some(rec_compute(db, &bin_expr.lhs(db))? / rec_compute(db, &bin_expr.rhs(db))?) } ast::BinaryOperator::Mod(_) => {
Same as for Div. Felts won't like this.
1551c72
to
3751e87
Compare
consteval_int
macro to compute integers at compile-time
3751e87
to
cd89bc0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 7 files at r1, 9 of 9 files at r2, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @wraitii)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Let's wait for Orizi to also take a look.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @wraitii)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 9 files at r2, all commit messages.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @wraitii)
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 34 at r2 (raw file):
} fn handle_constant(db: &dyn SyntaxGroup, constant_ast: &ast::ItemConstant) -> PluginResult {
doc
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 68 at r2 (raw file):
} fn extract_consteval_macro_expression(
doc
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 93 at r2 (raw file):
} fn rec_compute(
doc
and rename to 'compute_constant_expr'
cd89bc0
to
6748e25
Compare
Done all inlines. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 4 of 4 files at r3, all commit messages.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @spapinistarkware and @wraitii)
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 36 at r3 (raw file):
// Rewrite a constant declaration that contains a consteval_int macro // into a constant declaration with the computed value, // e.g. `const a: felt252 = consteval_int!(2 * 2 * 2);` into `const a: felt252 = 8;`.
Suggestion:
/// Rewrite a constant declaration that contains a consteval_int macro
/// into a constant declaration with the computed value,
/// e.g. `const a: felt252 = consteval_int!(2 * 2 * 2);` into `const a: felt252 = 8;`.
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 71 at r3 (raw file):
} // Extract the actual expression from the consteval_int macro, or fail with diagnostics.
Suggestion:
/// Extract the actual expression from the consteval_int macro, or fail with diagnostics.
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 98 at r3 (raw file):
// Compute the actual value of an integer expression, or fail with diagnostics. // This computation handles arbitrary integers, unlike regular Cairo math.
Suggestion:
/// Compute the actual value of an integer expression, or fail with diagnostics.
/// This computation handles arbitrary integers, unlike regular Cairo math.
tests/bug_samples/issue3130.cairo
line 7 at r3 (raw file):
#[test] fn main() {}
add to bug_samples/lib.cairo
Code quote:
const a: felt252 = (4 + 2 * 3) * 256;
const b: felt252 = 0xff & (24 + 5 * 2);
const c: felt252 = -0xff & (24 + 5 * 2);
const d: felt252 = 0xff | (24 + 5 * 2);
#[test]
fn main() {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 8 of 11 files reviewed, 6 unresolved discussions (waiting on @orizi and @spapinistarkware)
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 36 at r3 (raw file):
// Rewrite a constant declaration that contains a consteval_int macro // into a constant declaration with the computed value, // e.g. `const a: felt252 = consteval_int!(2 * 2 * 2);` into `const a: felt252 = 8;`.
Done.
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 71 at r3 (raw file):
} // Extract the actual expression from the consteval_int macro, or fail with diagnostics.
Done.
crates/cairo-lang-plugins/src/plugins/consteval_int.rs
line 98 at r3 (raw file):
// Compute the actual value of an integer expression, or fail with diagnostics. // This computation handles arbitrary integers, unlike regular Cairo math.
Done.
tests/bug_samples/issue3130.cairo
line 7 at r3 (raw file):
Previously, orizi wrote…
add to
bug_samples/lib.cairo
Done, and fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 3 files at r4, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @spapinistarkware and @wraitii)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 4 files at r3, 3 of 3 files at r4, all commit messages.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @wraitii)
Head branch was pushed to by a user without write access
a9fffbc
to
c10f79a
Compare
@orizi @spapinistarkware Cairo_fmt wants to add a space before the 'consteval_int', but that suggestion actually crashes the Cairo compiler. I'm not entirely sure how to fix this, it's not obvious to me where this space gets added in the formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to fix something in the formatter. (As this is the first macro usage)
Will fix tomorrow and ping here.
Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @wraitii)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you check again?
Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @wraitii)
c10f79a
to
787c9b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 9 files at r2, 1 of 1 files at r6, all commit messages.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @wraitii)
Fixes #3130
This introduces a Cairo plugin that converts constant expressions in constant into their actual value, so that we can write code such as the below without requiring further compiler changes. I think it's sufficient to do this in constants.
I have a separate diff on a 'pow operator' (
2**128
) that I can push next if that is wanted.This change is