summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeedehai <18319900+Leedehai@users.noreply.github.com>2024-01-30 04:30:34 -0500
committerGitHub <noreply@github.com>2024-01-30 09:30:34 +0000
commita6037293367db42c7242cab9dec14662402ef061 (patch)
treee20ab880795ee9f6d09a9db92a43cf664a0cd3b9
parenta434cbb64eb96fd822e6c4a669cfa208c7dd5712 (diff)
Remove an unnecessary clone in loop evaluation (#3297)
-rw-r--r--crates/typst-syntax/src/ast.rs2
-rw-r--r--crates/typst/src/eval/call.rs2
-rw-r--r--crates/typst/src/eval/flow.rs9
3 files changed, 7 insertions, 6 deletions
diff --git a/crates/typst-syntax/src/ast.rs b/crates/typst-syntax/src/ast.rs
index df865906..6f4b52f0 100644
--- a/crates/typst-syntax/src/ast.rs
+++ b/crates/typst-syntax/src/ast.rs
@@ -1967,7 +1967,7 @@ impl<'a> ForLoop<'a> {
}
/// The expression to iterate over.
- pub fn iter(self) -> Expr<'a> {
+ pub fn iterable(self) -> Expr<'a> {
self.0
.children()
.skip_while(|&c| c.kind() != SyntaxKind::In)
diff --git a/crates/typst/src/eval/call.rs b/crates/typst/src/eval/call.rs
index 379cc959..c1249c16 100644
--- a/crates/typst/src/eval/call.rs
+++ b/crates/typst/src/eval/call.rs
@@ -467,7 +467,7 @@ impl<'a> CapturesVisitor<'a> {
// active after the iterable is evaluated but before the body is
// evaluated.
Some(ast::Expr::For(expr)) => {
- self.visit(expr.iter().to_untyped());
+ self.visit(expr.iterable().to_untyped());
self.internal.enter();
let pattern = expr.pattern();
diff --git a/crates/typst/src/eval/flow.rs b/crates/typst/src/eval/flow.rs
index 720b8bcd..82b68192 100644
--- a/crates/typst/src/eval/flow.rs
+++ b/crates/typst/src/eval/flow.rs
@@ -134,10 +134,11 @@ impl Eval for ast::ForLoop<'_> {
}};
}
- let iter = self.iter().eval(vm)?;
+ let iterable = self.iterable().eval(vm)?;
+ let iterable_type = iterable.ty();
let pattern = self.pattern();
- match (&pattern, iter.clone()) {
+ match (&pattern, iterable) {
(ast::Pattern::Normal(_), Value::Str(string)) => {
// Iterate over graphemes of string.
iter!(for pattern in string.as_str().graphemes(true));
@@ -151,10 +152,10 @@ impl Eval for ast::ForLoop<'_> {
iter!(for pattern in array);
}
(ast::Pattern::Normal(_), _) => {
- bail!(self.iter().span(), "cannot loop over {}", iter.ty());
+ bail!(self.iterable().span(), "cannot loop over {}", iterable_type);
}
(_, _) => {
- bail!(pattern.span(), "cannot destructure values of {}", iter.ty())
+ bail!(pattern.span(), "cannot destructure values of {}", iterable_type)
}
}