From 55233eb30282a15a8134046941224a87b0f33e5d Mon Sep 17 00:00:00 2001 From: mertcandav Date: Wed, 13 Mar 2024 12:34:12 +0300 Subject: [PATCH] compiler: fix copy optimization --- src/julec/obj/cxx/scope.jule | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/julec/obj/cxx/scope.jule b/src/julec/obj/cxx/scope.jule index a2bff2ab5..65e64ea8a 100644 --- a/src/julec/obj/cxx/scope.jule +++ b/src/julec/obj/cxx/scope.jule @@ -30,6 +30,7 @@ use std::jule::sema::{ TupleExprModel, TypeKind, BuiltinAppendCallExprModel, + SlicingExprModel, } const MATCH_EXPR = "_match_expr" @@ -53,7 +54,7 @@ impl ScopeCoder { self.oc.add_indent() obj += self.oc.indent() obj += "auto " - if env::OPT_COPY && it.expr.lvalue { + if env::OPT_COPY && is_copy_optimizable(it.expr) { obj += "&" } obj += "expr = " @@ -123,7 +124,7 @@ impl ScopeCoder { self.oc.add_indent() obj += self.oc.indent() obj += "auto " - if env::OPT_COPY && it.expr.lvalue { + if env::OPT_COPY && is_copy_optimizable(it.expr) { obj += "&" } obj += "expr = " @@ -455,7 +456,7 @@ impl ScopeCoder { // Constant expressions generated as literals in conditions. if !generic_type_match && !m.expr.is_const() { - if m.expr.lvalue && env::OPT_COPY { + if env::OPT_COPY && is_copy_optimizable(m.expr) { obj += "auto &_match_expr{ " } else { obj += "auto _match_expr{ " @@ -765,6 +766,17 @@ impl ScopeCoder { } } +fn is_copy_optimizable(&expr: &Data): bool { + if !expr.lvalue { + ret false + } + match type expr.model { + | &SlicingExprModel: + ret false + } + ret true +} + fn is_iter_copy_optimizable(&expr: &Data, &v: &Var): bool { if !expr.lvalue && !expr.kind.mutable() { ret true