Skip to content

Commit a9f730f

Browse files
committed
[Sema] Ban @_eagerMove on Copyable things.
1 parent 53cb4a7 commit a9f730f

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,8 @@ ERROR(lifetime_invalid_global_scope,none, "%0 is only valid on methods",
35203520
ERROR(eagermove_and_lexical_combined,none,
35213521
"@_eagerMove and @_noEagerMove attributes are alternate styles of lifetimes "
35223522
"and can't be combined", ())
3523+
ERROR(eagermove_and_noncopyable_combined,none,
3524+
"@_eagerMove cannot be applied to NonCopyable types", ())
35233525

35243526
ERROR(autoclosure_function_type,none,
35253527
"@autoclosure attribute only applies to function types",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6878,6 +6878,25 @@ bool AttributeChecker::visitLifetimeAttr(DeclAttribute *attr) {
68786878
void AttributeChecker::visitEagerMoveAttr(EagerMoveAttr *attr) {
68796879
if (visitLifetimeAttr(attr))
68806880
return;
6881+
if (auto *nominal = dyn_cast<NominalTypeDecl>(D)) {
6882+
if (nominal->getDeclaredInterfaceType()->isPureMoveOnly()) {
6883+
diagnoseAndRemoveAttr(attr, diag::eagermove_and_noncopyable_combined);
6884+
return;
6885+
}
6886+
}
6887+
if (auto *func = dyn_cast<FuncDecl>(D)) {
6888+
auto *self = func->getImplicitSelfDecl();
6889+
if (self && self->getType()->isPureMoveOnly()) {
6890+
diagnoseAndRemoveAttr(attr, diag::eagermove_and_noncopyable_combined);
6891+
return;
6892+
}
6893+
}
6894+
if (auto *pd = dyn_cast<ParamDecl>(D)) {
6895+
if (pd->getType()->isPureMoveOnly()) {
6896+
diagnoseAndRemoveAttr(attr, diag::eagermove_and_noncopyable_combined);
6897+
return;
6898+
}
6899+
}
68816900
}
68826901

68836902
void AttributeChecker::visitNoEagerMoveAttr(NoEagerMoveAttr *attr) {

test/attr/lexical.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,16 @@ func foo() {
4343
_ = s2
4444
_ = s3
4545
}
46+
47+
@_moveOnly struct MoveOnly {}
48+
49+
@_eagerMove @_moveOnly struct MoveOnlyEagerly {} // expected-error {{@_eagerMove cannot be applied to NonCopyable types}}
50+
51+
func zoo(@_eagerMove _ : consuming MoveOnly) {} // expected-error {{@_eagerMove cannot be applied to NonCopyable types}}
52+
53+
func zooo(@_noEagerMove _ : consuming C) {} // ok, only way to spell this behavior
54+
55+
extension MoveOnly {
56+
@_eagerMove // expected-error {{@_eagerMove cannot be applied to NonCopyable types}}
57+
func zoo() {}
58+
}

0 commit comments

Comments
 (0)