Skip to content

Commit 981f8f6

Browse files
committed
Rename "destructor" -> "deinit" (as a keyword) and -> "deinitializer" (in diagnostics).
Swift SVN r14380
1 parent 5096abc commit 981f8f6

18 files changed

+61
-39
lines changed

Diff for: docs/LangRef.html

+11-11
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h2 id="decl">Declarations</h2>
3030
<pre class="grammar">
3131
decl ::= <a href="#decl-class">decl-class</a>
3232
decl ::= <a href="#decl-constructor">decl-constructor</a>
33-
decl ::= <a href="#decl-destructor">decl-destructor</a>
33+
decl ::= <a href="#decl-deinit">decl-deinit</a>
3434
decl ::= <a href="#decl-extension">decl-extension</a>
3535
decl ::= <a href="#decl-func">decl-func</a>
3636
decl ::= <a href="#decl-import">decl-import</a>
@@ -725,31 +725,31 @@ <h3 id="decl-constructor">constructor Declarations</h3>
725725
</pre>
726726

727727
<!-- ===================================================================== -->
728-
<h3 id="decl-destructor">destructor Declarations</h3>
728+
<h3 id="decl-deinit">deinitializer Declarations</h3>
729729
<!-- ===================================================================== -->
730730

731731
<pre class="grammar">
732-
decl-constructor ::= <a href="#attribute-list">attribute-list</a> 'destructor' '(' ')' <a href="#brace-item-list">brace-item-list</a>
732+
decl-constructor ::= <a href="#attribute-list">attribute-list</a> 'deinit' '(' ')' <a href="#brace-item-list">brace-item-list</a>
733733
</pre>
734734

735-
<p>'destructor' declares a destructor for a class. This function is called
735+
<p>'deinit' declares a deinitializer for a class. This function is called
736736
when there are no longer any references to a class object, just before it
737-
is destroyed. Note that destructors can only be declared for classes,
738-
and cannot be declared in extensions. Subclass destructors implicitly
739-
invoke their superclass destructors after executing.</p>
737+
is destroyed. Note that deinitializers can only be declared for classes,
738+
and cannot be declared in extensions. Subclass deinitializers implicitly
739+
invoke their superclass deinitializers after executing.</p>
740740

741741
<p>FIXME: We haven't really decided the precise rules here, but it's probably
742742
a fatal error to either throw an exception or stash a reference to 'self'
743-
in a destructor. Not sure what happens when we cause the reference count
744-
of another object to reach zero inside a destructor. We might eventually
745-
allow destructors in extensions once we have ivars in extensions.</p>
743+
in a deinitializer. Not sure what happens when we cause the reference count
744+
of another object to reach zero inside a deinitializer. We might eventually
745+
allow deinitializers in extensions once we have ivars in extensions.</p>
746746

747747
<p>A simple example:</p>
748748

749749
<pre class="example">
750750
class X {
751751
var fd : Int
752-
destructor() {
752+
deinit() {
753753
close(fd)
754754
}
755755
}

Diff for: docs/classes.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Syntax overview
2121
class func getAMyclass() -> MyClass {
2222
return new MyClass
2323
}
24-
constructor() {
24+
init() {
2525
MyVar = 10
2626
}
27-
destructor() {
27+
deinit() {
2828
// Misc finalization
2929
}
3030
}

Diff for: include/swift/AST/Decl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3626,7 +3626,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
36263626
/// \code
36273627
/// struct X {
36283628
/// var fd : Int
3629-
/// destructor() {
3629+
/// deinit() {
36303630
/// close(fd)
36313631
/// }
36323632
/// }

Diff for: include/swift/AST/DiagnosticsParse.def

+7-4
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,15 @@ ERROR(expected_lparen_initializer,decl_parsing,PointsToFirstBadToken,
296296

297297
// Destructor
298298
ERROR(destructor_decl_outside_class,decl_parsing,none,
299-
"'destructor' functions may only be declared within a class", ())
299+
"'deinitializer' functions may only be declared within a class", ())
300300
ERROR(expected_lparen_destructor,decl_parsing,none,
301-
"expected an empty parameter list for destructor", ())
301+
"expected an empty parameter list for deinitializer", ())
302302
ERROR(destructor_parameter_nonempty_tuple,decl_parsing,none,
303-
"destructors don't have parameters", ())
303+
"deinitializers don't have parameters", ())
304304
ERROR(expected_lbrace_destructor,decl_parsing,PointsToFirstBadToken,
305-
"expected '{' for destructor", ())
305+
"expected '{' for deinitializer", ())
306+
ERROR(destructor_is_deinit,decl_parsing,none,
307+
"'destructor' is now written as 'deinit'", ())
306308

307309
// Operator
308310
ERROR(operator_decl_inner_scope,decl_parsing,none,
@@ -529,6 +531,7 @@ ERROR(unsupported_unparenthesized_array_optional,type_parsing,none,
529531
// Dynamic Self
530532
ERROR(dynamic_self_is_self,type_parsing,none,
531533
"'DynamicSelf' is now called 'Self'", ())
534+
532535
//------------------------------------------------------------------------------
533536
// Pattern parsing diagnostics
534537
//------------------------------------------------------------------------------

Diff for: include/swift/AST/KnownIdentifiers.def

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
# error Must define IDENTIFIER_WITH_NAME(Name, IdStr) before including this x-macro file
2424
#endif
2525

26-
IDENTIFIER(destructor)
27-
IDENTIFIER(DynamicSelf)
26+
IDENTIFIER(deinit)
2827
IDENTIFIER(init)
2928
IDENTIFIER(self)
3029
IDENTIFIER(Self)

Diff for: include/swift/Parse/Tokens.def

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
// Keywords that start decls.
3434
KEYWORD(class)
35+
KEYWORD(deinit)
3536
KEYWORD(destructor)
3637
KEYWORD(enum)
3738
KEYWORD(extension)

Diff for: lib/AST/ASTDumper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ static void printContext(raw_ostream &os, DeclContext *dc) {
724724
if (isa<ConstructorDecl>(AFD))
725725
os << "init";
726726
if (isa<DestructorDecl>(AFD))
727-
os << "destructor";
727+
os << "deinit";
728728
break;
729729
}
730730
}

Diff for: lib/AST/ASTPrinter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
892892
recordDeclLoc(decl);
893893
printAttributes(decl->getAttrs());
894894
printImplicitObjCNote(decl);
895-
Printer << "destructor() ";
895+
Printer << "deinit() ";
896896

897897
if (!Options.FunctionDefinitions || !decl->getBody()) {
898898
return;

Diff for: lib/AST/Decl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ ClassDecl::ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
10011001
}
10021002

10031003
DestructorDecl *ClassDecl::getDestructor() {
1004-
auto name = getASTContext().Id_destructor;
1004+
auto name = getASTContext().Id_deinit;
10051005
auto results = lookupDirect(name);
10061006
assert(!results.empty() && "Class without destructor?");
10071007
assert(results.size() == 1 && "More than one destructor?");

Diff for: lib/Basic/Demangle.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2332,10 +2332,10 @@ void NodePrinter::print(Node *pointer, bool asContext, bool suppressType) {
23322332
printEntity(false, true, "init");
23332333
return;
23342334
case Node::Kind::Destructor:
2335-
printEntity(false, false, "destructor");
2335+
printEntity(false, false, "deinit");
23362336
return;
23372337
case Node::Kind::Deallocator:
2338-
printEntity(false, false, "__deallocating_destructor");
2338+
printEntity(false, false, "__deallocating_deinit");
23392339
return;
23402340
case Node::Kind::IVarInitializer:
23412341
printEntity(false, false, "__ivar_initializer");

Diff for: lib/Parse/ParseDecl.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ static bool isStartOfModifiedDecl(const Token &Tok, const Token &Tok2,
536536
return
537537
(Tok2.is(tok::kw_func) || Tok2.is(tok::kw_val) || Tok2.is(tok::kw_var) ||
538538
Tok2.is(tok::kw_init) || Tok2.is(tok::kw_destructor) ||
539+
Tok2.is(tok::kw_deinit) ||
539540
Tok2.is(tok::kw_subscript) || Tok2.is(tok::kw_struct) ||
540541
Tok2.is(tok::kw_enum) || Tok2.is(tok::kw_class) ||
541542
Tok2.is(tok::kw_protocol) || Tok2.is(tok::kw_typealias));
@@ -557,6 +558,7 @@ bool Parser::isStartOfDecl(const Token &Tok, const Token &Tok2) {
557558
case tok::kw_import:
558559
case tok::kw_subscript:
559560
case tok::kw_init:
561+
case tok::kw_deinit:
560562
case tok::kw_destructor:
561563
case tok::kw_func:
562564
case tok::pound_if:
@@ -702,6 +704,7 @@ ParserStatus Parser::parseDecl(SmallVectorImpl<Decl*> &Entries,
702704
DeclResult = parseDeclConstructor(Flags, Attributes);
703705
Status = DeclResult;
704706
break;
707+
case tok::kw_deinit:
705708
case tok::kw_destructor:
706709
DeclResult = parseDeclDestructor(Flags, Attributes);
707710
Status = DeclResult;
@@ -2947,7 +2950,15 @@ Parser::parseDeclConstructor(ParseDeclOptions Flags,
29472950

29482951
ParserResult<DestructorDecl> Parser::
29492952
parseDeclDestructor(ParseDeclOptions Flags, DeclAttributes &Attributes) {
2950-
SourceLoc DestructorLoc = consumeToken(tok::kw_destructor);
2953+
SourceLoc DestructorLoc;
2954+
if (Tok.is(tok::kw_destructor)) {
2955+
diagnose(Tok, diag::destructor_is_deinit)
2956+
.fixItReplace(SourceLoc(Tok.getLoc()), "deinit");
2957+
2958+
DestructorLoc = consumeToken(tok::kw_destructor);
2959+
} else {
2960+
DestructorLoc = consumeToken(tok::kw_deinit);
2961+
}
29512962

29522963
ParserResult<Pattern> Params;
29532964
if (Tok.is(tok::l_paren)) {
@@ -3003,7 +3014,7 @@ parseDeclDestructor(ParseDeclOptions Flags, DeclAttributes &Attributes) {
30033014
CurDeclContext, &SelfDecl);
30043015

30053016
Scope S(this, ScopeKind::DestructorBody);
3006-
auto *DD = new (Context) DestructorDecl(Context.Id_destructor, DestructorLoc,
3017+
auto *DD = new (Context) DestructorDecl(Context.Id_deinit, DestructorLoc,
30073018
SelfPattern, CurDeclContext);
30083019
// No need to setLocalDiscriminator.
30093020
addToScope(SelfDecl);

Diff for: lib/Parse/ParseSIL.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,12 @@ bool SILParser::parseSILIdentifier(Identifier &Result, SourceLoc &Loc,
222222
Result = P.Context.getIdentifier(P.Tok.getText());
223223
break;
224224
case tok::kw_destructor:
225-
Result = P.Context.Id_destructor;
225+
P.diagnose(P.Tok, diag::destructor_is_deinit)
226+
.fixItReplace(SourceLoc(P.Tok.getLoc()), "deinit");
227+
// fall through
228+
229+
case tok::kw_deinit:
230+
Result = P.Context.Id_deinit;
226231
break;
227232
case tok::kw_init:
228233
Result = P.Context.Id_init;
@@ -672,7 +677,7 @@ static ValueDecl *lookupMember(Parser &P, Type Ty, Identifier Name) {
672677
SmallVector<ValueDecl *, 4> Lookup;
673678
unsigned options = NL_QualifiedDefault;
674679
// FIXME: a bit of a hack.
675-
if (Name == P.Context.Id_destructor || Name == P.Context.Id_init)
680+
if (Name == P.Context.Id_deinit || Name == P.Context.Id_init)
676681
options = options & ~NL_VisitSupertypes;
677682
P.SF.lookupQualified(Ty, Name, options, nullptr, Lookup);
678683
assert(Lookup.size() == 1);

Diff for: lib/Sema/TypeCheckDecl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3423,7 +3423,7 @@ void TypeChecker::addImplicitDestructor(ClassDecl *CD) {
34233423
VarDecl *selfDecl;
34243424
Pattern *selfPat = buildImplicitSelfParameter(CD->getLoc(), CD, &selfDecl);
34253425

3426-
auto *DD = new (Context) DestructorDecl(Context.Id_destructor, CD->getLoc(),
3426+
auto *DD = new (Context) DestructorDecl(Context.Id_deinit, CD->getLoc(),
34273427
selfPat, CD);
34283428

34293429
DD->setImplicit();

Diff for: lib/Serialization/Deserialization.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2084,7 +2084,7 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional<DeclContext *> ForcedContext,
20842084
if (declOrOffset.isComplete())
20852085
break;
20862086

2087-
auto dtor = new (ctx) DestructorDecl(ctx.Id_destructor, SourceLoc(),
2087+
auto dtor = new (ctx) DestructorDecl(ctx.Id_deinit, SourceLoc(),
20882088
/*selfpat*/nullptr, parent);
20892089
declOrOffset = dtor;
20902090

Diff for: test/IRGen/objc_dealloc.sil

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func onDestruct() { }
1717

1818
class SwiftGizmo : Gizmo {
1919
var x : X
20-
destructor()
20+
deinit()
2121
}
2222
sil @_TFCSo10SwiftGizmoD : $@cc(method) @thin (SwiftGizmo) -> ()
2323

@@ -91,7 +91,7 @@ bb0(%0 : $SwiftGizmo):
9191
// CHECK-NEXT: store %objc_class* bitcast (%swift.type* getelementptr inbounds (%swift.full_heapmetadata* bitcast ({ void ([[SGIZMO]]*)*, i8**, i64, %objc_class*, %swift.opaque*, %swift.opaque*, i64, { i64, i8*, i64, i64, i8*, i64, i64 }*, i64, i64 }* @_TMdCSo10SwiftGizmo to %swift.full_heapmetadata*), i32 0, i32 2) to %objc_class*), %objc_class** [[OBJC_SUPER_CLASS]], align 8
9292
// CHECK-NEXT: [[DEALLOC_SEL:%[a-zA-Z0-9]+]] = load i8** @"\01L_selector(dealloc)", align 8
9393
// CHECK-NEXT: call void bitcast (void ()* @objc_msgSendSuper2 to void (%objc_super*, i8*)*)(%objc_super* [[OBJC_SUPER]], i8* [[DEALLOC_SEL]])
94-
%5 = super_method %0 : $SwiftGizmo, #Gizmo.destructor!deallocator.foreign : $@cc(objc_method) @thin (Gizmo) -> () // user: %7
94+
%5 = super_method %0 : $SwiftGizmo, #Gizmo.deinit!deallocator.foreign : $@cc(objc_method) @thin (Gizmo) -> () // user: %7
9595
%6 = upcast %0 : $SwiftGizmo to $Gizmo // user: %7
9696
%7 = apply %5(%6) : $@cc(objc_method) @thin (Gizmo) -> ()
9797

Diff for: test/SILPasses/dead_alloc_elim.sil

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Builtin
1010
class TrivialDestructor {
1111
var int : Builtin.Int32
1212
var ptr : Builtin.ObjectPointer
13-
destructor() { }
13+
deinit() { }
1414
}
1515

1616
sil @_TFC4main17TrivialDestructorD : $@cc(method) @thin (@owned TrivialDestructor) -> () {
@@ -184,7 +184,7 @@ sil @trivial_destructor_side_effect_use
184184
class NonTrivialDestructor {
185185
var ptr : Builtin.ObjectPointer
186186

187-
destructor() { }
187+
deinit() { }
188188
}
189189

190190
sil @_TFC4main17NonTrivialDestructorD : $@cc(method) @thin (@owned NonTrivialDestructor) -> () {
@@ -216,7 +216,7 @@ sil @non_trivial_destructor_refcount_on_different_value : $@thin () -> () {
216216
class NonTrivialDestructor2 {
217217
var ptr : NonTrivialDestructor
218218

219-
destructor() { }
219+
deinit() { }
220220
}
221221

222222
// Test if dealloc_ref on a different value disrupts the algorithm.
@@ -246,7 +246,7 @@ sil @non_trivial_destructor_deallocref_on_different_value : $@thin () -> () {
246246
class NonTrivialDestructor3 {
247247
var ptr : Builtin.ObjectPointer
248248

249-
destructor() { }
249+
deinit() { }
250250
}
251251

252252
// Make sure a non-builtin apply disrupts the algorithm.
@@ -277,7 +277,7 @@ sil @non_trivial_destructor_unknown_apply : $@thin () -> () {
277277
class NonTrivialObjCDestructor {
278278
var ptr : Builtin.ObjectPointer
279279

280-
destructor() { }
280+
deinit() { }
281281
}
282282

283283
// Make sure we do not handle objc_methods

Diff for: utils/buildbot-release-notes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@
130130

131131
* 'DynamicSelf' is now called 'Self'; the semantics are unchanged.
132132

133+
* 'destructor' has been replaced with 'deinit', to emphasize that it
134+
is related to 'init'. We will refer to these as 'deinitializers'.
135+
133136
2014-02-19
134137
----------
135138

Diff for: utils/sil-mode.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
;; Integer literals
2626
'("\\b[-]?[0-9]+\\b" . font-lock-preprocessor-face)
2727
;; Decl and type keywords
28-
`(,(regexp-opt '("class" "init" "destructor" "extension" "func"
28+
`(,(regexp-opt '("class" "init" "deinit" "extension" "func"
2929
"import" "protocol" "static" "struct" "subscript"
3030
"typealias" "enum" "var" "where" "sil_vtable"
3131
"sil_global" "private" "sil_witness_table")

0 commit comments

Comments
 (0)