Skip to content

allow discard in generic noncopyable type #65896

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
May 13, 2023
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
2 changes: 1 addition & 1 deletion lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool TypeBase::isMarkerExistential() {
}

bool TypeBase::isPureMoveOnly() {
if (auto *nom = getNominalOrBoundGenericNominal())
if (auto *nom = getAnyNominal())
return nom->isMoveOnly();

// if any components of the tuple are move-only, then the tuple is move-only.
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,9 @@ IsFinalRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
}

bool IsMoveOnlyRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
// For now only do this for nominal type decls.
if (isa<NominalTypeDecl>(decl)) {
// TODO: isPureMoveOnly and isMoveOnly and other checks are all spread out
// and need to be merged together.
if (isa<ClassDecl>(decl) || isa<StructDecl>(decl) || isa<EnumDecl>(decl)) {
if (decl->getAttrs().hasAttribute<MoveOnlyAttr>()) {
if (!decl->getASTContext().supportsMoveOnlyTypes())
decl->diagnose(diag::moveOnly_requires_lexical_lifetimes);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
// check the kind of type this discard statement appears within.
if (!diagnosed) {
auto *nominalDecl = fn->getDeclContext()->getSelfNominalTypeDecl();
Type nominalType = nominalDecl->getDeclaredType();
Type nominalType = nominalDecl->getDeclaredTypeInContext();

// must be noncopyable
if (!nominalType->isPureMoveOnly()) {
Expand Down
6 changes: 6 additions & 0 deletions test/Sema/discard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,9 @@ enum NoDeinitEnum: ~Copyable {
discard self // expected-error {{'discard' has no effect for type 'NoDeinitEnum' unless it has a deinitializer}}{{5-18=}}
}
}

struct HasGenericNotStored<T>: ~Copyable {
consuming func discard() { discard self }
func identity(_ t: T) -> T { return t }
deinit{}
}
12 changes: 12 additions & 0 deletions test/Sema/discard_trivially_destroyed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ struct AllOK: ~Copyable {
consuming func doDiscard() { discard self }
deinit {}
}

struct HasGenericStored<T>: ~Copyable {
let t: T // expected-note {{type 'T' cannot be trivially destroyed}}
consuming func discard() { discard self } // expected-error {{can only 'discard' type 'HasGenericStored<T>' if it contains trivially-destroyed stored properties at this time}}
deinit{}
}

struct HasAny: ~Copyable {
var t: Any // expected-note {{type 'Any' cannot be trivially destroyed}}
consuming func discard() { discard self } // expected-error {{can only 'discard' type 'HasAny' if it contains trivially-destroyed stored properties at this time}}
deinit{}
}