-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
Expr
methods: as_tuple
, as_parenthesized
, as_index
,…
… `as_if` (#5726) # Description ## Problem Part of #5668 ## Summary Just four, to keep PRs small. ## Additional Context ## Documentation Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [x] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information
Showing
3 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
test_programs/compile_success_empty/comptime_exp/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
fn main() { | ||
comptime | ||
{ | ||
// Check Expr::as_function_call | ||
let expr = quote { foo(bar) }.as_expr().unwrap(); | ||
let (_function, args) = expr.as_function_call().unwrap(); | ||
assert_eq(args.len(), 1); | ||
|
||
// Check Expr::as_index | ||
let expr = quote { foo[bar] }.as_expr().unwrap(); | ||
let _ = expr.as_index().unwrap(); | ||
|
||
// Check Expr::as_tuple | ||
let expr = quote { (1, 2) }.as_expr().unwrap(); | ||
let tuple_exprs = expr.as_tuple().unwrap(); | ||
assert_eq(tuple_exprs.len(), 2); | ||
|
||
// Check Expr::as_if | ||
let expr = quote { if 1 { 2 } }.as_expr().unwrap(); | ||
let (_condition, _consequence, alternative) = expr.as_if().unwrap(); | ||
assert(alternative.is_none()); | ||
|
||
let expr = quote { if 1 { 2 } else { 3 } }.as_expr().unwrap(); | ||
let (_condition, _consequence, alternative) = expr.as_if().unwrap(); | ||
assert(alternative.is_some()); | ||
|
||
// Check parenthesized expression is automatically unwrapped | ||
let expr = quote { ((if 1 { 2 })) }.as_expr().unwrap(); | ||
assert(expr.as_if().is_some()); | ||
} | ||
} |