mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Tuple restrictions (#486)
* implement tuple restrictions: (1) named fields after positional, (2) order matters * after_named_member => seen_named_member
This commit is contained in:
+1
-3
@@ -1,4 +1,2 @@
|
||||
10: type error in call
|
||||
expected: (0 = Int@0, 1 = Int@1)
|
||||
actual: (0 = (0 = Int@6, 1 = Int@7)@8)
|
||||
10: compilation error, tuples of different length
|
||||
EXIT CODE: 255
|
||||
|
||||
+1
-1
@@ -5,5 +5,5 @@
|
||||
fn f(Int: x, .d = Int: y, Int: z, .e = Int: a) => (x + y) - (z + a);
|
||||
|
||||
fn main() -> Int {
|
||||
return f(.e = 2, 1, 3, .d = 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+2
-1
@@ -1 +1,2 @@
|
||||
result: 0
|
||||
5: positional members must come before named members
|
||||
EXIT CODE: 255
|
||||
|
||||
+1
-5
@@ -5,12 +5,8 @@
|
||||
fn main() -> Int {
|
||||
var auto: t = fnty (Int,Int);
|
||||
var Int: x = 0;
|
||||
// match (t) {
|
||||
// case fnty(Int: x, Int: y): z =>
|
||||
// x = x - 1;
|
||||
// }
|
||||
match (t) {
|
||||
case fnty(Int,Int): z =>
|
||||
case Type: z =>
|
||||
x = x + 1;
|
||||
}
|
||||
match (t) {
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
fn main() -> Int {
|
||||
var (.x = Int, .y = Int): t2 = (.y = 5, .x = 2);
|
||||
var (.x = Int, .y = Int): t2 = (.x = 2, .y = 5);
|
||||
t2.y = 3;
|
||||
return t2.y - t2.x - 1; // 3 - 2 - 1
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,6 +3,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
fn main() -> Int {
|
||||
var (.x = Int, Int): t = (3, .x = 2);
|
||||
var (Int, .x = Int): t = (3, .x = 2);
|
||||
return t.x + 1 - t[0];
|
||||
}
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// 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
|
||||
|
||||
fn main() -> Int {
|
||||
var auto: t = (.x = 2, 3);
|
||||
return 0;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
6: positional members must come before named members
|
||||
EXIT CODE: 255
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// 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
|
||||
|
||||
// Test the that field order matters for tuples.
|
||||
|
||||
fn main() -> Int {
|
||||
var (.x = Int, .y = Int): t = (.y = 2, .x = 3);
|
||||
return 0;
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
8: type error in pattern variable
|
||||
expected: (x = Int@2, y = Int@3)
|
||||
actual: (y = Int@0, x = Int@1)
|
||||
EXIT CODE: 255
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
|
||||
fn main() -> Int {
|
||||
var (Int,Int): t1 = (5, 2);
|
||||
var (Int,Int): t2 = (5,);
|
||||
var (Int,): t2 = (5,);
|
||||
if (t1 == t2) {
|
||||
return 1;
|
||||
} else {
|
||||
|
||||
+4
-1
@@ -1 +1,4 @@
|
||||
result: 0
|
||||
8: type error in ==
|
||||
expected: (0 = Int@2, 1 = Int@3)
|
||||
actual: (0 = Int@5)
|
||||
EXIT CODE: 255
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
// Test matching with a mixture of positional and named fields.
|
||||
|
||||
fn main() -> Int {
|
||||
var auto: t = (.x = 5, 2);
|
||||
var auto: t = (2, .x = 5);
|
||||
match (t) {
|
||||
case (auto: a, .x = auto: b) =>
|
||||
return a - b + 3;
|
||||
|
||||
Reference in New Issue
Block a user