Add a type_literal instruction to represent syntactic type literals. (#6781)

This allows us to capture the location at which a type literal was used,
even in the cases where we don't otherwise need to create a new
instruction to represent the type such as for `char` or `str`.

The logic used to build the underlying type is now marked as desugaring.
For cases such as `iN`, this causes the call to `Core.Int` to no longer
be added as a dedicated IR instruction, and instead its constant value
is used directly as the value of the `type_literal`. This results in
this being on balance a reduction in the size of the IR.

This also fixes a crash in C++ interop when using a `char` literal as a
template argument. The crash was caused by the template argument not
having an associated location when mapping to a C++ location. See
changes to check/testdata/interop/cpp/template/type_param.carbon for an
example that used to crash before this change.

Update alias handling to allow an alias to point at any type literal,
reinstating support for aliases for type literals such as `bool` and
`i32` that had previously worked but stopped working when we
transitioned those types to being defined in the prelude. See changes to
toolchain/check/testdata/alias/builtins.carbon.

All the test changes other than the two mentioned above are mechanical
autoupdate changes switching to the new instruction.
This commit is contained in:
Richard Smith
2026-02-26 20:10:50 +00:00
committed by GitHub
parent 96f163f114
commit f9ab963bd6
337 changed files with 5372 additions and 6077 deletions
@@ -128,7 +128,8 @@ fn StarstWith(a: str, b: str) -> bool {
// CHECK:STDOUT: %return.patt: %pattern_type.461 = return_slot_pattern [concrete]
// CHECK:STDOUT: %return.param_patt: %pattern_type.461 = out_param_pattern %return.patt, call_param0 [concrete]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc13_11.2: Core.Form = init_form constants.%str.ee0, call_param0 [concrete = constants.%.2ff]
// CHECK:STDOUT: %str: type = type_literal constants.%str.ee0 [concrete = constants.%str.ee0]
// CHECK:STDOUT: %.loc13_11.2: Core.Form = init_form %str, call_param0 [concrete = constants.%.2ff]
// CHECK:STDOUT: %return.param: ref %str.ee0 = out_param call_param0
// CHECK:STDOUT: %return: ref %str.ee0 = return_slot %return.param
// CHECK:STDOUT: }
@@ -164,8 +165,6 @@ fn StarstWith(a: str, b: str) -> bool {
// CHECK:STDOUT: %str: type = class_type @String [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %pattern_type.461: type = pattern_type %str [concrete]
// CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
// CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
// CHECK:STDOUT: %.d29: Core.Form = init_form bool, call_param2 [concrete]
// CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
// CHECK:STDOUT: %StarstWith.type: type = fn_type @StarstWith [concrete]
@@ -202,13 +201,13 @@ fn StarstWith(a: str, b: str) -> bool {
// CHECK:STDOUT: %return.patt: %pattern_type.831 = return_slot_pattern [concrete]
// CHECK:STDOUT: %return.param_patt: %pattern_type.831 = out_param_pattern %return.patt, call_param2 [concrete]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %Bool.call: init type = call constants.%Bool() [concrete = bool]
// CHECK:STDOUT: %.loc7_34.1: type = value_of_initializer %Bool.call [concrete = bool]
// CHECK:STDOUT: %.loc7_34.2: type = converted %Bool.call, %.loc7_34.1 [concrete = bool]
// CHECK:STDOUT: %.loc7_34.3: Core.Form = init_form %.loc7_34.2, call_param2 [concrete = constants.%.d29]
// CHECK:STDOUT: %.loc7_34.1: type = type_literal bool [concrete = bool]
// CHECK:STDOUT: %.loc7_34.2: Core.Form = init_form %.loc7_34.1, call_param2 [concrete = constants.%.d29]
// CHECK:STDOUT: %a.param: %str = value_param call_param0
// CHECK:STDOUT: %str.loc7_18: type = type_literal constants.%str [concrete = constants.%str]
// CHECK:STDOUT: %a: %str = value_binding a, %a.param
// CHECK:STDOUT: %b.param: %str = value_param call_param1
// CHECK:STDOUT: %str.loc7_26: type = type_literal constants.%str [concrete = constants.%str]
// CHECK:STDOUT: %b: %str = value_binding b, %b.param
// CHECK:STDOUT: %return.param: ref bool = out_param call_param2
// CHECK:STDOUT: %return: ref bool = return_slot %return.param