Add name_reference SemIR node for references to names. (#3260)

Also add `name_reference_untyped` for references to non-first-class
names without types, which currently covers namespaces and functions.

This improves the fidelity of the SemIR representation, and fixes some
issues where we would use the wrong location for nodes and diagnostics
downstream of a name reference.

We're still missing a representation for dotted name expressions, such
as `Namespace.Function`, and we don't use the `untyped` node as an
operand of any other node yet.
This commit is contained in:
Richard Smith
2023-10-05 01:15:53 +00:00
committed by GitHub
parent d552545c6d
commit 5ccee62918
97 changed files with 489 additions and 235 deletions
+2
View File
@@ -23,11 +23,13 @@ fn G() {
// CHECK:STDOUT: %.loc10_28: i32 = int_literal 2
// CHECK:STDOUT: %.loc10_29: type = array_type %.loc10_28, (i32, i32, i32)
// CHECK:STDOUT: %v: ref [(i32, i32, i32); 2] = var "v"
// CHECK:STDOUT: %F.ref.loc10_34 = name_reference_untyped "F", package.%F
// CHECK:STDOUT: %.loc10_42.3: ref (i32, i32, i32) = splice_block %.loc10_42.2 {
// CHECK:STDOUT: %.loc10_42.1: i32 = int_literal 0
// CHECK:STDOUT: %.loc10_42.2: ref (i32, i32, i32) = array_index %v, %.loc10_42.1
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc10_35: init (i32, i32, i32) = call @F() to %.loc10_42.3
// CHECK:STDOUT: %F.ref.loc10_39 = name_reference_untyped "F", package.%F
// CHECK:STDOUT: %.loc10_42.6: ref (i32, i32, i32) = splice_block %.loc10_42.5 {
// CHECK:STDOUT: %.loc10_42.4: i32 = int_literal 1
// CHECK:STDOUT: %.loc10_42.5: ref (i32, i32, i32) = array_index %v, %.loc10_42.4
@@ -28,6 +28,7 @@ fn Run() {
// CHECK:STDOUT: %.loc10_16: i32 = int_literal 1
// CHECK:STDOUT: %.loc10_17: type = array_type %.loc10_16, i32
// CHECK:STDOUT: %t: ref [i32; 1] = var "t"
// CHECK:STDOUT: %F.ref = name_reference_untyped "F", package.%F
// CHECK:STDOUT: %.loc10_22.1: init (i32,) = call @F()
// CHECK:STDOUT: %.loc10_22.2: ref (i32,) = temporary_storage
// CHECK:STDOUT: %.loc10_22.3: ref (i32,) = temporary %.loc10_22.2, %.loc10_22.1
+18 -17
View File
@@ -27,21 +27,22 @@ var b: [i32; 3] = a;
// CHECK:STDOUT: %.loc8_14: i32 = int_literal 3
// CHECK:STDOUT: %.loc8_15: type = array_type %.loc8_14, i32
// CHECK:STDOUT: %b: ref [i32; 3] = var "b"
// CHECK:STDOUT: %.loc7_5.1: ref i32 = tuple_access %a, member0
// CHECK:STDOUT: %.loc7_5.2: i32 = bind_value %.loc7_5.1
// CHECK:STDOUT: %.loc7_5.3: i32 = int_literal 0
// CHECK:STDOUT: %.loc7_5.4: ref i32 = array_index %b, %.loc7_5.3
// CHECK:STDOUT: %.loc7_5.5: init i32 = initialize_from %.loc7_5.2 to %.loc7_5.4
// CHECK:STDOUT: %.loc7_5.6: ref i32 = tuple_access %a, member1
// CHECK:STDOUT: %.loc7_5.7: i32 = bind_value %.loc7_5.6
// CHECK:STDOUT: %.loc7_5.8: i32 = int_literal 1
// CHECK:STDOUT: %.loc7_5.9: ref i32 = array_index %b, %.loc7_5.8
// CHECK:STDOUT: %.loc7_5.10: init i32 = initialize_from %.loc7_5.7 to %.loc7_5.9
// CHECK:STDOUT: %.loc7_5.11: ref i32 = tuple_access %a, member2
// CHECK:STDOUT: %.loc7_5.12: i32 = bind_value %.loc7_5.11
// CHECK:STDOUT: %.loc7_5.13: i32 = int_literal 2
// CHECK:STDOUT: %.loc7_5.14: ref i32 = array_index %b, %.loc7_5.13
// CHECK:STDOUT: %.loc7_5.15: init i32 = initialize_from %.loc7_5.12 to %.loc7_5.14
// CHECK:STDOUT: %.loc7_5.16: init [i32; 3] = array_init %a, (%.loc7_5.5, %.loc7_5.10, %.loc7_5.15) to %b
// CHECK:STDOUT: assign %b, %.loc7_5.16
// CHECK:STDOUT: %a.ref: ref (i32, i32, i32) = name_reference "a", %a
// CHECK:STDOUT: %.loc8_19.1: ref i32 = tuple_access %a.ref, member0
// CHECK:STDOUT: %.loc8_19.2: i32 = bind_value %.loc8_19.1
// CHECK:STDOUT: %.loc8_19.3: i32 = int_literal 0
// CHECK:STDOUT: %.loc8_19.4: ref i32 = array_index %b, %.loc8_19.3
// CHECK:STDOUT: %.loc8_19.5: init i32 = initialize_from %.loc8_19.2 to %.loc8_19.4
// CHECK:STDOUT: %.loc8_19.6: ref i32 = tuple_access %a.ref, member1
// CHECK:STDOUT: %.loc8_19.7: i32 = bind_value %.loc8_19.6
// CHECK:STDOUT: %.loc8_19.8: i32 = int_literal 1
// CHECK:STDOUT: %.loc8_19.9: ref i32 = array_index %b, %.loc8_19.8
// CHECK:STDOUT: %.loc8_19.10: init i32 = initialize_from %.loc8_19.7 to %.loc8_19.9
// CHECK:STDOUT: %.loc8_19.11: ref i32 = tuple_access %a.ref, member2
// CHECK:STDOUT: %.loc8_19.12: i32 = bind_value %.loc8_19.11
// CHECK:STDOUT: %.loc8_19.13: i32 = int_literal 2
// CHECK:STDOUT: %.loc8_19.14: ref i32 = array_index %b, %.loc8_19.13
// CHECK:STDOUT: %.loc8_19.15: init i32 = initialize_from %.loc8_19.12 to %.loc8_19.14
// CHECK:STDOUT: %.loc8_19.16: init [i32; 3] = array_init %a.ref, (%.loc8_19.5, %.loc8_19.10, %.loc8_19.15) to %b
// CHECK:STDOUT: assign %b, %.loc8_19.16
// CHECK:STDOUT: }
+18 -16
View File
@@ -9,10 +9,10 @@
// CHECK:STDERR: ^
var a: [i32; 3] = (1, "Hello", "World");
// CHECK:STDERR: fail_type_mismatch.carbon:[[@LINE+3]]:5: ERROR: Cannot implicitly convert from `String` to `i32`.
// CHECK:STDERR: var t1: (i32, String, String);
// CHECK:STDERR: ^
var t1: (i32, String, String);
// CHECK:STDERR: fail_type_mismatch.carbon:[[@LINE+3]]:19: ERROR: Cannot implicitly convert from `String` to `i32`.
// CHECK:STDERR: var b: [i32; 3] = t1;
// CHECK:STDERR: ^
var b: [i32; 3] = t1;
// CHECK:STDERR: fail_type_mismatch.carbon:[[@LINE+3]]:24: ERROR: Cannot initialize array of 3 element(s) from 2 initializer(s).
@@ -20,10 +20,10 @@ var b: [i32; 3] = t1;
// CHECK:STDERR: ^
var c: [i32; 3] = (1, 2);
// CHECK:STDERR: fail_type_mismatch.carbon:[[@LINE+3]]:5: ERROR: Cannot initialize array of 3 element(s) from tuple with 2 element(s).
// CHECK:STDERR: var t2: (i32, i32);
// CHECK:STDERR: ^
var t2: (i32, i32);
// CHECK:STDERR: fail_type_mismatch.carbon:[[@LINE+3]]:19: ERROR: Cannot initialize array of 3 element(s) from tuple with 2 element(s).
// CHECK:STDERR: var d: [i32; 3] = t2;
// CHECK:STDERR: ^
var d: [i32; 3] = t2;
// CHECK:STDOUT: file "fail_type_mismatch.carbon" {
@@ -39,18 +39,19 @@ var d: [i32; 3] = t2;
// CHECK:STDOUT: %.loc10_39.4: ref i32 = array_index %a, %.loc10_39.3
// CHECK:STDOUT: %.loc10_39.5: init i32 = initialize_from %.loc10_20 to %.loc10_39.4
// CHECK:STDOUT: assign %a, <error>
// CHECK:STDOUT: %.loc15_29.1: type = tuple_type (type, type, type)
// CHECK:STDOUT: %.loc15_29.2: (type, type, type) = tuple_literal (i32, String, String)
// CHECK:STDOUT: %.loc12_29.1: type = tuple_type (type, type, type)
// CHECK:STDOUT: %.loc12_29.2: (type, type, type) = tuple_literal (i32, String, String)
// CHECK:STDOUT: %t1: ref (i32, String, String) = var "t1"
// CHECK:STDOUT: %.loc16_14: i32 = int_literal 3
// CHECK:STDOUT: %.loc16_15: type = array_type %.loc16_14, i32
// CHECK:STDOUT: %b: ref [i32; 3] = var "b"
// CHECK:STDOUT: %.loc15_5.1: ref i32 = tuple_access %t1, member0
// CHECK:STDOUT: %.loc15_5.2: i32 = bind_value %.loc15_5.1
// CHECK:STDOUT: %.loc15_5.3: i32 = int_literal 0
// CHECK:STDOUT: %.loc15_5.4: ref i32 = array_index %b, %.loc15_5.3
// CHECK:STDOUT: %.loc15_5.5: init i32 = initialize_from %.loc15_5.2 to %.loc15_5.4
// CHECK:STDOUT: %.loc15_5.6: ref String = tuple_access %t1, member1
// CHECK:STDOUT: %t1.ref: ref (i32, String, String) = name_reference "t1", %t1
// CHECK:STDOUT: %.loc16_19.1: ref i32 = tuple_access %t1.ref, member0
// CHECK:STDOUT: %.loc16_19.2: i32 = bind_value %.loc16_19.1
// CHECK:STDOUT: %.loc16_19.3: i32 = int_literal 0
// CHECK:STDOUT: %.loc16_19.4: ref i32 = array_index %b, %.loc16_19.3
// CHECK:STDOUT: %.loc16_19.5: init i32 = initialize_from %.loc16_19.2 to %.loc16_19.4
// CHECK:STDOUT: %.loc16_19.6: ref String = tuple_access %t1.ref, member1
// CHECK:STDOUT: assign %b, <error>
// CHECK:STDOUT: %.loc21_14: i32 = int_literal 3
// CHECK:STDOUT: %.loc21_15: type = array_type %.loc21_14, i32
@@ -60,11 +61,12 @@ var d: [i32; 3] = t2;
// CHECK:STDOUT: %.loc21_24.1: type = tuple_type (i32, i32)
// CHECK:STDOUT: %.loc21_24.2: (i32, i32) = tuple_literal (%.loc21_20, %.loc21_23)
// CHECK:STDOUT: assign %c, <error>
// CHECK:STDOUT: %.loc26_18.1: type = tuple_type (type, type)
// CHECK:STDOUT: %.loc26_18.2: (type, type) = tuple_literal (i32, i32)
// CHECK:STDOUT: %.loc23_18.1: type = tuple_type (type, type)
// CHECK:STDOUT: %.loc23_18.2: (type, type) = tuple_literal (i32, i32)
// CHECK:STDOUT: %t2: ref (i32, i32) = var "t2"
// CHECK:STDOUT: %.loc27_14: i32 = int_literal 3
// CHECK:STDOUT: %.loc27_15: type = array_type %.loc27_14, i32
// CHECK:STDOUT: %d: ref [i32; 3] = var "d"
// CHECK:STDOUT: %t2.ref: ref (i32, i32) = name_reference "t2", %t2
// CHECK:STDOUT: assign %d, <error>
// CHECK:STDOUT: }
+5 -2
View File
@@ -19,14 +19,17 @@ fn G() -> i32 {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%arr: [i32; 3], %i: i32) -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc8_15.1: ref [i32; 3] = value_as_reference %arr
// CHECK:STDOUT: %.loc8_15.2: ref i32 = array_index %.loc8_15.1, %i
// CHECK:STDOUT: %arr.ref: [i32; 3] = name_reference "arr", %arr
// CHECK:STDOUT: %i.ref: i32 = name_reference "i", %i
// CHECK:STDOUT: %.loc8_15.1: ref [i32; 3] = value_as_reference %arr.ref
// CHECK:STDOUT: %.loc8_15.2: ref i32 = array_index %.loc8_15.1, %i.ref
// CHECK:STDOUT: %.loc8_15.3: i32 = bind_value %.loc8_15.2
// CHECK:STDOUT: return %.loc8_15.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @G() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %F.ref = name_reference_untyped "F", package.%F
// CHECK:STDOUT: %.loc12_13: i32 = int_literal 1
// CHECK:STDOUT: %.loc12_16: i32 = int_literal 2
// CHECK:STDOUT: %.loc12_19: i32 = int_literal 3