Skip to content

[TypeChecker] Add error message when accessing a type's destructor #5536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ ERROR(cannot_convert_closure_result_protocol,none,
ERROR(cannot_convert_closure_result_nil,none,
"nil is not compatible with closure result type %0", (Type))

ERROR(destructor_not_accessible,none,
"deinitializers cannot be accessed", ())

// Array Element
ERROR(cannot_convert_array_element,none,
"cannot convert value of type %0 to expected element type %1",
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2774,6 +2774,10 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,

return;
}
case MemberLookupResult::UR_DestructorInaccessible: {
diagnose(nameLoc, diag::destructor_not_accessible);
return;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3016,6 +3016,11 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
// reasonable choice.
auto addChoice = [&](ValueDecl *cand, bool isBridged,
bool isUnwrappedOptional) {
// Destructors cannot be referenced manually
if (isa<DestructorDecl>(cand)) {
result.addUnviable(cand, MemberLookupResult::UR_DestructorInaccessible);
return;
}
// If the result is invalid, skip it.
TC.validateDecl(cand, true);
if (cand->isInvalid()) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,9 @@ struct MemberLookupResult {

/// The member is inaccessible (e.g. a private member in another file).
UR_Inaccessible,

// A type's destructor cannot be referenced
UR_DestructorInaccessible,
};

/// This is a list of considered, but rejected, candidates, along with a
Expand Down
16 changes: 16 additions & 0 deletions test/expr/postfix/dot/dot_keywords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ class SE0071Derived : SE0071Base {
super.default()
}
}

// SR-3043: Diagnostics when accessing deinit

class SR3043Base {
}

class SR3043Derived: SR3043Base {
deinit {
super.deinit() // expected-error {{deinitializers cannot be accessed}}
}
}

let sr3043 = SR3043Derived()
sr3043.deinit() // expected-error {{deinitializers cannot be accessed}}
sr3043.deinit // expected-error {{deinitializers cannot be accessed}}
SR3043Derived.deinit() // expected-error {{deinitializers cannot be accessed}}