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>
This commit is contained in:
Chandler Carruth
2026-06-10 15:30:43 +00:00
committed by GitHub
co-authored by josh11b
parent fb05da761f
commit 7871237c15
296 changed files with 6101 additions and 4899 deletions
@@ -12,7 +12,7 @@
class C {
impl as Core.Copy {
fn Op[unused self: Self]() -> Self { return {}; }
fn Op(unused self) -> Self { return {}; }
}
}
@@ -60,12 +60,12 @@ fn M() {
// CHECK:STDOUT:
// CHECK:STDOUT: %type = type {}
// CHECK:STDOUT:
// CHECK:STDOUT: @C.val.loc15_51 = internal constant {} zeroinitializer
// CHECK:STDOUT: @C.val.loc15_43 = internal constant {} zeroinitializer
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nounwind
// CHECK:STDOUT: define void @"_COp.C.Main:Copy.Core"(ptr sret({}) %return, ptr %self) #0 !dbg !4 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @C.val.loc15_51, i64 0, i1 false), !dbg !10
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @C.val.loc15_43, i64 0, i1 false), !dbg !10
// CHECK:STDOUT: ret void, !dbg !10
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -284,7 +284,7 @@ fn M() {
// CHECK:STDOUT: !7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
// CHECK:STDOUT: !8 = !{!9}
// CHECK:STDOUT: !9 = !DILocalVariable(arg: 1, scope: !4, type: !7)
// CHECK:STDOUT: !10 = !DILocation(line: 15, column: 42, scope: !4)
// CHECK:STDOUT: !10 = !DILocation(line: 15, column: 34, scope: !4)
// CHECK:STDOUT: !11 = distinct !DISubprogram(name: "Op", linkageName: "_COp.807eb4f2d3ed7e54:core.Destroy.Core", scope: null, file: !3, line: 43, type: !12, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !14)
// CHECK:STDOUT: !12 = !DISubroutineType(types: !13)
// CHECK:STDOUT: !13 = !{null, !7}
@@ -11,7 +11,7 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call_method.carbon
class C {
fn F[unused self: Self, T:! Core.Copy](x: T) -> T {
fn F[T:! Core.Copy](unused self, x: T) -> T {
return x;
}
}
@@ -12,7 +12,7 @@
class C {
impl as Core.Copy {
fn Op[unused self: Self]() -> Self { return {}; }
fn Op(unused self) -> Self { return {}; }
}
}
@@ -43,12 +43,12 @@ fn M() {
// CHECK:STDOUT: ; ModuleID = 'call_recursive_basic.carbon'
// CHECK:STDOUT: source_filename = "call_recursive_basic.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: @C.val.loc15_51 = internal constant {} zeroinitializer
// CHECK:STDOUT: @C.val.loc15_43 = internal constant {} zeroinitializer
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nounwind
// CHECK:STDOUT: define void @"_COp.C.Main:Copy.Core"(ptr sret({}) %return, ptr %self) #0 !dbg !4 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @C.val.loc15_51, i64 0, i1 false), !dbg !10
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @C.val.loc15_43, i64 0, i1 false), !dbg !10
// CHECK:STDOUT: ret void, !dbg !10
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -78,7 +78,7 @@ fn M() {
// CHECK:STDOUT: %.loc37_5 = load ptr, ptr %ptr_f64.var, align 8, !dbg !26
// CHECK:STDOUT: %F.call.loc37 = call ptr @_CF.Main.2621742a01881e29(ptr %.loc37_5, i32 0), !dbg !27
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %c.var), !dbg !18
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %c.var, ptr align 1 @C.val.loc15_51, i64 0, i1 false), !dbg !18
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %c.var, ptr align 1 @C.val.loc15_43, i64 0, i1 false), !dbg !18
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc40_9.2.temp), !dbg !19
// CHECK:STDOUT: call void @_CF.Main.4d597ec621e4ba00(ptr %.loc40_9.2.temp, ptr %c.var, i32 0), !dbg !19
// CHECK:STDOUT: call void @"_COp.807eb4f2d3ed7e54:core.Destroy.Core"(ptr %.loc40_9.2.temp), !dbg !19
@@ -195,7 +195,7 @@ fn M() {
// CHECK:STDOUT: !7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
// CHECK:STDOUT: !8 = !{!9}
// CHECK:STDOUT: !9 = !DILocalVariable(arg: 1, scope: !4, type: !7)
// CHECK:STDOUT: !10 = !DILocation(line: 15, column: 42, scope: !4)
// CHECK:STDOUT: !10 = !DILocation(line: 15, column: 34, scope: !4)
// CHECK:STDOUT: !11 = distinct !DISubprogram(name: "M", linkageName: "_CM.Main", scope: null, file: !3, line: 28, type: !12, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !12 = !DISubroutineType(types: !13)
// CHECK:STDOUT: !13 = !{null}
@@ -15,7 +15,7 @@
// generic function for an object, from a generic context.
// The specifics with a pointer type could be deduplicated here.
class C {
fn Cfn[unused self: Self, T:! Core.Copy](x: T) -> T {
fn Cfn[T:! Core.Copy](unused self, x: T) -> T {
return x;
}
}
@@ -22,7 +22,7 @@ class Foo {
// This test verifies how the emitted definition is marked. Right now it is:
// `_COp.a9f4dd835fff2161:core.Destroy.Core`
impl Foo as Core.Copy {
fn Op[self: Self]() -> Self {
fn Op(self) -> Self {
return (self as i32).(Core.Copy.Op)() as Foo;
}
}
@@ -11,7 +11,7 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/type_representation.carbon
interface Copy {
fn Op[self: Self]() -> Self;
fn Op(self) -> Self;
}
fn F[T:! Copy & Core.Destroy](a: T) -> T {
@@ -20,7 +20,7 @@ fn F[T:! Copy & Core.Destroy](a: T) -> T {
}
impl i32 as Copy {
fn Op[self: i32]() -> i32 {
fn Op(self: i32) -> i32 {
return self;
}
}
@@ -34,7 +34,7 @@ class X {
var b: i32;
impl as Copy {
fn Op[self: X]() -> X {
fn Op(self: X) -> X {
return {.a = self.a, .b = self.b};
}
}
@@ -45,7 +45,7 @@ fn F_X(a: X) -> X {
}
impl () as Copy {
fn Op[unused self: ()]() -> () {
fn Op(unused self: ()) -> () {
return ();
}
}
@@ -55,7 +55,7 @@ fn F_empty_tuple(a: ()) -> () {
}
impl (i32, i32) as Copy {
fn Op[self: (i32, i32)]() -> (i32, i32) {
fn Op(self: (i32, i32)) -> (i32, i32) {
return self;
}
}
@@ -65,7 +65,7 @@ fn F_two_tuple(a: (i32, i32)) -> (i32, i32) {
}
impl ((i32, i32), X) as Copy {
fn Op[self: ((i32, i32), X)]() -> ((i32, i32), X) {
fn Op(self: ((i32, i32), X)) -> ((i32, i32), X) {
return (self.0.(Copy.Op)(), self.1.(Copy.Op)());
}
}