Add array size deduction from tuple(fix#1590) (#2825)

Allow array size deduction from tuples and arrays on array declaration

Closes #1590
This commit is contained in:
kshokhin
2023-05-22 11:36:29 -07:00
committed by GitHub
parent 8ad08e34e2
commit 40b3518d37
21 changed files with 341 additions and 53 deletions
+16
View File
@@ -0,0 +1,16 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
var arr: [i32; 2] = (0, 1);
var x: [i32;] = arr;
var index: i32 = 1;
x[index] = 0;
return x[0] + x[1];
}
@@ -0,0 +1,16 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
var arr: [i32; 2] = (0, 1);
var (x: auto, y: [i32;]) = (1, arr);
var index: i32 = 1;
y[index] = 0;
return y[0] + y[1];
}
+15
View File
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
var x: [i32;] = (0, 1);
var index: i32 = 1;
x[index] = 0;
return x[0] + x[1];
}
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
var (x: auto, y: [i32;]) = (42, (0, 1));
var index: i32 = 1;
y[index] = 0;
return y[0] + y[1];
}
@@ -0,0 +1,14 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
var a: [i32;0] = ();
var b: [i32;] = a;
return 0;
}
@@ -0,0 +1,13 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
var a: [i32;] = ();
return 0;
}
@@ -0,0 +1,14 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
package ExplorerTest api;
fn Main() -> i32 {
var arr: [i32; 2] = (0, 1);
var x: [i32;] = arr;
// CHECK:STDERR: RUNTIME ERROR: fail_index_deduced_size_from_array.carbon:[[@LINE+1]]: index 2 out of range in (0, 1)
return x[2];
}
@@ -0,0 +1,13 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
package ExplorerTest api;
fn Main() -> i32 {
var x: [i32;] = (0, 1);
// CHECK:STDERR: RUNTIME ERROR: fail_index_deduced_size_from_tuple.carbon:[[@LINE+1]]: index 2 out of range in (0, 1)
return x[2];
}
@@ -0,0 +1,13 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: fail_to_deduce_nested_array_size_in_tuple_no_expression_to_deduce_from.carbon:[[@LINE+1]]: cannot deduce size for [i32;] in x: (i32, [i32;])
var x: (i32, [i32;]);
return x[0];
}
@@ -0,0 +1,13 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: fail_to_deduce_size_from_int.carbon:[[@LINE+1]]: type pattern '[i32;]' does not match actual type 'i32'
var x: [i32;] = 42;
return x[0];
}
@@ -0,0 +1,13 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: fail_to_deduce_size_no_expression_to_deduce_from.carbon:[[@LINE+1]]: cannot deduce size for x: [i32;]
var x: [i32;];
return x[0];
}