mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:20:10 +01:00
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:
co-authored by
josh11b
parent
fb05da761f
commit
7871237c15
+24
-23
@@ -48,7 +48,7 @@ library "[[@TEST_NAME]]";
|
||||
|
||||
interface Z {
|
||||
fn Zero();
|
||||
fn Method[self: Self]();
|
||||
fn Method(self);
|
||||
|
||||
// CHECK:STDERR: fail_impl_as_self_interface.carbon:[[@LINE+4]]:9: error: `impl as` can only be used in a class [ImplAsOutsideClass]
|
||||
// CHECK:STDERR: impl as Z {
|
||||
@@ -56,14 +56,14 @@ interface Z {
|
||||
// CHECK:STDERR:
|
||||
impl as Z {
|
||||
fn Zero() {}
|
||||
fn Method[unused self: Self]() {}
|
||||
fn Method(unused self) {}
|
||||
}
|
||||
}
|
||||
|
||||
class Point {
|
||||
impl as Z {
|
||||
fn Zero() {}
|
||||
fn Method[unused self: Self]() {}
|
||||
fn Method(unused self) {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ class X {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CHECK:STDOUT: --- fail_impl_as_file_scope.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: constants {
|
||||
@@ -312,13 +313,13 @@ class X {
|
||||
// CHECK:STDOUT: %self.param_patt: @Z.WithSelf.Method.%pattern_type (%pattern_type.52a) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %self.patt: @Z.WithSelf.Method.%pattern_type (%pattern_type.52a) = at_binding_pattern self, %self.param_patt [concrete]
|
||||
// CHECK:STDOUT: } {
|
||||
// CHECK:STDOUT: %self.param: @Z.WithSelf.Method.%Self.as_type.loc5_19.1 (%Self.as_type.0cf) = value_param call_param0
|
||||
// CHECK:STDOUT: %.loc5_19.1: type = splice_block %.loc5_19.2 [symbolic = %Self.as_type.loc5_19.1 (constants.%Self.as_type.0cf)] {
|
||||
// CHECK:STDOUT: %self.param: @Z.WithSelf.Method.%Self.as_type.loc5_13.1 (%Self.as_type.0cf) = value_param call_param0
|
||||
// CHECK:STDOUT: %.loc5_13.1: type = splice_block %.loc5_13.2 [symbolic = %Self.as_type.loc5_13.1 (constants.%Self.as_type.0cf)] {
|
||||
// CHECK:STDOUT: %Self.ref: %Z.type = name_ref Self, @Z.%Self [symbolic = %Self (constants.%Self.cb6)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_19.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc5_19.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %.loc5_19.2: type = converted %Self.ref, %Self.as_type.loc5_19.2 [symbolic = %Self.as_type.loc5_19.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_13.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc5_13.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %.loc5_13.2: type = converted %Self.ref, %Self.as_type.loc5_13.2 [symbolic = %Self.as_type.loc5_13.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %self: @Z.WithSelf.Method.%Self.as_type.loc5_19.1 (%Self.as_type.0cf) = value_binding self, %self.param
|
||||
// CHECK:STDOUT: %self: @Z.WithSelf.Method.%Self.as_type.loc5_13.1 (%Self.as_type.0cf) = value_binding self, %self.param
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %assoc1: %Z.assoc_type = assoc_entity element1, %Z.WithSelf.Method.decl [concrete = constants.%assoc1]
|
||||
// CHECK:STDOUT: impl_decl @<error>.as.Z.impl [concrete] {} {
|
||||
@@ -350,13 +351,13 @@ class X {
|
||||
// CHECK:STDOUT: %self.param_patt: @<error>.as.Z.impl.Method.%pattern_type (%pattern_type.52a) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %self.patt: @<error>.as.Z.impl.Method.%pattern_type (%pattern_type.52a) = at_binding_pattern self, %self.param_patt [concrete]
|
||||
// CHECK:STDOUT: } {
|
||||
// CHECK:STDOUT: %self.param: @<error>.as.Z.impl.Method.%Self.as_type.loc13_28.1 (%Self.as_type.0cf) = value_param call_param0
|
||||
// CHECK:STDOUT: %.loc13_28.1: type = splice_block %.loc13_28.2 [symbolic = %Self.as_type.loc13_28.1 (constants.%Self.as_type.0cf)] {
|
||||
// CHECK:STDOUT: %self.param: @<error>.as.Z.impl.Method.%Self.as_type.loc13_22.1 (%Self.as_type.0cf) = value_param call_param0
|
||||
// CHECK:STDOUT: %.loc13_22.1: type = splice_block %.loc13_22.2 [symbolic = %Self.as_type.loc13_22.1 (constants.%Self.as_type.0cf)] {
|
||||
// CHECK:STDOUT: %Self.ref: %Z.type = name_ref Self, @Z.%Self [symbolic = %Self (constants.%Self.cb6)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc13_28.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc13_28.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %.loc13_28.2: type = converted %Self.ref, %Self.as_type.loc13_28.2 [symbolic = %Self.as_type.loc13_28.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc13_22.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc13_22.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %.loc13_22.2: type = converted %Self.ref, %Self.as_type.loc13_22.2 [symbolic = %Self.as_type.loc13_22.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %self: @<error>.as.Z.impl.Method.%Self.as_type.loc13_28.1 (%Self.as_type.0cf) = value_binding self, %self.param
|
||||
// CHECK:STDOUT: %self: @<error>.as.Z.impl.Method.%Self.as_type.loc13_22.1 (%Self.as_type.0cf) = value_binding self, %self.param
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !members:
|
||||
@@ -406,10 +407,10 @@ class X {
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: generic fn @Z.WithSelf.Method(@Z.%Self: %Z.type) {
|
||||
// CHECK:STDOUT: %Self: %Z.type = symbolic_binding Self, 0 [symbolic = %Self (constants.%Self.cb6)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_19.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc5_19.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type.loc5_19.1 [symbolic = %pattern_type (constants.%pattern_type.52a)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_13.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc5_13.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type.loc5_13.1 [symbolic = %pattern_type (constants.%pattern_type.52a)]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn(%self.param: @Z.WithSelf.Method.%Self.as_type.loc5_19.1 (%Self.as_type.0cf));
|
||||
// CHECK:STDOUT: fn(%self.param: @Z.WithSelf.Method.%Self.as_type.loc5_13.1 (%Self.as_type.0cf));
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: generic fn @<error>.as.Z.impl.Zero(@Z.%Self: %Z.type) {
|
||||
@@ -423,13 +424,13 @@ class X {
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: generic fn @<error>.as.Z.impl.Method(@Z.%Self: %Z.type) {
|
||||
// CHECK:STDOUT: %Self: %Z.type = symbolic_binding Self, 0 [symbolic = %Self (constants.%Self.cb6)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc13_28.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc13_28.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type.loc13_28.1 [symbolic = %pattern_type (constants.%pattern_type.52a)]
|
||||
// CHECK:STDOUT: %Self.as_type.loc13_22.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc13_22.1 (constants.%Self.as_type.0cf)]
|
||||
// CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type.loc13_22.1 [symbolic = %pattern_type (constants.%pattern_type.52a)]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !definition:
|
||||
// CHECK:STDOUT: %require_complete: <witness> = require_complete_type %Self.as_type.loc13_28.1 [symbolic = %require_complete (constants.%require_complete)]
|
||||
// CHECK:STDOUT: %require_complete: <witness> = require_complete_type %Self.as_type.loc13_22.1 [symbolic = %require_complete (constants.%require_complete)]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn(%self.param: @<error>.as.Z.impl.Method.%Self.as_type.loc13_28.1 (%Self.as_type.0cf)) {
|
||||
// CHECK:STDOUT: fn(%self.param: @<error>.as.Z.impl.Method.%Self.as_type.loc13_22.1 (%Self.as_type.0cf)) {
|
||||
// CHECK:STDOUT: !entry:
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
@@ -490,7 +491,7 @@ class X {
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: specific @Z.WithSelf.Method(constants.%Self.cb6) {
|
||||
// CHECK:STDOUT: %Self => constants.%Self.cb6
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_19.1 => constants.%Self.as_type.0cf
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_13.1 => constants.%Self.as_type.0cf
|
||||
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.52a
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
@@ -507,7 +508,7 @@ class X {
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: specific @<error>.as.Z.impl.Method(constants.%Self.cb6) {
|
||||
// CHECK:STDOUT: %Self => constants.%Self.cb6
|
||||
// CHECK:STDOUT: %Self.as_type.loc13_28.1 => constants.%Self.as_type.0cf
|
||||
// CHECK:STDOUT: %Self.as_type.loc13_22.1 => constants.%Self.as_type.0cf
|
||||
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.52a
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
@@ -524,7 +525,7 @@ class X {
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: specific @Z.WithSelf.Method(constants.%Z.facet) {
|
||||
// CHECK:STDOUT: %Self => constants.%Z.facet
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_19.1 => constants.%Point
|
||||
// CHECK:STDOUT: %Self.as_type.loc5_13.1 => constants.%Point
|
||||
// CHECK:STDOUT: %pattern_type => constants.%pattern_type.9d8
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
|
||||
Reference in New Issue
Block a user