mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +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
+23
-22
@@ -14,10 +14,11 @@
|
||||
|
||||
class Class(T:! type) {
|
||||
var a: T;
|
||||
fn F[self: Self](n: T);
|
||||
fn F(self, n: T);
|
||||
}
|
||||
|
||||
fn Class(T:! type).F[unused self: Self](unused n: T) {}
|
||||
fn Class(T:! type).F(unused self, unused n: T) {}
|
||||
|
||||
|
||||
// CHECK:STDOUT: --- generic_method.carbon
|
||||
// CHECK:STDOUT:
|
||||
@@ -63,10 +64,10 @@ fn Class(T:! type).F[unused self: Self](unused n: T) {}
|
||||
// CHECK:STDOUT: %T.loc15_14.2: type = symbolic_binding T, 0 [symbolic = %T.loc15_14.1 (constants.%T)]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %Class.F.decl: %Class.F.type = fn_decl @Class.F [symbolic = constants.%Class.F] {
|
||||
// CHECK:STDOUT: %self.param_patt: @Class.F.%pattern_type.loc17_12 (%pattern_type.36e) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %self.patt: @Class.F.%pattern_type.loc17_12 (%pattern_type.36e) = at_binding_pattern self, %self.param_patt [concrete]
|
||||
// CHECK:STDOUT: %n.param_patt: @Class.F.%pattern_type.loc17_21 (%pattern_type.51d) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %n.patt: @Class.F.%pattern_type.loc17_21 (%pattern_type.51d) = at_binding_pattern n, %n.param_patt [concrete]
|
||||
// CHECK:STDOUT: %self.param_patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.36e) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %self.patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.36e) = at_binding_pattern self, %self.param_patt [concrete]
|
||||
// CHECK:STDOUT: %n.param_patt: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %n.patt: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = at_binding_pattern n, %n.param_patt [concrete]
|
||||
// CHECK:STDOUT: } {
|
||||
// CHECK:STDOUT: %.loc20_14.1: type = splice_block %.loc20_14.2 [concrete = type] {
|
||||
// CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
|
||||
@@ -74,9 +75,9 @@ fn Class(T:! type).F[unused self: Self](unused n: T) {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %T.loc20: type = symbolic_binding T, 0 [symbolic = @Class.%T.loc15_14.1 (constants.%T)]
|
||||
// CHECK:STDOUT: %self.param.loc20: @Class.F.%Class (%Class) = value_param call_param0
|
||||
// CHECK:STDOUT: %.loc20_35.1: type = splice_block %Self.ref.loc20 [symbolic = %Class (constants.%Class)] {
|
||||
// CHECK:STDOUT: %.loc20_35.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: %Self.ref.loc20: type = name_ref Self, %.loc20_35.2 [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: %.loc20_29.1: type = splice_block %Self.ref.loc20 [symbolic = %Class (constants.%Class)] {
|
||||
// CHECK:STDOUT: %.loc20_29.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: %Self.ref.loc20: type = name_ref Self, %.loc20_29.2 [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %self.loc20: @Class.F.%Class (%Class) = value_binding self, %self.param.loc20
|
||||
// CHECK:STDOUT: %n.param.loc20: @Class.F.%T.loc17 (%T) = value_param call_param1
|
||||
@@ -100,15 +101,15 @@ fn Class(T:! type).F[unused self: Self](unused n: T) {}
|
||||
// CHECK:STDOUT: class {
|
||||
// CHECK:STDOUT: %.loc16: @Class.%Class.elem (%Class.elem) = field_decl a, element0 [concrete]
|
||||
// CHECK:STDOUT: %Class.F.decl: @Class.%Class.F.type (%Class.F.type) = fn_decl @Class.F [symbolic = @Class.%Class.F (constants.%Class.F)] {
|
||||
// CHECK:STDOUT: %self.param_patt: @Class.F.%pattern_type.loc17_12 (%pattern_type.36e) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %self.patt: @Class.F.%pattern_type.loc17_12 (%pattern_type.36e) = at_binding_pattern self, %self.param_patt [concrete]
|
||||
// CHECK:STDOUT: %n.param_patt: @Class.F.%pattern_type.loc17_21 (%pattern_type.51d) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %n.patt: @Class.F.%pattern_type.loc17_21 (%pattern_type.51d) = at_binding_pattern n, %n.param_patt [concrete]
|
||||
// CHECK:STDOUT: %self.param_patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.36e) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %self.patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.36e) = at_binding_pattern self, %self.param_patt [concrete]
|
||||
// CHECK:STDOUT: %n.param_patt: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = value_param_pattern [concrete]
|
||||
// CHECK:STDOUT: %n.patt: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = at_binding_pattern n, %n.param_patt [concrete]
|
||||
// CHECK:STDOUT: } {
|
||||
// CHECK:STDOUT: %self.param.loc17: @Class.F.%Class (%Class) = value_param call_param0
|
||||
// CHECK:STDOUT: %.loc17_14.1: type = splice_block %Self.ref.loc17 [symbolic = %Class (constants.%Class)] {
|
||||
// CHECK:STDOUT: %.loc17_14.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, %.loc17_14.2 [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: %.loc17_8.1: type = splice_block %Self.ref.loc17 [symbolic = %Class (constants.%Class)] {
|
||||
// CHECK:STDOUT: %.loc17_8.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, %.loc17_8.2 [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %self.loc17: @Class.F.%Class (%Class) = value_binding self, %self.param.loc17
|
||||
// CHECK:STDOUT: %n.param.loc17: @Class.F.%T.loc17 (%T) = value_param call_param1
|
||||
@@ -129,12 +130,12 @@ fn Class(T:! type).F[unused self: Self](unused n: T) {}
|
||||
// CHECK:STDOUT: generic fn @Class.F(@Class.%T.loc15_14.2: type) {
|
||||
// CHECK:STDOUT: %T.loc17: type = symbolic_binding T, 0 [symbolic = %T.loc17 (constants.%T)]
|
||||
// CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.loc17) [symbolic = %Class (constants.%Class)]
|
||||
// CHECK:STDOUT: %pattern_type.loc17_12: type = pattern_type %Class [symbolic = %pattern_type.loc17_12 (constants.%pattern_type.36e)]
|
||||
// CHECK:STDOUT: %pattern_type.loc17_21: type = pattern_type %T.loc17 [symbolic = %pattern_type.loc17_21 (constants.%pattern_type.51d)]
|
||||
// CHECK:STDOUT: %pattern_type.loc17_8: type = pattern_type %Class [symbolic = %pattern_type.loc17_8 (constants.%pattern_type.36e)]
|
||||
// CHECK:STDOUT: %pattern_type.loc17_15: type = pattern_type %T.loc17 [symbolic = %pattern_type.loc17_15 (constants.%pattern_type.51d)]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !definition:
|
||||
// CHECK:STDOUT: %require_complete.loc20_33: <witness> = require_complete_type %Class [symbolic = %require_complete.loc20_33 (constants.%require_complete.4d9)]
|
||||
// CHECK:STDOUT: %require_complete.loc20_49: <witness> = require_complete_type %T.loc17 [symbolic = %require_complete.loc20_49 (constants.%require_complete.944)]
|
||||
// CHECK:STDOUT: %require_complete.loc20_29: <witness> = require_complete_type %Class [symbolic = %require_complete.loc20_29 (constants.%require_complete.4d9)]
|
||||
// CHECK:STDOUT: %require_complete.loc20_43: <witness> = require_complete_type %T.loc17 [symbolic = %require_complete.loc20_43 (constants.%require_complete.944)]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn(%self.param.loc20: @Class.F.%Class (%Class), %n.param.loc20: @Class.F.%T.loc17 (%T)) {
|
||||
// CHECK:STDOUT: !entry:
|
||||
@@ -158,7 +159,7 @@ fn Class(T:! type).F[unused self: Self](unused n: T) {}
|
||||
// CHECK:STDOUT: specific @Class.F(constants.%T) {
|
||||
// CHECK:STDOUT: %T.loc17 => constants.%T
|
||||
// CHECK:STDOUT: %Class => constants.%Class
|
||||
// CHECK:STDOUT: %pattern_type.loc17_12 => constants.%pattern_type.36e
|
||||
// CHECK:STDOUT: %pattern_type.loc17_21 => constants.%pattern_type.51d
|
||||
// CHECK:STDOUT: %pattern_type.loc17_8 => constants.%pattern_type.36e
|
||||
// CHECK:STDOUT: %pattern_type.loc17_15 => constants.%pattern_type.51d
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
|
||||
Reference in New Issue
Block a user