Files
carbon-lang/toolchain/check/testdata/generic/call_basic_depth.carbon
T
Chandler Carruthandjosh11b 7871237c15 Move self to the explicit () parameter list (proposal #7016) (#7272)
Implements proposal #7016: `self` moves from the deduced implicit list
(`fn F[self: Self]()`) to the front of the explicit list. Its type may
be written explicitly (`fn F(self: Self)`) or omitted, in which case it
defaults to `Self` (`fn F(self)`, `fn F(ref self)`); `self` in the
implicit list is rejected.

Throughout checking, `self` is modeled as the first explicit parameter.
Because a method is just a function whose first parameter is `self`, it
can also be called as an ordinary function with the receiver passed
explicitly (`Type.M(obj, ...)`), not only as `obj.M(...)`. A new
`SemIR::CallArgParamPatterns` helper chooses the parameters matched
against the explicit arguments, excluding a leading `self` only when it
is supplied as a method-call receiver; arity checking, conversion, and
generic deduction use it. The resulting SemIR and lowering are
unchanged: `self` is still `call_param0`, and witnesses, thunks, and
vtables are unaffected.

An omitted `self` type is parsed as a `SelfBindingPattern` node with no
type expression; checking synthesizes the `Self` type so it behaves
exactly like `self: Self`. However, the exact spelling used must match
between a forward declaration and a definition, following #3763's rules
around declaration matching.

Generated functions, thunks, and C++ interop import/export build `self`
as the first explicit parameter, and the `self`-type override (e.g.
Derived->Base for a virtual override) applies to the explicit `self`.
Placement is validated by new diagnostics: `SelfInImplicitParamList`,
`SelfNotFirstParam`, and `SelfOutsideParamList`. The benchmark source
generator and the documentation adopt the `(self)` shorthand; the
prelude, the examples, and the test data are migrated in the following
commits.

Assisted-by: Claude Code with Claude Opus 4.7

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2026-06-10 15:30:43 +00:00

215 lines
10 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/destroy.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/generic/call_basic_depth.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/generic/call_basic_depth.carbon
class C {
fn Cfn[T:! type](unused self, unused x: T) {
}
}
//@dump-sem-ir-begin
fn F[T:! type](unused x: T) {
}
//@dump-sem-ir-end
fn H[T:! type](x: T) {
//@dump-sem-ir-begin
F(x);
//@dump-sem-ir-end
}
fn G[T:! type](x: T) {
//@dump-sem-ir-begin
H(x);
F(x);
//@dump-sem-ir-end
var c: C = {};
c.Cfn(x);
}
fn M() {
var n: C = {};
//@dump-sem-ir-begin
F(n);
G(n);
//@dump-sem-ir-end
}
// CHECK:STDOUT: --- call_basic_depth.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %C: type = class_type @C [concrete]
// CHECK:STDOUT: %type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
// CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
// CHECK:STDOUT: %pattern_type.98b: type = pattern_type %C [concrete]
// CHECK:STDOUT: %pattern_type.51d: type = pattern_type %T [symbolic]
// CHECK:STDOUT: %C.Cfn.type: type = fn_type @C.Cfn [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %C.Cfn: %C.Cfn.type = struct_value () [concrete]
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
// CHECK:STDOUT: %require_complete: <witness> = require_complete_type %T [symbolic]
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
// CHECK:STDOUT: %H.type: type = fn_type @H [concrete]
// CHECK:STDOUT: %H: %H.type = struct_value () [concrete]
// CHECK:STDOUT: %F.specific_fn.eab: <specific function> = specific_function %F, @F(%T) [symbolic]
// CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
// CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
// CHECK:STDOUT: %H.specific_fn.e9c: <specific function> = specific_function %H, @H(%T) [symbolic]
// CHECK:STDOUT: %F.specific_fn.e0d: <specific function> = specific_function %F, @F(%C) [concrete]
// CHECK:STDOUT: %G.specific_fn: <specific function> = specific_function %G, @G(%C) [concrete]
// CHECK:STDOUT: %H.specific_fn.ca8: <specific function> = specific_function %H, @H(%C) [concrete]
// CHECK:STDOUT: %C.Cfn.specific_fn.380: <specific function> = specific_function %C.Cfn, @C.Cfn(%C) [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
// CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
// CHECK:STDOUT: %x.param_patt: @F.%pattern_type (%pattern_type.51d) = value_param_pattern [concrete]
// CHECK:STDOUT: %x.patt: @F.%pattern_type (%pattern_type.51d) = at_binding_pattern x, %x.param_patt [concrete]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc19_10.1: type = splice_block %.loc19_10.2 [concrete = type] {
// CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
// CHECK:STDOUT: %.loc19_10.2: type = type_literal type [concrete = type]
// CHECK:STDOUT: }
// CHECK:STDOUT: %T.loc19_7.2: type = symbolic_binding T, 0 [symbolic = %T.loc19_7.1 (constants.%T)]
// CHECK:STDOUT: %x.param: @F.%T.loc19_7.1 (%T) = value_param call_param0
// CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc19_7.2 [symbolic = %T.loc19_7.1 (constants.%T)]
// CHECK:STDOUT: %x: @F.%T.loc19_7.1 (%T) = value_binding x, %x.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @F(%T.loc19_7.2: type) {
// CHECK:STDOUT: %T.loc19_7.1: type = symbolic_binding T, 0 [symbolic = %T.loc19_7.1 (constants.%T)]
// CHECK:STDOUT: %pattern_type: type = pattern_type %T.loc19_7.1 [symbolic = %pattern_type (constants.%pattern_type.51d)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete: <witness> = require_complete_type %T.loc19_7.1 [symbolic = %require_complete (constants.%require_complete)]
// CHECK:STDOUT:
// CHECK:STDOUT: fn(%x.param: @F.%T.loc19_7.1 (%T)) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @H(%T.loc23_7.2: type) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %F.specific_fn.loc25_3.2: <specific function> = specific_function constants.%F, @F(%T.loc23_7.1) [symbolic = %F.specific_fn.loc25_3.2 (constants.%F.specific_fn.eab)]
// CHECK:STDOUT:
// CHECK:STDOUT: fn(%x.param: @H.%T.loc23_7.1 (%T)) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
// CHECK:STDOUT: %x.ref: @H.%T.loc23_7.1 (%T) = name_ref x, %x
// CHECK:STDOUT: %F.specific_fn.loc25_3.1: <specific function> = specific_function %F.ref, @F(constants.%T) [symbolic = %F.specific_fn.loc25_3.2 (constants.%F.specific_fn.eab)]
// CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc25_3.1(%x.ref)
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @G(%T.loc29_7.2: type) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %H.specific_fn.loc31_3.2: <specific function> = specific_function constants.%H, @H(%T.loc29_7.1) [symbolic = %H.specific_fn.loc31_3.2 (constants.%H.specific_fn.e9c)]
// CHECK:STDOUT: %F.specific_fn.loc32_3.2: <specific function> = specific_function constants.%F, @F(%T.loc29_7.1) [symbolic = %F.specific_fn.loc32_3.2 (constants.%F.specific_fn.eab)]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: fn(%x.param: @G.%T.loc29_7.1 (%T)) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [concrete = constants.%H]
// CHECK:STDOUT: %x.ref.loc31: @G.%T.loc29_7.1 (%T) = name_ref x, %x
// CHECK:STDOUT: %H.specific_fn.loc31_3.1: <specific function> = specific_function %H.ref, @H(constants.%T) [symbolic = %H.specific_fn.loc31_3.2 (constants.%H.specific_fn.e9c)]
// CHECK:STDOUT: %H.call: init %empty_tuple.type = call %H.specific_fn.loc31_3.1(%x.ref.loc31)
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
// CHECK:STDOUT: %x.ref.loc32: @G.%T.loc29_7.1 (%T) = name_ref x, %x
// CHECK:STDOUT: %F.specific_fn.loc32_3.1: <specific function> = specific_function %F.ref, @F(constants.%T) [symbolic = %F.specific_fn.loc32_3.2 (constants.%F.specific_fn.eab)]
// CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc32_3.1(%x.ref.loc32)
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @M() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
// CHECK:STDOUT: %n.ref.loc43: ref %C = name_ref n, %n
// CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F.ref, @F(constants.%C) [concrete = constants.%F.specific_fn.e0d]
// CHECK:STDOUT: %.loc43: %C = acquire_value %n.ref.loc43
// CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn(%.loc43)
// CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
// CHECK:STDOUT: %n.ref.loc44: ref %C = name_ref n, %n
// CHECK:STDOUT: %G.specific_fn: <specific function> = specific_function %G.ref, @G(constants.%C) [concrete = constants.%G.specific_fn]
// CHECK:STDOUT: %.loc44: %C = acquire_value %n.ref.loc44
// CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.specific_fn(%.loc44)
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @F(constants.%T) {
// CHECK:STDOUT: %T.loc19_7.1 => constants.%T
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.51d
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete => constants.%require_complete
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @H(constants.%T) {
// CHECK:STDOUT: %T.loc23_7.1 => constants.%T
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.51d
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete => constants.%require_complete
// CHECK:STDOUT: %F.specific_fn.loc25_3.2 => constants.%F.specific_fn.eab
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @G(constants.%T) {
// CHECK:STDOUT: %T.loc29_7.1 => constants.%T
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.51d
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @F(constants.%C) {
// CHECK:STDOUT: %T.loc19_7.1 => constants.%C
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.98b
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete => constants.%complete_type
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @G(constants.%C) {
// CHECK:STDOUT: %T.loc29_7.1 => constants.%C
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.98b
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete => constants.%complete_type
// CHECK:STDOUT: %H.specific_fn.loc31_3.2 => constants.%H.specific_fn.ca8
// CHECK:STDOUT: %F.specific_fn.loc32_3.2 => constants.%F.specific_fn.e0d
// CHECK:STDOUT: %C.Cfn.specific_fn.loc36_4.2 => constants.%C.Cfn.specific_fn.380
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @H(constants.%C) {
// CHECK:STDOUT: %T.loc23_7.1 => constants.%C
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.98b
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete => constants.%complete_type
// CHECK:STDOUT: %F.specific_fn.loc25_3.2 => constants.%F.specific_fn.e0d
// CHECK:STDOUT: }
// CHECK:STDOUT: