Skip to content

Commit 38d9c9f

Browse files
authored
Merge pull request swiftlang#75410 from kavon/kavon-fixes-1
Small Noncopyable Fixes, pt 1
2 parents cbb9b80 + 5230b19 commit 38d9c9f

File tree

93 files changed

+394
-448
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+394
-448
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,8 @@ ERROR(expr_keypath_type_of_property,none,
654654
(DeclNameRef, Type))
655655
ERROR(expr_keypath_generic_type,none,
656656
"key path cannot refer to generic type %0", (Identifier))
657+
ERROR(expr_keypath_noncopyable_type,none,
658+
"key path cannot refer to noncopyable type %0", (Type))
657659
ERROR(expr_keypath_not_property,none,
658660
"%select{key path|dynamic key path member lookup}1 cannot refer to %kind0",
659661
(const ValueDecl *, bool))
@@ -7741,6 +7743,8 @@ ERROR(noncopyable_objc_enum, none,
77417743
"noncopyable enums cannot be marked '@objc'", ())
77427744
ERROR(moveOnly_not_allowed_here,none,
77437745
"'@_moveOnly' attribute is only valid on structs or enums", ())
7746+
ERROR(moveOnly_deprecated,none,
7747+
"'@_moveOnly' attribute is deprecated and will be removed; use '~Copyable' instead", ())
77447748
ERROR(consume_expression_needed_for_cast,none,
77457749
"implicit conversion to %0 is consuming", (Type))
77467750
NOTE(add_consume_to_silence,none,

lib/Sema/CSApply.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,12 @@ static bool willHaveConfusingConsumption(Type type,
384384
case ConstraintLocator::ApplyArgToParam: {
385385
auto argLoc = loc->castLastElementTo<LocatorPathElt::ApplyArgToParam>();
386386
auto paramFlags = argLoc.getParameterFlags();
387-
if (paramFlags.getOwnershipSpecifier() == ParamSpecifier::Consuming)
388-
return false; // Parameter already declares 'consuming'.
387+
// If the param declares borrowing, then this implicit consumption
388+
// due to the conversion to pass the argument is indeed confusing.
389+
if (paramFlags.getOwnershipSpecifier() == ParamSpecifier::Borrowing)
390+
return true;
389391

390-
return true;
392+
return false;
391393
}
392394

393395
default:

lib/Sema/MiscDiagnostics.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,8 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
168168
if (isa<TypeExpr>(Base))
169169
checkUseOfMetaTypeName(Base);
170170

171-
if (auto *KPE = dyn_cast<KeyPathExpr>(E)) {
172-
// raise an error if this KeyPath contains an effectful member.
173-
checkForEffectfulKeyPath(KPE);
174-
}
171+
if (auto *KPE = dyn_cast<KeyPathExpr>(E))
172+
checkForInvalidKeyPath(KPE);
175173

176174
// Check function calls, looking through implicit conversions on the
177175
// function and inspecting the arguments directly.
@@ -364,11 +362,15 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
364362
}
365363

366364
/// Visit each component of the keypath and emit a diagnostic if they
367-
/// refer to a member that has effects.
368-
void checkForEffectfulKeyPath(KeyPathExpr *keyPath) {
365+
/// refer to a member that meets any of the following:
366+
/// - has effects.
367+
/// - is a noncopyable type.
368+
void checkForInvalidKeyPath(KeyPathExpr *keyPath) {
369369
for (const auto &component : keyPath->getComponents()) {
370370
if (component.hasDeclRef()) {
371371
auto decl = component.getDeclRef().getDecl();
372+
373+
// Check for effects
372374
if (auto asd = dyn_cast<AbstractStorageDecl>(decl)) {
373375
if (auto getter = asd->getEffectfulGetAccessor()) {
374376
Ctx.Diags.diagnose(component.getLoc(),
@@ -378,6 +380,13 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
378380
asd->getDescriptiveKind());
379381
}
380382
}
383+
384+
// Check for the ability to copy.
385+
if (component.getComponentType()->isNoncopyable()) {
386+
Ctx.Diags.diagnose(component.getLoc(),
387+
diag::expr_keypath_noncopyable_type,
388+
component.getComponentType()->getRValueType());
389+
}
381390
}
382391
}
383392
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,12 @@ void AttributeChecker::visitFinalAttr(FinalAttr *attr) {
25462546
}
25472547

25482548
void AttributeChecker::visitMoveOnlyAttr(MoveOnlyAttr *attr) {
2549+
// This attribute is deprecated and slated for removal.
2550+
diagnose(attr->getLocation(), diag::moveOnly_deprecated)
2551+
.fixItRemove(attr->getRange())
2552+
.warnInSwiftInterface(D->getDeclContext());
2553+
2554+
25492555
if (isa<StructDecl>(D) || isa<EnumDecl>(D))
25502556
return;
25512557

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class CheckRepressions {
113113
/// repressed or return the inverted type.
114114
Type add(Type ty, InverseTypeRepr &repr,
115115
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl) {
116-
if (!ty)
116+
if (!ty || ty->hasError())
117117
return Type();
118118
assert(!ty->is<ExistentialMetatypeType>());
119119
auto kp = ty->getKnownProtocol();

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ extension UnownedJob: CustomStringConvertible {
193193
@available(SwiftStdlib 5.9, *)
194194
@available(*, deprecated, renamed: "ExecutorJob")
195195
@frozen
196-
@_moveOnly
197-
public struct Job: Sendable {
196+
public struct Job: Sendable, ~Copyable {
198197
internal var context: Builtin.Job
199198

200199
@usableFromInline
@@ -262,8 +261,7 @@ extension Job {
262261
/// you don't generally interact with jobs directly.
263262
@available(SwiftStdlib 5.9, *)
264263
@frozen
265-
@_moveOnly
266-
public struct ExecutorJob: Sendable {
264+
public struct ExecutorJob: Sendable, ~Copyable {
267265
internal var context: Builtin.Job
268266

269267
@usableFromInline

test/Concurrency/transfernonsendable.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ sil @transferKlassContainingKlasses : $@convention(thin) @async (@guaranteed Kla
4848
sil @useKlassContainingKlasses : $@convention(thin) (@guaranteed KlassContainingKlasses) -> ()
4949
sil @constructKlassContainingKlasses : $@convention(thin) () -> @owned KlassContainingKlasses
5050

51-
@_moveOnly
52-
struct NonSendableMoveOnlyStruct {
51+
struct NonSendableMoveOnlyStruct: ~Copyable {
5352
var ns: NonSendableKlass
5453

5554
deinit

test/Concurrency/transfernonsendable_instruction_matching.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ sil @transferKlassContainingKlasses : $@convention(thin) @async (@guaranteed Kla
4242
sil @useKlassContainingKlasses : $@convention(thin) (@guaranteed KlassContainingKlasses) -> ()
4343
sil @constructKlassContainingKlasses : $@convention(thin) () -> @owned KlassContainingKlasses
4444

45-
@_moveOnly
46-
struct NonSendableMoveOnlyStruct {
45+
struct NonSendableMoveOnlyStruct: ~Copyable {
4746
var ns: NonSendableKlass
4847

4948
deinit

test/Concurrency/transfernonsendable_instruction_matching_differentiability.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ sil @transferKlassContainingKlasses : $@convention(thin) @async (@guaranteed Kla
3636
sil @useKlassContainingKlasses : $@convention(thin) (@guaranteed KlassContainingKlasses) -> ()
3737
sil @constructKlassContainingKlasses : $@convention(thin) () -> @owned KlassContainingKlasses
3838

39-
@_moveOnly
40-
struct NonSendableMoveOnlyStruct {
39+
struct NonSendableMoveOnlyStruct: ~Copyable {
4140
var ns: NonSendableKlass
4241

4342
deinit

test/Concurrency/transfernonsendable_instruction_matching_opaquevalues.sil

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class NonSendableKlass {}
2222

2323
final class SendableKlass : Sendable {}
2424

25-
@_moveOnly
26-
struct NonSendableMoveOnlyStruct {
25+
struct NonSendableMoveOnlyStruct: ~Copyable {
2726
var ns: NonSendableKlass
2827
}
2928

@@ -194,4 +193,4 @@ bb0:
194193

195194
%9999 = tuple ()
196195
return %9999 : $()
197-
}
196+
}

0 commit comments

Comments
 (0)