Basic support for incomplete types. (#3302)

Incomplete types may be nested within other types; for example, a tuple
type might have an incomplete type as an element. Handle such cases by
walking through nested incomplete types when completing a type. This is
done non-recursively in case a very complex type is formed.

Types are generally no longer completed at the point where they're
formed. Instead, we attempt to complete a type when it is used in a
context that requires a complete type, and diagnose if the type cannot
be completed at that point. This will be necessary for classes, which
can become complete after their first use, and helps tease out bugs
where a type completeness check is missing.
This commit is contained in:
Richard Smith
2023-10-17 19:30:51 +00:00
committed by GitHub
parent 3015135a52
commit 69353ed271
57 changed files with 1141 additions and 597 deletions
+3 -4
View File
@@ -35,10 +35,9 @@ fn Main() {
// CHECK:STDOUT: %.loc9: i32 = int_literal 9
// CHECK:STDOUT: assign %a.ref.loc9, %.loc9
// CHECK:STDOUT: %.loc11_19.1: type = tuple_type (type, type)
// CHECK:STDOUT: %.loc11_19.2: type = ptr_type (type, type)
// CHECK:STDOUT: %.loc11_19.3: (type, type) = tuple_literal (i32, i32)
// CHECK:STDOUT: %.loc11_19.4: type = tuple_type (i32, i32)
// CHECK:STDOUT: %.loc11_19.5: type = ptr_type (i32, i32)
// CHECK:STDOUT: %.loc11_19.2: (type, type) = tuple_literal (i32, i32)
// CHECK:STDOUT: %.loc11_19.3: type = tuple_type (i32, i32)
// CHECK:STDOUT: %.loc11_19.4: type = ptr_type (i32, i32)
// CHECK:STDOUT: %b: ref (i32, i32) = var "b"
// CHECK:STDOUT: %.loc11_24: i32 = int_literal 1
// CHECK:STDOUT: %.loc11_27: i32 = int_literal 2
@@ -7,45 +7,45 @@
fn F() -> i32;
fn Main() {
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:3: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:3: ERROR: Expression is not assignable.
// CHECK:STDERR: 1 = 2;
// CHECK:STDERR: ^
1 = 2;
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:5: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:5: ERROR: Expression is not assignable.
// CHECK:STDERR: F() = 1;
// CHECK:STDERR: ^
F() = 1;
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:8: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:8: ERROR: Expression is not assignable.
// CHECK:STDERR: (1, 2) = (3, 4);
// CHECK:STDERR: ^
(1, 2) = (3, 4);
var n: i32 = 0;
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:8: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:8: ERROR: Expression is not assignable.
// CHECK:STDERR: (n, n) = (1, 2);
// CHECK:STDERR: ^
(n, n) = (1, 2);
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:3: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:3: ERROR: Expression is not assignable.
// CHECK:STDERR: i32 = i32*;
// CHECK:STDERR: ^
i32 = i32*;
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:18: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:18: ERROR: Expression is not assignable.
// CHECK:STDERR: {.x = 1, .y = 2} = {.x = 3, .y = 4};
// CHECK:STDERR: ^
{.x = 1, .y = 2} = {.x = 3, .y = 4};
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:25: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:25: ERROR: Expression is not assignable.
// CHECK:STDERR: (if true then 1 else 2) = 3;
// CHECK:STDERR: ^
(if true then 1 else 2) = 3;
// Under #911, if expressions are never reference expressions.
var a: i32;
// CHECK:STDERR: fail_assigment_to_non_assignable.carbon:[[@LINE+3]]:25: ERROR: Expression is not assignable.
// CHECK:STDERR: fail_assignment_to_non_assignable.carbon:[[@LINE+3]]:25: ERROR: Expression is not assignable.
// CHECK:STDERR: (if true then a else a) = 10;
// CHECK:STDERR: ^
(if true then a else a) = 10;
}
// CHECK:STDOUT: file "fail_assigment_to_non_assignable.carbon" {
// CHECK:STDOUT: file "fail_assignment_to_non_assignable.carbon" {
// CHECK:STDOUT: %F: <function> = fn_decl @F
// CHECK:STDOUT: %Main: <function> = fn_decl @Main
// CHECK:STDOUT: }
@@ -64,18 +64,18 @@ fn Main() {
// CHECK:STDOUT: %.loc21_4: i32 = int_literal 1
// CHECK:STDOUT: %.loc21_7: i32 = int_literal 2
// CHECK:STDOUT: %.loc21_8.1: type = tuple_type (i32, i32)
// CHECK:STDOUT: %.loc21_8.2: type = ptr_type (i32, i32)
// CHECK:STDOUT: %.loc21_8.3: (i32, i32) = tuple_literal (%.loc21_4, %.loc21_7)
// CHECK:STDOUT: %.loc21_8.2: (i32, i32) = tuple_literal (%.loc21_4, %.loc21_7)
// CHECK:STDOUT: %.loc21_13: i32 = int_literal 3
// CHECK:STDOUT: %.loc21_16: i32 = int_literal 4
// CHECK:STDOUT: %.loc21_17.1: (i32, i32) = tuple_literal (%.loc21_13, %.loc21_16)
// CHECK:STDOUT: %.loc21_17.2: i32 = tuple_access %.loc21_8.3, member0
// CHECK:STDOUT: %.loc21_8.3: type = ptr_type (i32, i32)
// CHECK:STDOUT: %.loc21_17.2: i32 = tuple_access %.loc21_8.2, member0
// CHECK:STDOUT: %.loc21_17.3: init i32 = initialize_from %.loc21_13 to %.loc21_17.2
// CHECK:STDOUT: %.loc21_17.4: i32 = tuple_access %.loc21_8.3, member1
// CHECK:STDOUT: %.loc21_17.4: i32 = tuple_access %.loc21_8.2, member1
// CHECK:STDOUT: %.loc21_17.5: init i32 = initialize_from %.loc21_16 to %.loc21_17.4
// CHECK:STDOUT: %.loc21_17.6: init (i32, i32) = tuple_init %.loc21_17.1, (%.loc21_17.3, %.loc21_17.5)
// CHECK:STDOUT: assign %.loc21_8.3, %.loc21_17.6
// CHECK:STDOUT: %.loc21_8.4: (i32, i32) = tuple_value %.loc21_8.3, (%.loc21_4, %.loc21_7)
// CHECK:STDOUT: assign %.loc21_8.2, %.loc21_17.6
// CHECK:STDOUT: %.loc21_8.4: (i32, i32) = tuple_value %.loc21_8.2, (%.loc21_4, %.loc21_7)
// CHECK:STDOUT: %n: ref i32 = var "n"
// CHECK:STDOUT: %.loc22: i32 = int_literal 0
// CHECK:STDOUT: assign %n, %.loc22
@@ -99,18 +99,18 @@ fn Main() {
// CHECK:STDOUT: %.loc34_9: i32 = int_literal 1
// CHECK:STDOUT: %.loc34_17: i32 = int_literal 2
// CHECK:STDOUT: %.loc34_18.1: type = struct_type {.x: i32, .y: i32}
// CHECK:STDOUT: %.loc34_18.2: type = ptr_type {.x: i32, .y: i32}
// CHECK:STDOUT: %.loc34_18.3: {.x: i32, .y: i32} = struct_literal (%.loc34_9, %.loc34_17)
// CHECK:STDOUT: %.loc34_18.2: {.x: i32, .y: i32} = struct_literal (%.loc34_9, %.loc34_17)
// CHECK:STDOUT: %.loc34_28: i32 = int_literal 3
// CHECK:STDOUT: %.loc34_36: i32 = int_literal 4
// CHECK:STDOUT: %.loc34_37.1: {.x: i32, .y: i32} = struct_literal (%.loc34_28, %.loc34_36)
// CHECK:STDOUT: %.loc34_37.2: i32 = struct_access %.loc34_18.3, member0
// CHECK:STDOUT: %.loc34_18.3: type = ptr_type {.x: i32, .y: i32}
// CHECK:STDOUT: %.loc34_37.2: i32 = struct_access %.loc34_18.2, member0
// CHECK:STDOUT: %.loc34_37.3: init i32 = initialize_from %.loc34_28 to %.loc34_37.2
// CHECK:STDOUT: %.loc34_37.4: i32 = struct_access %.loc34_18.3, member1
// CHECK:STDOUT: %.loc34_37.4: i32 = struct_access %.loc34_18.2, member1
// CHECK:STDOUT: %.loc34_37.5: init i32 = initialize_from %.loc34_36 to %.loc34_37.4
// CHECK:STDOUT: %.loc34_37.6: init {.x: i32, .y: i32} = struct_init %.loc34_37.1, (%.loc34_37.3, %.loc34_37.5)
// CHECK:STDOUT: assign %.loc34_18.3, %.loc34_37.6
// CHECK:STDOUT: %.loc34_18.4: {.x: i32, .y: i32} = struct_value %.loc34_18.3, (%.loc34_9, %.loc34_17)
// CHECK:STDOUT: assign %.loc34_18.2, %.loc34_37.6
// CHECK:STDOUT: %.loc34_18.4: {.x: i32, .y: i32} = struct_value %.loc34_18.2, (%.loc34_9, %.loc34_17)
// CHECK:STDOUT: %.loc38_7: bool = bool_literal true
// CHECK:STDOUT: if %.loc38_7 br !if.expr.then.loc38 else br !if.expr.else.loc38
// CHECK:STDOUT: