Skip to content

Commit 705c95d

Browse files
authored
Drop fn destroy support (#6136)
`fn destroy` is being removed per decision on #6124. It seems like the relevant decision will result in no more keyword-based function names, so this is removing all related support.
1 parent 5b34054 commit 705c95d

File tree

18 files changed

+160
-701
lines changed

18 files changed

+160
-701
lines changed

core/prelude/destroy.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package Core library "prelude/destroy";
77
import library "prelude/types/bool";
88
import library "prelude/types/int_literal";
99

10-
// Object destruction, including running `fn destroy()`. Note this is distinct
11-
// from memory deallocation.
10+
// Destroys objects. Note this is distinct from memory deallocation.
11+
// TODO: Switch to `Destructor`+`Destroy` model.
1212
interface Destroy {
1313
fn Op[addr self: Self*]();
1414
}

toolchain/check/handle_function.cpp

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ auto HandleParseNode(Context& context, Parse::ReturnTypeId node_id) -> bool {
6464
// not on the pattern stacks yet. They are only needed in that case if we have
6565
// a return type, which we now know that we do.
6666
if (context.node_stack().PeekNodeKind() ==
67-
Parse::NodeKind::IdentifierNameNotBeforeParams ||
68-
context.node_stack().PeekNodeKind() ==
69-
Parse::NodeKind::KeywordNameNotBeforeParams) {
67+
Parse::NodeKind::IdentifierNameNotBeforeParams) {
7068
context.pattern_block_stack().Push();
7169
context.full_pattern_stack().PushFullPattern(
7270
FullPatternStack::Kind::ExplicitParamList);
@@ -365,81 +363,6 @@ static auto RequestVtableIfVirtual(
365363
context.vtable_stack().AddInstId(decl_id);
366364
}
367365

368-
// Validates the `destroy` function's signature. May replace invalid values for
369-
// recovery.
370-
static auto ValidateIfDestroy(Context& context, bool is_redecl,
371-
std::optional<SemIR::Inst> parent_scope_inst,
372-
SemIR::Function& function_info) -> void {
373-
if (function_info.name_id != SemIR::NameId::Destroy) {
374-
return;
375-
}
376-
377-
// For recovery, always force explicit parameters to be empty. We do this
378-
// before any of the returns for simplicity.
379-
auto orig_param_patterns_id = function_info.param_patterns_id;
380-
function_info.param_patterns_id = SemIR::InstBlockId::Empty;
381-
382-
// Use differences on merge to diagnose remaining issues.
383-
if (is_redecl) {
384-
return;
385-
}
386-
387-
if (!parent_scope_inst || !parent_scope_inst->Is<SemIR::ClassDecl>()) {
388-
CARBON_DIAGNOSTIC(DestroyFunctionOutsideClass, Error,
389-
"declaring `fn destroy` in non-class scope");
390-
context.emitter().Emit(function_info.latest_decl_id(),
391-
DestroyFunctionOutsideClass);
392-
return;
393-
}
394-
395-
if (!function_info.self_param_id.has_value()) {
396-
CARBON_DIAGNOSTIC(DestroyFunctionMissingSelf, Error,
397-
"missing implicit `self` parameter");
398-
context.emitter().Emit(function_info.latest_decl_id(),
399-
DestroyFunctionMissingSelf);
400-
return;
401-
}
402-
403-
// `self` must be the only implicit parameter.
404-
if (auto block =
405-
context.inst_blocks().Get(function_info.implicit_param_patterns_id);
406-
block.size() > 1) {
407-
// Point at the first non-`self` parameter.
408-
auto param_id = block[function_info.self_param_id == block[0] ? 1 : 0];
409-
CARBON_DIAGNOSTIC(DestroyFunctionUnexpectedImplicitParam, Error,
410-
"unexpected implicit parameter");
411-
context.emitter().Emit(param_id, DestroyFunctionUnexpectedImplicitParam);
412-
return;
413-
}
414-
415-
if (!orig_param_patterns_id.has_value()) {
416-
CARBON_DIAGNOSTIC(DestroyFunctionPositionalParams, Error,
417-
"missing empty explicit parameter list");
418-
context.emitter().Emit(function_info.latest_decl_id(),
419-
DestroyFunctionPositionalParams);
420-
return;
421-
}
422-
423-
if (orig_param_patterns_id != SemIR::InstBlockId::Empty) {
424-
CARBON_DIAGNOSTIC(DestroyFunctionNonEmptyExplicitParams, Error,
425-
"unexpected parameter");
426-
context.emitter().Emit(context.inst_blocks().Get(orig_param_patterns_id)[0],
427-
DestroyFunctionNonEmptyExplicitParams);
428-
return;
429-
}
430-
431-
if (auto return_type_id =
432-
function_info.GetDeclaredReturnType(context.sem_ir());
433-
return_type_id.has_value() &&
434-
return_type_id != GetTupleType(context, {})) {
435-
CARBON_DIAGNOSTIC(DestroyFunctionIncorrectReturnType, Error,
436-
"incorrect return type; must be unspecified or `()`");
437-
context.emitter().Emit(function_info.return_slot_pattern_id,
438-
DestroyFunctionIncorrectReturnType);
439-
return;
440-
}
441-
}
442-
443366
// Diagnoses when positional params aren't supported. Reassigns the pattern
444367
// block if needed.
445368
static auto DiagnosePositionalParams(Context& context,
@@ -505,12 +428,6 @@ static auto BuildFunctionDecl(Context& context,
505428
function_info.definition_id = decl_id;
506429
}
507430

508-
// Analyze standard function signatures before positional parameters, so that
509-
// we can have more specific diagnostics and recovery.
510-
bool is_redecl =
511-
name_context.state == DeclNameStack::NameContext::State::Resolved;
512-
ValidateIfDestroy(context, is_redecl, parent_scope_inst, function_info);
513-
514431
DiagnosePositionalParams(context, function_info);
515432

516433
TryMergeRedecl(context, node_id, name_context, function_decl, function_info,

toolchain/check/handle_name.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -140,38 +140,6 @@ auto HandleParseNode(Context& context, Parse::IdentifierNameExprId node_id)
140140
return true;
141141
}
142142

143-
// Returns the `NameId` for a keyword node.
144-
static auto GetKeywordAsNameId(
145-
Context& context, Parse::NodeIdOneOf<Parse::KeywordNameNotBeforeParamsId,
146-
Parse::KeywordNameBeforeParamsId>
147-
node_id) -> SemIR::NameId {
148-
auto token = context.parse_tree().node_token(node_id);
149-
switch (auto token_kind = context.tokens().GetKind(token)) {
150-
case Lex::TokenKind::Destroy:
151-
return SemIR::NameId::Destroy;
152-
default:
153-
CARBON_FATAL("Unexpected token kind: {0}", token_kind);
154-
}
155-
}
156-
157-
auto HandleParseNode(Context& context,
158-
Parse::KeywordNameNotBeforeParamsId node_id) -> bool {
159-
// The parent is responsible for binding the name.
160-
context.node_stack().Push(node_id, GetKeywordAsNameId(context, node_id));
161-
return true;
162-
}
163-
164-
auto HandleParseNode(Context& context, Parse::KeywordNameBeforeParamsId node_id)
165-
-> bool {
166-
// Push a pattern block stack entry to handle the parameter pattern.
167-
context.pattern_block_stack().Push();
168-
context.full_pattern_stack().PushFullPattern(
169-
FullPatternStack::Kind::ImplicitParamList);
170-
// The parent is responsible for binding the name.
171-
context.node_stack().Push(node_id, GetKeywordAsNameId(context, node_id));
172-
return true;
173-
}
174-
175143
auto HandleParseNode(Context& context, Parse::BaseNameId node_id) -> bool {
176144
context.node_stack().Push(node_id, SemIR::NameId::Base);
177145
return true;
@@ -219,18 +187,6 @@ auto HandleParseNode(Context& context,
219187
return ApplyNameQualifier(context);
220188
}
221189

222-
auto HandleParseNode(Context& context,
223-
Parse::KeywordNameQualifierWithParamsId /*node_id*/)
224-
-> bool {
225-
return ApplyNameQualifier(context);
226-
}
227-
228-
auto HandleParseNode(Context& context,
229-
Parse::KeywordNameQualifierWithoutParamsId /*node_id*/)
230-
-> bool {
231-
return ApplyNameQualifier(context);
232-
}
233-
234190
auto HandleParseNode(Context& context, Parse::DesignatorExprId node_id)
235191
-> bool {
236192
SemIR::NameId name_id = context.node_stack().PopName();

toolchain/check/node_stack.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,6 @@ class NodeStack {
496496
case Parse::NodeKind::ImportIntroducer:
497497
case Parse::NodeKind::IndexExprStart:
498498
case Parse::NodeKind::InvalidParseStart:
499-
case Parse::NodeKind::KeywordNameQualifierWithParams:
500-
case Parse::NodeKind::KeywordNameQualifierWithoutParams:
501499
case Parse::NodeKind::LibraryIntroducer:
502500
case Parse::NodeKind::LibrarySpecifier:
503501
case Parse::NodeKind::InlineImportSpecifier:

toolchain/check/scope_stack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ class ScopeStack {
295295
// A stack for scope context.
296296
llvm::SmallVector<ScopeStackEntry> scope_stack_;
297297

298-
// A stack of `destroy` functions to call. This only has entries inside of
299-
// function bodies, where destruction on scope exit is required.
298+
// A stack of instances to destroy. This only has entries inside of function
299+
// bodies, where destruction on scope exit is required.
300300
ArrayStack<SemIR::InstId> destroy_id_stack_;
301301

302302
// Information about non-lexical scopes. This is a subset of the entries and

0 commit comments

Comments
 (0)