mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 14:21:09 +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
@@ -21,6 +21,7 @@
|
||||
#include "toolchain/check/type.h"
|
||||
#include "toolchain/diagnostics/format_providers.h"
|
||||
#include "toolchain/sem_ir/expr_info.h"
|
||||
#include "toolchain/sem_ir/ids.h"
|
||||
#include "toolchain/sem_ir/inst_kind.h"
|
||||
#include "toolchain/sem_ir/pattern.h"
|
||||
|
||||
@@ -978,7 +979,7 @@ auto CalleePatternMatch(Context& context,
|
||||
.param_ranges = {implicit_end, explicit_end, return_end}};
|
||||
}
|
||||
|
||||
auto ThunkPatternMatch(Context& context, SemIR::InstId self_pattern_id,
|
||||
auto ThunkPatternMatch(Context& context,
|
||||
llvm::ArrayRef<SemIR::InstId> param_pattern_ids,
|
||||
llvm::ArrayRef<SemIR::InstId> outer_call_args)
|
||||
-> ThunkPatternMatchResults {
|
||||
@@ -986,15 +987,7 @@ auto ThunkPatternMatch(Context& context, SemIR::InstId self_pattern_id,
|
||||
MatchContext match(context);
|
||||
|
||||
llvm::SmallVector<SemIR::InstId> inner_args;
|
||||
inner_args.reserve(outer_call_args.size() + 1);
|
||||
|
||||
if (self_pattern_id.has_value()) {
|
||||
inner_args.push_back(match.MatchWithResult(
|
||||
&state,
|
||||
{.pattern_id = self_pattern_id,
|
||||
.work = MatchContext::PreWork{.scrutinee_id = SemIR::InstId::None},
|
||||
.allow_unmarked_ref = true}));
|
||||
}
|
||||
inner_args.reserve(outer_call_args.size());
|
||||
|
||||
for (SemIR::InstId inst_id : param_pattern_ids) {
|
||||
inner_args.push_back(match.MatchWithResult(
|
||||
@@ -1035,18 +1028,21 @@ auto CallerPatternMatch(Context& context, SemIR::SpecificId specific_id,
|
||||
CallerState state = {.callee_specific_id = specific_id};
|
||||
MatchContext match(context);
|
||||
|
||||
if (self_pattern_id.has_value()) {
|
||||
match.Match(&state,
|
||||
{.pattern_id = self_pattern_id,
|
||||
.work = MatchContext::PreWork{.scrutinee_id = self_arg_id},
|
||||
.allow_unmarked_ref = true});
|
||||
// When we have a separate `self_arg_id`, we concatenate that onto the front
|
||||
// of the arg_refs to match against the first parameter.
|
||||
llvm::ArrayRef<SemIR::InstId> self_arg_refs = {};
|
||||
if (self_arg_id.has_value()) {
|
||||
self_arg_refs = self_arg_id;
|
||||
CARBON_CHECK(self_pattern_id.has_value());
|
||||
}
|
||||
|
||||
for (auto [arg_id, param_pattern_id] : llvm::zip_equal(
|
||||
arg_refs, context.inst_blocks().GetOrEmpty(param_patterns_id))) {
|
||||
match.Match(&state, {.pattern_id = param_pattern_id,
|
||||
.work = MatchContext::PreWork{.scrutinee_id = arg_id},
|
||||
.allow_unmarked_ref = is_desugared});
|
||||
for (const auto& [arg_id, param_pattern_id] : llvm::zip_equal(
|
||||
llvm::concat<const SemIR::InstId>(self_arg_refs, arg_refs),
|
||||
context.inst_blocks().GetOrEmpty(param_patterns_id))) {
|
||||
match.Match(&state,
|
||||
{.pattern_id = param_pattern_id,
|
||||
.work = MatchContext::PreWork{.scrutinee_id = arg_id},
|
||||
.allow_unmarked_ref = arg_id == self_arg_id || is_desugared});
|
||||
}
|
||||
|
||||
// Track the return storage, if present.
|
||||
|
||||
Reference in New Issue
Block a user