Files
carbon-lang/toolchain/check/testdata/builtins/pointer/make_null.carbon
T
Chandler Carruth 8be274cf60 Replace :! binding syntax with phase keywords and contextual defaults (#7479)
Implement the toolchain side of proposal #7254, removing the `:!`
binding
syntax for generic and template parameters in favor of the keywords
`generic`,
`template`, and `runtime` plus contextual defaults for phase.

For valid programs this is semantics-preserving: each binding resolves
to the
same phase, and produces the same SemIR, as it did under `:!`/`:`. The
parser
derives a binding's phase from its syntactic context plus any explicit
phase
keyword; new diagnostics and error recovery for misused keywords are
described
below.

Implementation details for each component:

- Lexer: remove the `:!` (`ColonExclaim`) token, move its virtual
parse-node
  budget onto `:`, and add the `generic` and `runtime` keywords.
- Parser: thread a `BindingContext` (`ExplicitParam`, `DeducedParam`, or
`CompileTimeEntityParam`) from declaration introducers down through
parameter
lists to each binding pattern, using a one-token lookahead to
distinguish a
name-qualifier parameter list from a declaration's own final list.
Parameters
of a compile-time entity (`class`, `interface`, `constraint`, `choice`,
`alias`, `export`, `namespace`) and deduced `[]` parameters default to
checked
generic; explicit function parameters and local bindings default to
runtime.
`HandleBindingPattern` resolves the phase from that context plus the
keyword: a
`generic` keyword needs no node of its own (the phase is carried by the
  binding's node kind), while a `runtime` keyword is preserved as a
`RuntimeBindingName` node so `check` can name it in a diagnostic. A
phase
keyword that is merely redundant with the contextual default is
diagnosed
  here, without invalidating the parse tree.
- Check: a phase keyword that is invalid for its context (for example
`runtime`
on a checked-generic parameter) is diagnosed here, and recovers by
building an
error binding that still introduces the name so that later uses of it do
not
  produce cascading errors.

The removed `:!` syntax is now rejected as an ordinary parse error.

The `form`/`:?`/`->?` ("extended types") portion of proposal #7254 is
left for a
separate change.

Assisted-by: Claude Code
2026-07-11 01:22:44 +00:00

201 lines
12 KiB
Plaintext

// 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(generic 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: %empty_struct: %empty_struct_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.6b6: type = ptr_type %C [concrete]
// CHECK:STDOUT: %.9e8: type = maybe_unformed_type %ptr.6b6 [concrete]
// CHECK:STDOUT: %pattern_type.e22: type = pattern_type %.9e8 [concrete]
// CHECK:STDOUT: %MakeNullC.type: type = fn_type @MakeNullC [concrete]
// CHECK:STDOUT: %MakeNullC: %MakeNullC.type = struct_value () [concrete]
// CHECK:STDOUT: %s.patt: %pattern_type.b42 = value_binding_pattern s [concrete]
// CHECK:STDOUT: %c.patt: %pattern_type.e22 = value_binding_pattern c [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %.loc11_48.1: %.b2d = value_of_initializer @__global_init.%MakeNullEmptyStruct.call
// CHECK:STDOUT: %.loc11_48.2: %.b2d = converted @__global_init.%MakeNullEmptyStruct.call, %.loc11_48.1
// 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 () [concrete = constants.%empty_struct]
// 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: %s: %.b2d = wrapper_binding s, %.loc11_48.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %s.patt: %pattern_type.b42 = value_binding_pattern s [concrete = constants.%s.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc12_37.1: %.9e8 = value_of_initializer @__global_init.%MakeNullC.call
// CHECK:STDOUT: %.loc12_37.2: %.9e8 = converted @__global_init.%MakeNullC.call, %.loc12_37.1
// CHECK:STDOUT: %.loc12_23.1: type = splice_block %.loc12_23.3 [concrete = constants.%.9e8] {
// 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.6b6]
// CHECK:STDOUT: %MakeUnformed.call.loc12: init type = call %MakeUnformed.ref.loc12(%ptr.loc12) [concrete = constants.%.9e8]
// CHECK:STDOUT: %.loc12_23.2: type = value_of_initializer %MakeUnformed.call.loc12 [concrete = constants.%.9e8]
// CHECK:STDOUT: %.loc12_23.3: type = converted %MakeUnformed.call.loc12, %.loc12_23.2 [concrete = constants.%.9e8]
// CHECK:STDOUT: }
// CHECK:STDOUT: %c: %.9e8 = wrapper_binding c, %.loc12_37.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %c.patt: %pattern_type.e22 = value_binding_pattern c [concrete = constants.%c.patt]
// CHECK:STDOUT: }
// 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: %MakeNullEmptyStruct.call: init %.b2d = call %MakeNullEmptyStruct.ref()
// CHECK:STDOUT: %MakeNullC.ref: %MakeNullC.type = name_ref MakeNullC, file.%MakeNullC.decl [concrete = constants.%MakeNullC]
// CHECK:STDOUT: %MakeNullC.call: init %.9e8 = call %MakeNullC.ref()
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !observes:
// 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: %empty_struct: %empty_struct_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: %s.patt: %pattern_type.b42 = value_binding_pattern s [concrete]
// CHECK:STDOUT: %MakeNull.specific_fn.391: <specific function> = specific_function %MakeNull, @MakeNull(%empty_struct_type) [concrete]
// CHECK:STDOUT: %ptr.6b6: type = ptr_type %C [concrete]
// CHECK:STDOUT: %.9e8: type = maybe_unformed_type %ptr.6b6 [concrete]
// CHECK:STDOUT: %pattern_type.e22: type = pattern_type %.9e8 [concrete]
// CHECK:STDOUT: %c.patt: %pattern_type.e22 = value_binding_pattern c [concrete]
// CHECK:STDOUT: %MakeNull.specific_fn.75f: <specific function> = specific_function %MakeNull, @MakeNull(%C) [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %.loc10_39.1: %.b2d = value_of_initializer @__global_init.%MakeNull.call.loc10
// CHECK:STDOUT: %.loc10_39.2: %.b2d = converted @__global_init.%MakeNull.call.loc10, %.loc10_39.1
// 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 () [concrete = constants.%empty_struct]
// 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: %s: %.b2d = wrapper_binding s, %.loc10_39.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %s.patt: %pattern_type.b42 = value_binding_pattern s [concrete = constants.%s.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc11_37.1: %.9e8 = value_of_initializer @__global_init.%MakeNull.call.loc11
// CHECK:STDOUT: %.loc11_37.2: %.9e8 = converted @__global_init.%MakeNull.call.loc11, %.loc11_37.1
// CHECK:STDOUT: %.loc11_23.1: type = splice_block %.loc11_23.3 [concrete = constants.%.9e8] {
// 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.6b6]
// CHECK:STDOUT: %MakeUnformed.call.loc11: init type = call %MakeUnformed.ref.loc11(%ptr.loc11) [concrete = constants.%.9e8]
// CHECK:STDOUT: %.loc11_23.2: type = value_of_initializer %MakeUnformed.call.loc11 [concrete = constants.%.9e8]
// CHECK:STDOUT: %.loc11_23.3: type = converted %MakeUnformed.call.loc11, %.loc11_23.2 [concrete = constants.%.9e8]
// CHECK:STDOUT: }
// CHECK:STDOUT: %c: %.9e8 = wrapper_binding c, %.loc11_37.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %c.patt: %pattern_type.e22 = value_binding_pattern c [concrete = constants.%c.patt]
// CHECK:STDOUT: }
// 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 () [concrete = constants.%empty_struct]
// CHECK:STDOUT: %.loc10_39: 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.391]
// CHECK:STDOUT: %MakeNull.call.loc10: init %.b2d = call %MakeNull.specific_fn.loc10()
// 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.75f]
// CHECK:STDOUT: %MakeNull.call.loc11: init %.9e8 = call %MakeNull.specific_fn.loc11()
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !observes:
// CHECK:STDOUT: }
// CHECK:STDOUT: