浏览代码

Add builtins to form and detect null `MaybeUnformed(T*)` values. (#6208)

In preparation for modeling `Optional(T*)` as a null pointer value.

With this PR, pointers remain non-nullable, but `MaybeUnformed(T*)` has
a particular unformed state that has the same representation as a C++
null pointer, which is accessible and detectable via builtins.
Richard Smith 6 月之前
父节点
当前提交
90771414f5

+ 3 - 1
toolchain/check/eval.cpp

@@ -1693,7 +1693,9 @@ static auto MakeConstantForBuiltinCall(EvalContext& eval_context,
     case SemIR::BuiltinFunctionKind::IntOrAssign:
     case SemIR::BuiltinFunctionKind::IntXorAssign:
     case SemIR::BuiltinFunctionKind::IntLeftShiftAssign:
-    case SemIR::BuiltinFunctionKind::IntRightShiftAssign: {
+    case SemIR::BuiltinFunctionKind::IntRightShiftAssign:
+    case SemIR::BuiltinFunctionKind::PointerMakeNull:
+    case SemIR::BuiltinFunctionKind::PointerIsNull: {
       // These are runtime-only builtins.
       // TODO: Consider tracking this on the `BuiltinFunctionKind`.
       return SemIR::ConstantId::NotConstant;

+ 275 - 0
toolchain/check/testdata/builtins/pointer/is_null.carbon

@@ -0,0 +1,275 @@
+// 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
+//
+// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/bool.carbon
+//
+// AUTOUPDATE
+// TIP: To test this file alone, run:
+// TIP:   bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/pointer/is_null.carbon
+// TIP: To dump output, run:
+// TIP:   bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/pointer/is_null.carbon
+
+// --- call_exact.carbon
+
+library "[[@TEST_NAME]]";
+
+class C {}
+
+fn MakeUnformed(t: type) -> type = "maybe_unformed.make_type";
+fn IsNullEmptyStruct(p: MakeUnformed({}*)) -> bool = "pointer.is_null";
+fn IsNullC(p: MakeUnformed(C*)) -> bool = "pointer.is_null";
+
+//@dump-sem-ir-begin
+fn TestEmptyStruct(s: MakeUnformed({}*)) -> bool {
+  return IsNullEmptyStruct(s);
+}
+
+fn TestC(c: MakeUnformed(C*)) -> bool {
+  return IsNullC(c);
+}
+//@dump-sem-ir-end
+
+// --- call_generic.carbon
+
+library "[[@TEST_NAME]]";
+
+fn MakeUnformed(t: type) -> type = "maybe_unformed.make_type";
+fn IsNull[T:! type](p: MakeUnformed(T*)) -> bool = "pointer.is_null";
+
+class C {}
+
+//@dump-sem-ir-begin
+fn TestEmptyStruct(s: MakeUnformed({}*)) -> bool {
+  return IsNull(s);
+}
+
+fn TestC(c: MakeUnformed(C*)) -> bool {
+  return IsNull(c);
+}
+//@dump-sem-ir-end
+
+// --- fail_bad_decl.carbon
+
+library "[[@TEST_NAME]]";
+
+fn MakeUnformed(t: type) -> type = "maybe_unformed.make_type";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.is_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn NoParam() -> bool = "pointer.is_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn NoParam() -> bool = "pointer.is_null";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.is_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn NoRetType(p: MakeUnformed({}*)) = "pointer.is_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn NoRetType(p: MakeUnformed({}*)) = "pointer.is_null";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.is_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn WrongRetType(p: MakeUnformed({}*)) -> {} = "pointer.is_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn WrongRetType(p: MakeUnformed({}*)) -> {} = "pointer.is_null";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.is_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn NoUnformed(p: {}*) -> bool = "pointer.is_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn NoUnformed(p: {}*) -> bool = "pointer.is_null";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.is_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn NotPointer(p: MakeUnformed({})) -> bool = "pointer.is_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn NotPointer(p: MakeUnformed({})) -> bool = "pointer.is_null";
+
+// CHECK:STDOUT: --- call_exact.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %C: type = class_type @C [concrete]
+// CHECK:STDOUT:   %empty_struct_type: type = struct_type {} [concrete]
+// CHECK:STDOUT:   %MakeUnformed.type: type = fn_type @MakeUnformed [concrete]
+// CHECK:STDOUT:   %MakeUnformed: %MakeUnformed.type = struct_value () [concrete]
+// CHECK:STDOUT:   %ptr.c28: type = ptr_type %empty_struct_type [concrete]
+// CHECK:STDOUT:   %.b2d: type = maybe_unformed_type %ptr.c28 [concrete]
+// CHECK:STDOUT:   %pattern_type.b42: type = pattern_type %.b2d [concrete]
+// CHECK:STDOUT:   %Bool.type: type = fn_type @Bool [concrete]
+// CHECK:STDOUT:   %Bool: %Bool.type = struct_value () [concrete]
+// CHECK:STDOUT:   %pattern_type.831: type = pattern_type bool [concrete]
+// CHECK:STDOUT:   %IsNullEmptyStruct.type: type = fn_type @IsNullEmptyStruct [concrete]
+// CHECK:STDOUT:   %IsNullEmptyStruct: %IsNullEmptyStruct.type = struct_value () [concrete]
+// CHECK:STDOUT:   %ptr.019: type = ptr_type %C [concrete]
+// CHECK:STDOUT:   %.273: type = maybe_unformed_type %ptr.019 [concrete]
+// CHECK:STDOUT:   %pattern_type.ad6: type = pattern_type %.273 [concrete]
+// CHECK:STDOUT:   %IsNullC.type: type = fn_type @IsNullC [concrete]
+// CHECK:STDOUT:   %IsNullC: %IsNullC.type = struct_value () [concrete]
+// CHECK:STDOUT:   %TestEmptyStruct.type: type = fn_type @TestEmptyStruct [concrete]
+// CHECK:STDOUT:   %TestEmptyStruct: %TestEmptyStruct.type = struct_value () [concrete]
+// CHECK:STDOUT:   %TestC.type: type = fn_type @TestC [concrete]
+// CHECK:STDOUT:   %TestC: %TestC.type = struct_value () [concrete]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: imports {
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   %TestEmptyStruct.decl: %TestEmptyStruct.type = fn_decl @TestEmptyStruct [concrete = constants.%TestEmptyStruct] {
+// CHECK:STDOUT:     %s.patt: %pattern_type.b42 = binding_pattern s [concrete]
+// CHECK:STDOUT:     %s.param_patt: %pattern_type.b42 = value_param_pattern %s.patt, call_param0 [concrete]
+// 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_param1 [concrete]
+// CHECK:STDOUT:   } {
+// CHECK:STDOUT:     %Bool.call: init type = call constants.%Bool() [concrete = bool]
+// CHECK:STDOUT:     %.loc11_45.1: type = value_of_initializer %Bool.call [concrete = bool]
+// CHECK:STDOUT:     %.loc11_45.2: type = converted %Bool.call, %.loc11_45.1 [concrete = bool]
+// CHECK:STDOUT:     %s.param: %.b2d = value_param call_param0
+// CHECK:STDOUT:     %.loc11_39.1: type = splice_block %.loc11_39.3 [concrete = constants.%.b2d] {
+// CHECK:STDOUT:       %MakeUnformed.ref: %MakeUnformed.type = name_ref MakeUnformed, file.%MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:       %.loc11_37: %empty_struct_type = struct_literal ()
+// CHECK:STDOUT:       %.loc11_38: type = converted %.loc11_37, constants.%empty_struct_type [concrete = constants.%empty_struct_type]
+// CHECK:STDOUT:       %ptr: type = ptr_type %.loc11_38 [concrete = constants.%ptr.c28]
+// CHECK:STDOUT:       %MakeUnformed.call: init type = call %MakeUnformed.ref(%ptr) [concrete = constants.%.b2d]
+// CHECK:STDOUT:       %.loc11_39.2: type = value_of_initializer %MakeUnformed.call [concrete = constants.%.b2d]
+// CHECK:STDOUT:       %.loc11_39.3: type = converted %MakeUnformed.call, %.loc11_39.2 [concrete = constants.%.b2d]
+// CHECK:STDOUT:     }
+// CHECK:STDOUT:     %s: %.b2d = bind_name s, %s.param
+// CHECK:STDOUT:     %return.param: ref bool = out_param call_param1
+// CHECK:STDOUT:     %return: ref bool = return_slot %return.param
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %TestC.decl: %TestC.type = fn_decl @TestC [concrete = constants.%TestC] {
+// CHECK:STDOUT:     %c.patt: %pattern_type.ad6 = binding_pattern c [concrete]
+// CHECK:STDOUT:     %c.param_patt: %pattern_type.ad6 = value_param_pattern %c.patt, call_param0 [concrete]
+// 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_param1 [concrete]
+// CHECK:STDOUT:   } {
+// CHECK:STDOUT:     %Bool.call: init type = call constants.%Bool() [concrete = bool]
+// CHECK:STDOUT:     %.loc15_34.1: type = value_of_initializer %Bool.call [concrete = bool]
+// CHECK:STDOUT:     %.loc15_34.2: type = converted %Bool.call, %.loc15_34.1 [concrete = bool]
+// CHECK:STDOUT:     %c.param: %.273 = value_param call_param0
+// CHECK:STDOUT:     %.loc15_28.1: type = splice_block %.loc15_28.3 [concrete = constants.%.273] {
+// CHECK:STDOUT:       %MakeUnformed.ref: %MakeUnformed.type = name_ref MakeUnformed, file.%MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:       %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
+// CHECK:STDOUT:       %ptr: type = ptr_type %C.ref [concrete = constants.%ptr.019]
+// CHECK:STDOUT:       %MakeUnformed.call: init type = call %MakeUnformed.ref(%ptr) [concrete = constants.%.273]
+// CHECK:STDOUT:       %.loc15_28.2: type = value_of_initializer %MakeUnformed.call [concrete = constants.%.273]
+// CHECK:STDOUT:       %.loc15_28.3: type = converted %MakeUnformed.call, %.loc15_28.2 [concrete = constants.%.273]
+// CHECK:STDOUT:     }
+// CHECK:STDOUT:     %c: %.273 = bind_name c, %c.param
+// CHECK:STDOUT:     %return.param: ref bool = out_param call_param1
+// CHECK:STDOUT:     %return: ref bool = return_slot %return.param
+// CHECK:STDOUT:   }
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @TestEmptyStruct(%s.param: %.b2d) -> bool {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %IsNullEmptyStruct.ref: %IsNullEmptyStruct.type = name_ref IsNullEmptyStruct, file.%IsNullEmptyStruct.decl [concrete = constants.%IsNullEmptyStruct]
+// CHECK:STDOUT:   %s.ref: %.b2d = name_ref s, %s
+// CHECK:STDOUT:   %IsNullEmptyStruct.call: init bool = call %IsNullEmptyStruct.ref(%s.ref)
+// CHECK:STDOUT:   return %IsNullEmptyStruct.call to %return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @TestC(%c.param: %.273) -> bool {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %IsNullC.ref: %IsNullC.type = name_ref IsNullC, file.%IsNullC.decl [concrete = constants.%IsNullC]
+// CHECK:STDOUT:   %c.ref: %.273 = name_ref c, %c
+// CHECK:STDOUT:   %IsNullC.call: init bool = call %IsNullC.ref(%c.ref)
+// CHECK:STDOUT:   return %IsNullC.call to %return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: --- call_generic.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %MakeUnformed.type: type = fn_type @MakeUnformed [concrete]
+// CHECK:STDOUT:   %MakeUnformed: %MakeUnformed.type = struct_value () [concrete]
+// CHECK:STDOUT:   %Bool.type: type = fn_type @Bool [concrete]
+// CHECK:STDOUT:   %Bool: %Bool.type = struct_value () [concrete]
+// CHECK:STDOUT:   %pattern_type.831: type = pattern_type bool [concrete]
+// CHECK:STDOUT:   %IsNull.type: type = fn_type @IsNull [concrete]
+// CHECK:STDOUT:   %IsNull: %IsNull.type = struct_value () [concrete]
+// CHECK:STDOUT:   %C: type = class_type @C [concrete]
+// CHECK:STDOUT:   %empty_struct_type: type = struct_type {} [concrete]
+// CHECK:STDOUT:   %ptr.c28: type = ptr_type %empty_struct_type [concrete]
+// CHECK:STDOUT:   %.b2d: type = maybe_unformed_type %ptr.c28 [concrete]
+// CHECK:STDOUT:   %pattern_type.b42: type = pattern_type %.b2d [concrete]
+// CHECK:STDOUT:   %TestEmptyStruct.type: type = fn_type @TestEmptyStruct [concrete]
+// CHECK:STDOUT:   %TestEmptyStruct: %TestEmptyStruct.type = struct_value () [concrete]
+// CHECK:STDOUT:   %IsNull.specific_fn.c98: <specific function> = specific_function %IsNull, @IsNull(%empty_struct_type) [concrete]
+// CHECK:STDOUT:   %ptr.019: type = ptr_type %C [concrete]
+// CHECK:STDOUT:   %.273: type = maybe_unformed_type %ptr.019 [concrete]
+// CHECK:STDOUT:   %pattern_type.ad6: type = pattern_type %.273 [concrete]
+// CHECK:STDOUT:   %TestC.type: type = fn_type @TestC [concrete]
+// CHECK:STDOUT:   %TestC: %TestC.type = struct_value () [concrete]
+// CHECK:STDOUT:   %IsNull.specific_fn.87c: <specific function> = specific_function %IsNull, @IsNull(%C) [concrete]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: imports {
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   %TestEmptyStruct.decl: %TestEmptyStruct.type = fn_decl @TestEmptyStruct [concrete = constants.%TestEmptyStruct] {
+// CHECK:STDOUT:     %s.patt: %pattern_type.b42 = binding_pattern s [concrete]
+// CHECK:STDOUT:     %s.param_patt: %pattern_type.b42 = value_param_pattern %s.patt, call_param0 [concrete]
+// 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_param1 [concrete]
+// CHECK:STDOUT:   } {
+// CHECK:STDOUT:     %Bool.call: init type = call constants.%Bool() [concrete = bool]
+// CHECK:STDOUT:     %.loc10_45.1: type = value_of_initializer %Bool.call [concrete = bool]
+// CHECK:STDOUT:     %.loc10_45.2: type = converted %Bool.call, %.loc10_45.1 [concrete = bool]
+// CHECK:STDOUT:     %s.param: %.b2d = value_param call_param0
+// CHECK:STDOUT:     %.loc10_39.1: type = splice_block %.loc10_39.3 [concrete = constants.%.b2d] {
+// CHECK:STDOUT:       %MakeUnformed.ref: %MakeUnformed.type = name_ref MakeUnformed, file.%MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:       %.loc10_37: %empty_struct_type = struct_literal ()
+// CHECK:STDOUT:       %.loc10_38: type = converted %.loc10_37, constants.%empty_struct_type [concrete = constants.%empty_struct_type]
+// CHECK:STDOUT:       %ptr: type = ptr_type %.loc10_38 [concrete = constants.%ptr.c28]
+// CHECK:STDOUT:       %MakeUnformed.call: init type = call %MakeUnformed.ref(%ptr) [concrete = constants.%.b2d]
+// CHECK:STDOUT:       %.loc10_39.2: type = value_of_initializer %MakeUnformed.call [concrete = constants.%.b2d]
+// CHECK:STDOUT:       %.loc10_39.3: type = converted %MakeUnformed.call, %.loc10_39.2 [concrete = constants.%.b2d]
+// CHECK:STDOUT:     }
+// CHECK:STDOUT:     %s: %.b2d = bind_name s, %s.param
+// CHECK:STDOUT:     %return.param: ref bool = out_param call_param1
+// CHECK:STDOUT:     %return: ref bool = return_slot %return.param
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %TestC.decl: %TestC.type = fn_decl @TestC [concrete = constants.%TestC] {
+// CHECK:STDOUT:     %c.patt: %pattern_type.ad6 = binding_pattern c [concrete]
+// CHECK:STDOUT:     %c.param_patt: %pattern_type.ad6 = value_param_pattern %c.patt, call_param0 [concrete]
+// 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_param1 [concrete]
+// CHECK:STDOUT:   } {
+// CHECK:STDOUT:     %Bool.call: init type = call constants.%Bool() [concrete = bool]
+// CHECK:STDOUT:     %.loc14_34.1: type = value_of_initializer %Bool.call [concrete = bool]
+// CHECK:STDOUT:     %.loc14_34.2: type = converted %Bool.call, %.loc14_34.1 [concrete = bool]
+// CHECK:STDOUT:     %c.param: %.273 = value_param call_param0
+// CHECK:STDOUT:     %.loc14_28.1: type = splice_block %.loc14_28.3 [concrete = constants.%.273] {
+// CHECK:STDOUT:       %MakeUnformed.ref: %MakeUnformed.type = name_ref MakeUnformed, file.%MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:       %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
+// CHECK:STDOUT:       %ptr: type = ptr_type %C.ref [concrete = constants.%ptr.019]
+// CHECK:STDOUT:       %MakeUnformed.call: init type = call %MakeUnformed.ref(%ptr) [concrete = constants.%.273]
+// CHECK:STDOUT:       %.loc14_28.2: type = value_of_initializer %MakeUnformed.call [concrete = constants.%.273]
+// CHECK:STDOUT:       %.loc14_28.3: type = converted %MakeUnformed.call, %.loc14_28.2 [concrete = constants.%.273]
+// CHECK:STDOUT:     }
+// CHECK:STDOUT:     %c: %.273 = bind_name c, %c.param
+// CHECK:STDOUT:     %return.param: ref bool = out_param call_param1
+// CHECK:STDOUT:     %return: ref bool = return_slot %return.param
+// CHECK:STDOUT:   }
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @TestEmptyStruct(%s.param: %.b2d) -> bool {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %IsNull.ref: %IsNull.type = name_ref IsNull, file.%IsNull.decl [concrete = constants.%IsNull]
+// CHECK:STDOUT:   %s.ref: %.b2d = name_ref s, %s
+// CHECK:STDOUT:   %IsNull.specific_fn: <specific function> = specific_function %IsNull.ref, @IsNull(constants.%empty_struct_type) [concrete = constants.%IsNull.specific_fn.c98]
+// CHECK:STDOUT:   %IsNull.call: init bool = call %IsNull.specific_fn(%s.ref)
+// CHECK:STDOUT:   return %IsNull.call to %return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @TestC(%c.param: %.273) -> bool {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %IsNull.ref: %IsNull.type = name_ref IsNull, file.%IsNull.decl [concrete = constants.%IsNull]
+// CHECK:STDOUT:   %c.ref: %.273 = name_ref c, %c
+// CHECK:STDOUT:   %IsNull.specific_fn: <specific function> = specific_function %IsNull.ref, @IsNull(constants.%C) [concrete = constants.%IsNull.specific_fn.87c]
+// CHECK:STDOUT:   %IsNull.call: init bool = call %IsNull.specific_fn(%c.ref)
+// CHECK:STDOUT:   return %IsNull.call to %return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 194 - 0
toolchain/check/testdata/builtins/pointer/make_null.carbon

@@ -0,0 +1,194 @@
+// 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
+//
+// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon
+//
+// AUTOUPDATE
+// TIP: To test this file alone, run:
+// TIP:   bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/pointer/make_null.carbon
+// TIP: To dump output, run:
+// TIP:   bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/pointer/make_null.carbon
+
+// --- call_exact.carbon
+
+library "[[@TEST_NAME]]";
+
+class C {}
+
+fn MakeUnformed(t: type) -> type = "maybe_unformed.make_type";
+fn MakeNullEmptyStruct() -> MakeUnformed({}*) = "pointer.make_null";
+fn MakeNullC() -> MakeUnformed(C*) = "pointer.make_null";
+
+//@dump-sem-ir-begin
+let s: MakeUnformed({}*) = MakeNullEmptyStruct();
+let c: MakeUnformed(C*) = MakeNullC();
+//@dump-sem-ir-end
+
+// --- call_generic.carbon
+
+library "[[@TEST_NAME]]";
+
+fn MakeUnformed(t: type) -> type = "maybe_unformed.make_type";
+fn MakeNull(T:! type) -> MakeUnformed(T*) = "pointer.make_null";
+
+class C {}
+
+//@dump-sem-ir-begin
+let s: MakeUnformed({}*) = MakeNull({});
+let c: MakeUnformed(C*) = MakeNull(C);
+//@dump-sem-ir-end
+
+// --- fail_bad_decl.carbon
+
+library "[[@TEST_NAME]]";
+
+fn MakeUnformed(t: type) -> type = "maybe_unformed.make_type";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.make_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn NoRetType() = "pointer.make_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn NoRetType() = "pointer.make_null";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.make_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn NoUnformed() -> {}* = "pointer.make_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn NoUnformed() -> {}* = "pointer.make_null";
+
+// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "pointer.make_null" [InvalidBuiltinSignature]
+// CHECK:STDERR: fn NotPointer() -> MakeUnformed({}) = "pointer.make_null";
+// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// CHECK:STDERR:
+fn NotPointer() -> MakeUnformed({}) = "pointer.make_null";
+
+// CHECK:STDOUT: --- call_exact.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %C: type = class_type @C [concrete]
+// CHECK:STDOUT:   %empty_struct_type: type = struct_type {} [concrete]
+// CHECK:STDOUT:   %MakeUnformed.type: type = fn_type @MakeUnformed [concrete]
+// CHECK:STDOUT:   %MakeUnformed: %MakeUnformed.type = struct_value () [concrete]
+// CHECK:STDOUT:   %ptr.c28: type = ptr_type %empty_struct_type [concrete]
+// CHECK:STDOUT:   %.b2d: type = maybe_unformed_type %ptr.c28 [concrete]
+// CHECK:STDOUT:   %pattern_type.b42: type = pattern_type %.b2d [concrete]
+// CHECK:STDOUT:   %MakeNullEmptyStruct.type: type = fn_type @MakeNullEmptyStruct [concrete]
+// CHECK:STDOUT:   %MakeNullEmptyStruct: %MakeNullEmptyStruct.type = struct_value () [concrete]
+// CHECK:STDOUT:   %ptr.019: type = ptr_type %C [concrete]
+// CHECK:STDOUT:   %.273: type = maybe_unformed_type %ptr.019 [concrete]
+// CHECK:STDOUT:   %pattern_type.ad6: type = pattern_type %.273 [concrete]
+// CHECK:STDOUT:   %MakeNullC.type: type = fn_type @MakeNullC [concrete]
+// CHECK:STDOUT:   %MakeNullC: %MakeNullC.type = struct_value () [concrete]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   name_binding_decl {
+// CHECK:STDOUT:     %s.patt: %pattern_type.b42 = binding_pattern s [concrete]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc11_24.1: type = splice_block %.loc11_24.3 [concrete = constants.%.b2d] {
+// CHECK:STDOUT:     %MakeUnformed.ref.loc11: %MakeUnformed.type = name_ref MakeUnformed, %MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:     %.loc11_22: %empty_struct_type = struct_literal ()
+// CHECK:STDOUT:     %.loc11_23: type = converted %.loc11_22, constants.%empty_struct_type [concrete = constants.%empty_struct_type]
+// CHECK:STDOUT:     %ptr.loc11: type = ptr_type %.loc11_23 [concrete = constants.%ptr.c28]
+// CHECK:STDOUT:     %MakeUnformed.call.loc11: init type = call %MakeUnformed.ref.loc11(%ptr.loc11) [concrete = constants.%.b2d]
+// CHECK:STDOUT:     %.loc11_24.2: type = value_of_initializer %MakeUnformed.call.loc11 [concrete = constants.%.b2d]
+// CHECK:STDOUT:     %.loc11_24.3: type = converted %MakeUnformed.call.loc11, %.loc11_24.2 [concrete = constants.%.b2d]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc11_48.1: ref %.b2d = temporary @__global_init.%.loc11, @__global_init.%MakeNullEmptyStruct.call
+// CHECK:STDOUT:   %.loc11_48.2: %.b2d = bind_value %.loc11_48.1
+// CHECK:STDOUT:   %s: %.b2d = bind_name s, %.loc11_48.2
+// CHECK:STDOUT:   name_binding_decl {
+// CHECK:STDOUT:     %c.patt: %pattern_type.ad6 = binding_pattern c [concrete]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc12_23.1: type = splice_block %.loc12_23.3 [concrete = constants.%.273] {
+// CHECK:STDOUT:     %MakeUnformed.ref.loc12: %MakeUnformed.type = name_ref MakeUnformed, %MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:     %C.ref: type = name_ref C, %C.decl [concrete = constants.%C]
+// CHECK:STDOUT:     %ptr.loc12: type = ptr_type %C.ref [concrete = constants.%ptr.019]
+// CHECK:STDOUT:     %MakeUnformed.call.loc12: init type = call %MakeUnformed.ref.loc12(%ptr.loc12) [concrete = constants.%.273]
+// CHECK:STDOUT:     %.loc12_23.2: type = value_of_initializer %MakeUnformed.call.loc12 [concrete = constants.%.273]
+// CHECK:STDOUT:     %.loc12_23.3: type = converted %MakeUnformed.call.loc12, %.loc12_23.2 [concrete = constants.%.273]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc12_37.1: ref %.273 = temporary @__global_init.%.loc12, @__global_init.%MakeNullC.call
+// CHECK:STDOUT:   %.loc12_37.2: %.273 = bind_value %.loc12_37.1
+// CHECK:STDOUT:   %c: %.273 = bind_name c, %.loc12_37.2
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @__global_init() {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %MakeNullEmptyStruct.ref: %MakeNullEmptyStruct.type = name_ref MakeNullEmptyStruct, file.%MakeNullEmptyStruct.decl [concrete = constants.%MakeNullEmptyStruct]
+// CHECK:STDOUT:   %.loc11: ref %.b2d = temporary_storage
+// CHECK:STDOUT:   %MakeNullEmptyStruct.call: init %.b2d = call %MakeNullEmptyStruct.ref() to %.loc11
+// CHECK:STDOUT:   %MakeNullC.ref: %MakeNullC.type = name_ref MakeNullC, file.%MakeNullC.decl [concrete = constants.%MakeNullC]
+// CHECK:STDOUT:   %.loc12: ref %.273 = temporary_storage
+// CHECK:STDOUT:   %MakeNullC.call: init %.273 = call %MakeNullC.ref() to %.loc12
+// CHECK:STDOUT:   <elided>
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: --- call_generic.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %MakeUnformed.type: type = fn_type @MakeUnformed [concrete]
+// CHECK:STDOUT:   %MakeUnformed: %MakeUnformed.type = struct_value () [concrete]
+// CHECK:STDOUT:   %MakeNull.type: type = fn_type @MakeNull [concrete]
+// CHECK:STDOUT:   %MakeNull: %MakeNull.type = struct_value () [concrete]
+// CHECK:STDOUT:   %C: type = class_type @C [concrete]
+// CHECK:STDOUT:   %empty_struct_type: type = struct_type {} [concrete]
+// CHECK:STDOUT:   %ptr.c28: type = ptr_type %empty_struct_type [concrete]
+// CHECK:STDOUT:   %.b2d: type = maybe_unformed_type %ptr.c28 [concrete]
+// CHECK:STDOUT:   %pattern_type.b42: type = pattern_type %.b2d [concrete]
+// CHECK:STDOUT:   %MakeNull.specific_fn.7cb: <specific function> = specific_function %MakeNull, @MakeNull(%empty_struct_type) [concrete]
+// CHECK:STDOUT:   %ptr.019: type = ptr_type %C [concrete]
+// CHECK:STDOUT:   %.273: type = maybe_unformed_type %ptr.019 [concrete]
+// CHECK:STDOUT:   %pattern_type.ad6: type = pattern_type %.273 [concrete]
+// CHECK:STDOUT:   %MakeNull.specific_fn.22e: <specific function> = specific_function %MakeNull, @MakeNull(%C) [concrete]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   name_binding_decl {
+// CHECK:STDOUT:     %s.patt: %pattern_type.b42 = binding_pattern s [concrete]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc10_24.1: type = splice_block %.loc10_24.3 [concrete = constants.%.b2d] {
+// CHECK:STDOUT:     %MakeUnformed.ref.loc10: %MakeUnformed.type = name_ref MakeUnformed, %MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:     %.loc10_22: %empty_struct_type = struct_literal ()
+// CHECK:STDOUT:     %.loc10_23: type = converted %.loc10_22, constants.%empty_struct_type [concrete = constants.%empty_struct_type]
+// CHECK:STDOUT:     %ptr.loc10: type = ptr_type %.loc10_23 [concrete = constants.%ptr.c28]
+// CHECK:STDOUT:     %MakeUnformed.call.loc10: init type = call %MakeUnformed.ref.loc10(%ptr.loc10) [concrete = constants.%.b2d]
+// CHECK:STDOUT:     %.loc10_24.2: type = value_of_initializer %MakeUnformed.call.loc10 [concrete = constants.%.b2d]
+// CHECK:STDOUT:     %.loc10_24.3: type = converted %MakeUnformed.call.loc10, %.loc10_24.2 [concrete = constants.%.b2d]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc10_39.1: ref %.b2d = temporary @__global_init.%.loc10_39.2, @__global_init.%MakeNull.call.loc10
+// CHECK:STDOUT:   %.loc10_39.2: %.b2d = bind_value %.loc10_39.1
+// CHECK:STDOUT:   %s: %.b2d = bind_name s, %.loc10_39.2
+// CHECK:STDOUT:   name_binding_decl {
+// CHECK:STDOUT:     %c.patt: %pattern_type.ad6 = binding_pattern c [concrete]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc11_23.1: type = splice_block %.loc11_23.3 [concrete = constants.%.273] {
+// CHECK:STDOUT:     %MakeUnformed.ref.loc11: %MakeUnformed.type = name_ref MakeUnformed, %MakeUnformed.decl [concrete = constants.%MakeUnformed]
+// CHECK:STDOUT:     %C.ref: type = name_ref C, %C.decl [concrete = constants.%C]
+// CHECK:STDOUT:     %ptr.loc11: type = ptr_type %C.ref [concrete = constants.%ptr.019]
+// CHECK:STDOUT:     %MakeUnformed.call.loc11: init type = call %MakeUnformed.ref.loc11(%ptr.loc11) [concrete = constants.%.273]
+// CHECK:STDOUT:     %.loc11_23.2: type = value_of_initializer %MakeUnformed.call.loc11 [concrete = constants.%.273]
+// CHECK:STDOUT:     %.loc11_23.3: type = converted %MakeUnformed.call.loc11, %.loc11_23.2 [concrete = constants.%.273]
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %.loc11_37.1: ref %.273 = temporary @__global_init.%.loc11, @__global_init.%MakeNull.call.loc11
+// CHECK:STDOUT:   %.loc11_37.2: %.273 = bind_value %.loc11_37.1
+// CHECK:STDOUT:   %c: %.273 = bind_name c, %.loc11_37.2
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @__global_init() {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %MakeNull.ref.loc10: %MakeNull.type = name_ref MakeNull, file.%MakeNull.decl [concrete = constants.%MakeNull]
+// CHECK:STDOUT:   %.loc10_38: %empty_struct_type = struct_literal ()
+// CHECK:STDOUT:   %.loc10_39.1: type = converted %.loc10_38, constants.%empty_struct_type [concrete = constants.%empty_struct_type]
+// CHECK:STDOUT:   %MakeNull.specific_fn.loc10: <specific function> = specific_function %MakeNull.ref.loc10, @MakeNull(constants.%empty_struct_type) [concrete = constants.%MakeNull.specific_fn.7cb]
+// CHECK:STDOUT:   %.loc10_39.2: ref %.b2d = temporary_storage
+// CHECK:STDOUT:   %MakeNull.call.loc10: init %.b2d = call %MakeNull.specific_fn.loc10() to %.loc10_39.2
+// CHECK:STDOUT:   %MakeNull.ref.loc11: %MakeNull.type = name_ref MakeNull, file.%MakeNull.decl [concrete = constants.%MakeNull]
+// CHECK:STDOUT:   %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
+// CHECK:STDOUT:   %MakeNull.specific_fn.loc11: <specific function> = specific_function %MakeNull.ref.loc11, @MakeNull(constants.%C) [concrete = constants.%MakeNull.specific_fn.22e]
+// CHECK:STDOUT:   %.loc11: ref %.273 = temporary_storage
+// CHECK:STDOUT:   %MakeNull.call.loc11: init %.273 = call %MakeNull.specific_fn.loc11() to %.loc11
+// CHECK:STDOUT:   <elided>
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 20 - 0
toolchain/lower/handle_call.cpp

@@ -469,6 +469,26 @@ static auto HandleBuiltinCall(FunctionContext& context, SemIR::InstId inst_id,
       CARBON_FATAL("Missing constant value for call to comptime-only function");
     }
 
+    case SemIR::BuiltinFunctionKind::PointerMakeNull: {
+      // MaybeUnformed(T*) has an in-place initializing representation, so an
+      // out parameter will be passed.
+      context.builder().CreateStore(
+          llvm::ConstantPointerNull::get(
+              llvm::PointerType::get(context.llvm_context(),
+                                     /*AddressSpace=*/0)),
+          context.GetValue(arg_ids[0]));
+      context.SetLocal(inst_id,
+                       llvm::PoisonValue::get(context.GetTypeOfInst(inst_id)));
+      return;
+    }
+
+    case SemIR::BuiltinFunctionKind::PointerIsNull: {
+      auto* ptr = context.builder().CreateLoad(
+          context.GetTypeOfInst(arg_ids[0]), context.GetValue(arg_ids[0]));
+      context.SetLocal(inst_id, context.builder().CreateIsNull(ptr));
+      return;
+    }
+
     case SemIR::BuiltinFunctionKind::TypeDestroy:
       // TODO: Destroy aggregate members.
       return;

+ 57 - 0
toolchain/lower/testdata/builtins/pointer.carbon

@@ -0,0 +1,57 @@
+// 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
+//
+// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/bool.carbon
+//
+// AUTOUPDATE
+// TIP: To test this file alone, run:
+// TIP:   bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/builtins/pointer.carbon
+// TIP: To dump output, run:
+// TIP:   bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/builtins/pointer.carbon
+
+fn MakeUnformed(t: type) -> type = "maybe_unformed.make_type";
+fn MakeNull(T:! type) -> MakeUnformed(T*) = "pointer.make_null";
+fn IsNull[T:! type](p: MakeUnformed(T*)) -> bool = "pointer.is_null";
+
+class C {}
+
+fn Null() -> MakeUnformed(C*) {
+  return MakeNull(C);
+}
+
+fn Check(p: MakeUnformed(C*)) -> bool {
+  return IsNull(p);
+}
+
+// CHECK:STDOUT: ; ModuleID = 'pointer.carbon'
+// CHECK:STDOUT: source_filename = "pointer.carbon"
+// CHECK:STDOUT:
+// CHECK:STDOUT: define void @_CNull.Main(ptr sret(ptr) %return) !dbg !4 {
+// CHECK:STDOUT: entry:
+// CHECK:STDOUT:   store ptr null, ptr %return, align 8, !dbg !7
+// CHECK:STDOUT:   ret void, !dbg !8
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: define i1 @_CCheck.Main(ptr %p) !dbg !9 {
+// CHECK:STDOUT: entry:
+// CHECK:STDOUT:   %IsNull.call = load ptr, ptr %p, align 8, !dbg !10
+// CHECK:STDOUT:   %IsNull.call1 = icmp eq ptr %IsNull.call, null, !dbg !10
+// CHECK:STDOUT:   ret i1 %IsNull.call1, !dbg !11
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
+// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
+// CHECK:STDOUT:
+// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
+// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
+// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
+// CHECK:STDOUT: !3 = !DIFile(filename: "pointer.carbon", directory: "")
+// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Null", linkageName: "_CNull.Main", scope: null, file: !3, line: 19, type: !5, spFlags: DISPFlagDefinition, unit: !2)
+// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
+// CHECK:STDOUT: !6 = !{}
+// CHECK:STDOUT: !7 = !DILocation(line: 20, column: 10, scope: !4)
+// CHECK:STDOUT: !8 = !DILocation(line: 20, column: 3, scope: !4)
+// CHECK:STDOUT: !9 = distinct !DISubprogram(name: "Check", linkageName: "_CCheck.Main", scope: null, file: !3, line: 23, type: !5, spFlags: DISPFlagDefinition, unit: !2)
+// CHECK:STDOUT: !10 = !DILocation(line: 24, column: 10, scope: !9)
+// CHECK:STDOUT: !11 = !DILocation(line: 24, column: 3, scope: !9)

+ 28 - 0
toolchain/sem_ir/builtin_function_kind.cpp

@@ -80,6 +80,22 @@ struct PointerTo {
   }
 };
 
+// Constraint that a type is MaybeUnformed<T>.
+template <typename T>
+struct MaybeUnformed {
+  static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
+      -> bool {
+    auto maybe_unformed =
+        sem_ir.types().TryGetAs<SemIR::MaybeUnformedType>(type_id);
+    if (!maybe_unformed) {
+      return false;
+    }
+    return Check<T>(
+        sem_ir, state,
+        sem_ir.types().GetTypeIdForTypeInstId(maybe_unformed->inner_id));
+  }
+};
+
 // Constraint that a type is `()`, used as the return type of builtin functions
 // with no return value.
 struct NoReturn {
@@ -605,6 +621,18 @@ constexpr BuiltinInfo BoolEq = {"bool.eq",
 constexpr BuiltinInfo BoolNeq = {"bool.neq",
                                  ValidateSignature<auto(Bool, Bool)->Bool>};
 
+// "pointer.make_null": returns the representation of a null pointer value. This
+// is an unformed state for the pointer type.
+constexpr BuiltinInfo PointerMakeNull = {
+    "pointer.make_null",
+    ValidateSignature<auto()->MaybeUnformed<PointerTo<AnyType>>>};
+
+// "pointer.is_null": determines whether the given pointer representation is a
+// null pointer value.
+constexpr BuiltinInfo PointerIsNull = {
+    "pointer.is_null",
+    ValidateSignature<auto(MaybeUnformed<PointerTo<AnyType>>)->Bool>};
+
 // "type.and": facet type combination.
 constexpr BuiltinInfo TypeAnd = {"type.and",
                                  ValidateSignature<auto(Type, Type)->Type>};

+ 4 - 0
toolchain/sem_ir/builtin_function_kind.def

@@ -123,6 +123,10 @@ CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatGreaterEq)
 CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(BoolEq)
 CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(BoolNeq)
 
+// Pointers.
+CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(PointerMakeNull)
+CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(PointerIsNull)
+
 // Facet type combination.
 CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(TypeAnd)
 

+ 3 - 1
toolchain/sem_ir/type_iterator.h

@@ -40,7 +40,9 @@ class TypeIterator {
   // The iterator will visit things in the reverse order that they are added.
   auto Add(InstId inst_id) -> void {
     auto type_id = sem_ir_->insts().Get(inst_id).type_id();
-    CARBON_CHECK(sem_ir_->types().IsFacetType(type_id));
+    CARBON_CHECK(sem_ir_->types().IsFacetType(type_id),
+                 "Type {0} of type inst is not a facet type",
+                 sem_ir_->types().GetAsInst(type_id).kind());
     PushInstId(inst_id);
   }
 

+ 1 - 0
toolchain/sem_ir/typed_insts.h

@@ -1201,6 +1201,7 @@ struct MaybeUnformedType {
           .ir_name = "maybe_unformed_type",
           .is_type = InstIsType::Always,
           .constant_kind = InstConstantKind::WheneverPossible,
+          .deduce_through = true,
       });
 
   TypeId type_id;