mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:24:42 +01:00
Add nested array size deduction from tuple (#2909)
As a follow up to #2825 this pr implements size deduction for nested arrays. E.g. `var x: [[i32;];] = ((1,2), (3,4));`. Also updated the pattern matching logic for arrays, now it also checks element types of the tuple size being deduced from. As a result code like `var x: [i32;] = ("foo", "bar");`(note, that it tries to init an array of i32 with a tuple of strings) fails to compile with a pattern match error instead of an implicit cast failed error. Moved some common type-related logic used in type_checker.cpp and interpreter.cpp to the separate file.
This commit is contained in:
Vendored
+14
@@ -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 {
|
||||
// CHECK:STDERR: COMPILATION ERROR: fail_to_deduce_nested_array_size_from_tuple_different_subtuples_size.carbon:[[@LINE+1]]: type pattern '{{\[\[}}i32;];]' does not match actual type '((i32, i32), (i32, i32, i32))'
|
||||
var x: [[i32;];] = ((1,2), (3,4,5));
|
||||
|
||||
return x[0][2];
|
||||
}
|
||||
Vendored
+13
@@ -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_from_tuple_of_different_types.carbon:[[@LINE+1]]: type pattern '{{\[\[}}i32;]; 2]' does not match actual type '((i32, i32), (String, String))'
|
||||
var x: [[i32;];2] = ((1,2), ("foo", "bar"));
|
||||
return x[0];
|
||||
}
|
||||
+13
@@ -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_tuple_of_different_types.carbon:[[@LINE+1]]: type pattern '[i32;]' does not match actual type '(i32, String)'
|
||||
var x: [i32;] = (42, "foo");
|
||||
return x[0];
|
||||
}
|
||||
@@ -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
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
fn Main() -> i32 {
|
||||
var arr: [[i32;2];2] = ((1,2), (3,4));
|
||||
|
||||
var x : [[i32;];] = arr;
|
||||
// CHECK:STDERR: RUNTIME ERROR: fail_to_index_deduced_nested_size_from_array.carbon:[[@LINE+1]]: index 2 out of range in (1, 2)
|
||||
return x[0][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;];] = ((1,2), (3,4));
|
||||
// CHECK:STDERR: RUNTIME ERROR: fail_to_index_deduced_nested_size_from_tuple.carbon:[[@LINE+1]]: index 2 out of range in (1, 2)
|
||||
return x[0][2];
|
||||
}
|
||||
+16
@@ -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
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
fn Main() -> i32 {
|
||||
var x: [[i32;];] = ((1,2), (3,4));
|
||||
x[0][1] = 1;
|
||||
x[1][0] = 1;
|
||||
return x[0][0] + x[0][1] + x[1][0] + x[1][1];
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: result: 7
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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];2] = ((1,2), (3,4));
|
||||
|
||||
var x : [[i32;];] = arr;
|
||||
x[0][1] = 1;
|
||||
x[1][0] = 1;
|
||||
return x[0][0] + x[0][1] + x[1][0] + x[1][1];
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: result: 7
|
||||
@@ -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 x: [[i32;];2] = ((), ());
|
||||
return 42;
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: result: 42
|
||||
@@ -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
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
fn Main() -> i32 {
|
||||
var x: [[i32;];2] = ((1,2), (3,4));
|
||||
x[0][1] = 1;
|
||||
x[1][0] = 1;
|
||||
return x[0][0] + x[0][1] + x[1][0] + x[1][1];
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: result: 7
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// 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; 2] = (1, 2);
|
||||
var y: [[i32;];] = (x, (3,4));
|
||||
y[0][1] = 1;
|
||||
y[1][0] = 1;
|
||||
return y[0][0] + y[0][1] + y[1][0] + y[1][1];
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: result: 7
|
||||
@@ -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
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
fn Main() -> i32 {
|
||||
let v: i32 = 8;
|
||||
var x: [[[i32;];];] = (((1,2), (3,4)), ((5,6), (7,v)));
|
||||
x[1][1][1] -= v;
|
||||
return x[1][1][1];
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: result: 0
|
||||
Reference in New Issue
Block a user