Support struct implicit conversions in type-checking (#870)

I should emphasize that I am **completely cheating** here. This PR does not add support for actually _performing_  implicit conversions at run time, because the AST doesn't yet contain the necessary type information. At run time, code like `var p: Point = {.x = 1, .y = 2};` directly initializes the name `p` with the _struct_ value `{.x = 1, .y = 2}`; no object of type `Point` is actually created. I'm only getting away with this because we don't yet have any tests that can tell the difference.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
Geoff Romer
2021-10-12 10:27:20 -07:00
committed by GitHub
co-authored by Jon Meow
parent a99d882223
commit bc5a42211b
14 changed files with 226 additions and 60 deletions
+24
View File
@@ -0,0 +1,24 @@
// 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
//
// RUN: executable_semantics %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes=false %s
// RUN: executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: result: 0
package ExecutableSemanticsTest api;
class Point {
var x: i32;
var y: i32;
}
fn main() -> i32 {
var p1: Point = {.x = 1, .y = 2};
var p2: auto = p1;
p2 = {.x = 3, .y = 2};
return p1.x - 1;
}
+1 -1
View File
@@ -17,7 +17,7 @@ class Point {
}
fn main() -> i32 {
var p1: auto = Point(.x = 1, .y = 2);
var p1: Point = {.x = 1, .y = 2};
var p2: auto = p1;
p2.x = 3;
return p1.x - 1;
@@ -7,7 +7,7 @@
// RUN: not executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/class/fail_field_access_mismatch.carbon:20: class Point does not have a field named z
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/class/fail_field_access_mismatch.carbon:21: class Point does not have a field named z
package ExecutableSemanticsTest api;
@@ -17,5 +17,6 @@ class Point {
}
fn main() -> i32 {
return Point(.x = 1, .y = 2).z - 1;
var p: Point = {.x = 1, .y = 2};
return p.z - 1;
}
@@ -7,9 +7,7 @@
// RUN: not executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/class/fail_field_mismatch.carbon:22: type error in call
// CHECK: expected: (x = i32, y = i32)
// CHECK: actual: (x = i32, z = i32)
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/class/fail_field_mismatch.carbon:20: type error in name binding: '{.x: i32, .z: i32}' is not implicitly convertible to 'class Point'
package ExecutableSemanticsTest api;
@@ -19,5 +17,6 @@ class Point {
}
fn main() -> i32 {
return Point(.x = 1, .z = 2).x - 1;
var p: Point = {.x = 1, .z = 2};
return p.x - 1;
}
@@ -7,9 +7,7 @@
// RUN: not executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/class/fail_field_missing.carbon:22: type error in call
// CHECK: expected: (x = i32, y = i32)
// CHECK: actual: (x = i32)
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/class/fail_field_missing.carbon:20: type error in name binding: '{.x: i32}' is not implicitly convertible to 'class Point'
package ExecutableSemanticsTest api;
@@ -19,5 +17,6 @@ class Point {
}
fn main() -> i32 {
return Point(.x = 1).x - 1;
var p: Point = {.x = 1};
return p.x - 1;
}
@@ -0,0 +1,25 @@
// 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
//
// RUN: executable_semantics %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes=false %s
// RUN: executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: result: 0
package ExecutableSemanticsTest api;
class Point {
var x: i32;
var y: i32;
}
fn GetX(p: Point) -> i32 {
return p.x;
}
fn main() -> i32 {
return GetX({.x = 1, .y = 2}) - 1;
}
+23
View File
@@ -0,0 +1,23 @@
// 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
//
// RUN: executable_semantics %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes=false %s
// RUN: executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: result: 0
package ExecutableSemanticsTest api;
class Point {
var x: i32;
var y: i32;
}
var p: Point = {.x = 1, .y = 2};
fn main() -> i32 {
return p.y - p.x - 1;
}
+5 -1
View File
@@ -16,6 +16,10 @@ class Point {
var y: i32;
}
fn MakePoint() -> Point {
return {.x = 1, .y = 2};
}
fn main() -> i32 {
return Point(.x = 1, .y = 2).x - 1;
return MakePoint().x - 1;
}
+1 -1
View File
@@ -17,6 +17,6 @@ class Point {
}
fn main() -> i32 {
var p: auto = Point(.x = 1, .y = 2);
var p: Point = {.x = 1, .y = 2};
return p.y - p.x - 1;
}
@@ -7,9 +7,7 @@
// RUN: not executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/function/fail_call_with_tuple.carbon:21: type error in call
// CHECK: expected: (0 = i32, 1 = i32)
// CHECK: actual: (0 = (0 = i32, 1 = i32))
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/function/fail_call_with_tuple.carbon:19: type error in call: '(0 = (0 = i32, 1 = i32))' is not implicitly convertible to '(0 = i32, 1 = i32)'
package ExecutableSemanticsTest api;
@@ -7,9 +7,7 @@
// RUN: not executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/global_variable/fail_init_type_mismatch.carbon:18: type error in initializer of variable
// CHECK: expected: i32
// CHECK: actual: Bool
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/global_variable/fail_init_type_mismatch.carbon:16: type error in initializer of variable: 'Bool' is not implicitly convertible to 'i32'
package ExecutableSemanticsTest api;
@@ -2,16 +2,16 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// RUN: not executable_semantics %s 2>&1 | \
// RUN: executable_semantics %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes=false %s
// RUN: not executable_semantics --trace %s 2>&1 | \
// RUN: executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/struct/fail_name_order.carbon:17: Type pattern '{.x: i32, .y: i32}' does not match actual type '{.y: i32, .x: i32}'
// CHECK: result: 0
package ExecutableSemanticsTest api;
// Test the that field order matters for structs.
// Test the that field order doesn't matter for structs.
fn main() -> i32 {
var t: {.x: i32, .y: i32} = {.y = 2, .x = 3};
+1 -1
View File
@@ -7,7 +7,7 @@
// RUN: not executable_semantics --trace %s 2>&1 | \
// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: executable_semantics %s
// CHECK: PROGRAM ERROR: {{.*}}/executable_semantics/testdata/tuple/fail_name_order.carbon:17: Tuple field name 'y' does not match pattern field name 'x'
// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/tuple/fail_name_order.carbon:17: type error in name binding: '(y = i32, x = i32)' is not implicitly convertible to '(x = i32, y = i32)'
package ExecutableSemanticsTest api;