From 99f14a7ead60873471a40055e69566b867dbc763 Mon Sep 17 00:00:00 2001
From: Thomas Mangin <thomas.mangin@exa.net.uk>
Date: Tue, 14 Dec 2021 11:05:54 +0000
Subject: [PATCH] transformer: only enable array optimisation with -prod
 (#12833)

---
 vlib/v/tests/array_access_optimisation_test.v |  3 ++-
 vlib/v/tests/testdata/test_array_bound.v      |  2 ++
 vlib/v/transformer/transformer.v              | 15 +++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/vlib/v/tests/array_access_optimisation_test.v b/vlib/v/tests/array_access_optimisation_test.v
index 03fd5ed70b..74839fc652 100644
--- a/vlib/v/tests/array_access_optimisation_test.v
+++ b/vlib/v/tests/array_access_optimisation_test.v
@@ -25,7 +25,8 @@ fn access(line string) {
 }
 
 fn test_array_optimisation() {
-	mut args := []string{cap: 3}
+	mut args := []string{cap: 4}
+	args << '-prod'
 	args << test
 	args << '-o'
 	args << '-'
diff --git a/vlib/v/tests/testdata/test_array_bound.v b/vlib/v/tests/testdata/test_array_bound.v
index 1aa5d4a40a..c51d590fb9 100644
--- a/vlib/v/tests/testdata/test_array_bound.v
+++ b/vlib/v/tests/testdata/test_array_bound.v
@@ -93,6 +93,8 @@ fn check_for_c_init_1(a []byte) {
 	for access_it := a[34]; a[34] == 0; {
 		direct(a[34])
 		access(a[35])
+		// work around https://github.com/vlang/v/issues/12832
+		println(access_it)
 		return
 	}
 }
diff --git a/vlib/v/transformer/transformer.v b/vlib/v/transformer/transformer.v
index c200fc96bf..70e5e3661d 100644
--- a/vlib/v/transformer/transformer.v
+++ b/vlib/v/transformer/transformer.v
@@ -156,6 +156,9 @@ pub fn (mut t Transformer) transform(mut ast_file ast.File) {
 }
 
 pub fn (mut t Transformer) find_new_array_len(node ast.AssignStmt) {
+	if !t.pref.is_prod {
+		return
+	}
 	// looking for, array := []type{len:int}
 	mut right := node.right[0]
 	if mut right is ast.ArrayInit {
@@ -185,6 +188,9 @@ pub fn (mut t Transformer) find_new_array_len(node ast.AssignStmt) {
 }
 
 pub fn (mut t Transformer) find_new_range(node ast.AssignStmt) {
+	if !t.pref.is_prod {
+		return
+	}
 	// looking for, array := []type{len:int}
 	mut right := node.right[0]
 	if mut right is ast.IndexExpr {
@@ -214,10 +220,16 @@ pub fn (mut t Transformer) find_new_range(node ast.AssignStmt) {
 }
 
 pub fn (mut t Transformer) find_mut_self_assign(node ast.AssignStmt) {
+	if !t.pref.is_prod {
+		return
+	}
 	// even if mutable we can be sure than `a[1] = a[2] is safe
 }
 
 pub fn (mut t Transformer) find_assert_len(node ast.InfixExpr) {
+	if !t.pref.is_prod {
+		return
+	}
 	right := node.right
 	match right {
 		ast.IntegerLiteral {
@@ -265,6 +277,9 @@ pub fn (mut t Transformer) find_assert_len(node ast.InfixExpr) {
 }
 
 pub fn (mut t Transformer) check_safe_array(mut node ast.IndexExpr) {
+	if !t.pref.is_prod {
+		return
+	}
 	if !node.is_array {
 		return
 	}