Skip to content

Commit c587c69

Browse files
authored
Merge pull request #5536 from ahoppen/SR-3043-deinit-reference
[TypeChecker] Add error message when accessing a type's destructor
2 parents cd74545 + b0e67da commit c587c69

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ ERROR(cannot_convert_closure_result_protocol,none,
340340
ERROR(cannot_convert_closure_result_nil,none,
341341
"nil is not compatible with closure result type %0", (Type))
342342

343+
ERROR(destructor_not_accessible,none,
344+
"deinitializers cannot be accessed", ())
345+
343346
// Array Element
344347
ERROR(cannot_convert_array_element,none,
345348
"cannot convert value of type %0 to expected element type %1",

lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,10 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
27742774

27752775
return;
27762776
}
2777+
case MemberLookupResult::UR_DestructorInaccessible: {
2778+
diagnose(nameLoc, diag::destructor_not_accessible);
2779+
return;
2780+
}
27772781
}
27782782
}
27792783

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,11 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
30163016
// reasonable choice.
30173017
auto addChoice = [&](ValueDecl *cand, bool isBridged,
30183018
bool isUnwrappedOptional) {
3019+
// Destructors cannot be referenced manually
3020+
if (isa<DestructorDecl>(cand)) {
3021+
result.addUnviable(cand, MemberLookupResult::UR_DestructorInaccessible);
3022+
return;
3023+
}
30193024
// If the result is invalid, skip it.
30203025
TC.validateDecl(cand, true);
30213026
if (cand->isInvalid()) {

lib/Sema/ConstraintSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,9 @@ struct MemberLookupResult {
802802

803803
/// The member is inaccessible (e.g. a private member in another file).
804804
UR_Inaccessible,
805+
806+
// A type's destructor cannot be referenced
807+
UR_DestructorInaccessible,
805808
};
806809

807810
/// This is a list of considered, but rejected, candidates, along with a

test/expr/postfix/dot/dot_keywords.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,19 @@ class SE0071Derived : SE0071Base {
3838
super.default()
3939
}
4040
}
41+
42+
// SR-3043: Diagnostics when accessing deinit
43+
44+
class SR3043Base {
45+
}
46+
47+
class SR3043Derived: SR3043Base {
48+
deinit {
49+
super.deinit() // expected-error {{deinitializers cannot be accessed}}
50+
}
51+
}
52+
53+
let sr3043 = SR3043Derived()
54+
sr3043.deinit() // expected-error {{deinitializers cannot be accessed}}
55+
sr3043.deinit // expected-error {{deinitializers cannot be accessed}}
56+
SR3043Derived.deinit() // expected-error {{deinitializers cannot be accessed}}

0 commit comments

Comments
 (0)