Skip to content

Commit

Permalink
Addressing PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerson2102 committed Nov 11, 2024
1 parent bca512f commit 25427e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 55 deletions.
16 changes: 3 additions & 13 deletions crates/cairo-lang-semantic/src/inline_macros/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,28 @@ impl InlineMacroExprPlugin for ArrayMacro {
Some(
indoc! {r#"
Creates a new array containing the provided elements.
The `array!` macro allows you to create an array by specifying a list of elements. \
The elements are added to a new array in the order they are provided.
# Syntax
```cairo
array![element1, element2, element3, ...]
```
# Returns
An array containing the specified elements.
# Examples
```cairo
let arr = array![]; // Creates an empty array.
let arr = array![1, 2, 3]; // Creates an array containing 1, 2, and 3.
let x = 5;
let y = 10;
let arr = array![x, y, x + y]; // Creates an array containing 5, 10, and 15.
```
# Notes
- All elements must be of the same type or compatible types that can be coerced to a common type.
- The macro internally uses `ArrayTrait::new()` to create a new array and `ArrayTrait::append()` to add each element.
- The order of the elements in the array is the same as the order they are provided in the macro.
- Elements must be of the same type or convertible to a common type.
- Uses `ArrayTrait::new()` to create the array and `ArrayTrait::append()` to add elements.
- Elements are added in the order provided.
"#}
.to_string(),
)
Expand Down
30 changes: 9 additions & 21 deletions crates/cairo-lang-semantic/src/inline_macros/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,48 +116,36 @@ impl InlineMacroExprPlugin for AssertMacro {
Some(
indoc! {r#"
Asserts that a condition is true at runtime.
The `assert!` macro is used to check whether a boolean expression evaluates to `true`. \
If the expression evaluates to `false`, the macro will cause the program to panic, \
optionally displaying a custom error message. This is useful for debugging and ensuring \
that certain conditions hold during execution.
The `assert!` macro checks a boolean expression; if it evaluates to `false`, \
it panics with an optional custom error message. Useful for debugging and \
ensuring conditions hold during execution.
# Syntax
```cairo
assert!(condition);
assert!(condition, "error message");
assert!(condition, "formatted error: {}", value);
```
# Parameters
- `condition`: A boolean expression to evaluate. The assertion passes if this expression is `true`.
- `format_string` (optional): A string literal that may include format placeholders.
- `condition`: A boolean expression to evaluate.
- `format_string` (optional): A string literal for format placeholders.
- `args` (optional): Arguments corresponding to the format placeholders in the `format_string`.
# Examples
```cairo
assert!(2 + 2 == 4); // Passes, does nothing.
assert!(2 + 2 == 5); // Panics with "assertion failed: `2 + 2 == 5`."
let age = 18;
assert!(age >= 21, "Age must be at least 21, found {}", age);
// Panics with "Age must be at least 21, found 18."
let x = -1;
assert!(x >= 0, "Invalid value: x = {}", x);
// Panics with "Invalid value: x = -1."
```
# Notes
- **Use Cases**: Ideal for catching programming errors and enforcing invariants.
- **Performance**: Assertions can impact performance; consider using `debug_assert!` for checks in debug mode only.
- **Error Handling**: For recoverable errors, use proper error handling mechanisms like `Result` or `Option` instead of panicking.
- Use to catch programming errors and enforce invariants.
- May impact performance; consider `debug_assert!` for debug-only checks.
- For recoverable errors, prefer using `Result` or `Option` instead of panicking.
"#}
.to_string(),
)
Expand Down
32 changes: 11 additions & 21 deletions crates/cairo-lang-semantic/src/inline_macros/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,33 +136,26 @@ impl InlineMacroExprPlugin for PanicMacro {
Some(
indoc! {r#"
Terminates the program immediately with an error message.
The `panic!` macro is used to halt execution when the program encounters an unrecoverable \
error. It prints an error message to the standard error output and exits the program. \
This macro accepts a format string and arguments, similar to the `format!` macro, allowing for \
detailed error messages.
The `panic!` macro halts execution when an unrecoverable error \
occurs. It prints an error message and exits the program. \
Accepts a format string and arguments, similar to `format!`, \
for detailed error messages.
# Syntax
```cairo
panic!();
panic!("error message");
panic!("formatted error: {}", value);
```
# Behavior
- If called without any arguments, it panics with a default message.
- If provided with a message or formatted string, it panics with the given message.
- The macro constructs the panic message at runtime using the provided format string and arguments.
- Without arguments, panics with a default message.
- With a message or formatted string, panics with that message.
- Constructs the panic message at runtime using the format string and arguments.
# Examples
```cairo
panic!(); // Panics with a default message.
panic!("An unexpected error occurred."); // Panics with the provided message.
let x = 10;
let y = 20;
if x + y != 30 {
Expand All @@ -172,12 +165,9 @@ impl InlineMacroExprPlugin for PanicMacro {
```
# Notes
- Use `panic!` for unrecoverable errors where the program cannot safely continue.
- Panicking will terminate the current execution flow and unwind the stack, which may skip resource cleanup.
- In library code, prefer returning `Result` or `Option` to allow the caller to handle errors.
- Use `panic!` only for unrecoverable errors.
- In library code, prefer returning `Result` or `Option` to let callers handle errors.
- Avoid using `panic!` for control flow or expected error conditions.
"#}
.to_string(),
)
Expand Down

0 comments on commit 25427e0

Please sign in to comment.