Skip to content

Commit cbb1ddc

Browse files
authored
Merge pull request #71420 from Azoy/fix-seo-atomic
[Sema] Diagnose @_staticExclusiveOnly in clients without the feature flag
2 parents dc959a4 + 69370ec commit cbb1ddc

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,8 +2497,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24972497

24982498
// @_staticExclusiveOnly types cannot be put into 'var's, only 'let'.
24992499
if (auto SD = VD->getInterfaceType()->getStructOrBoundGenericStruct()) {
2500-
if (Ctx.LangOpts.hasFeature(Feature::StaticExclusiveOnly) &&
2501-
SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
2500+
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
25022501
!VD->isLet()) {
25032502
Ctx.Diags.diagnoseWithNotes(
25042503
VD->diagnose(diag::attr_static_exclusive_only_let_only,
@@ -4214,8 +4213,7 @@ void TypeChecker::checkParameterList(ParameterList *params,
42144213
// @_staticExclusiveOnly types cannot be passed as 'inout', only as either
42154214
// a borrow or as consuming.
42164215
if (auto SD = param->getInterfaceType()->getStructOrBoundGenericStruct()) {
4217-
if (SD->getASTContext().LangOpts.hasFeature(Feature::StaticExclusiveOnly) &&
4218-
SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
4216+
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
42194217
param->isInOut()) {
42204218
SD->getASTContext().Diags.diagnoseWithNotes(
42214219
param->diagnose(diag::attr_static_exclusive_only_let_only_param,

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,8 +3771,7 @@ TypeResolver::resolveASTFunctionTypeParams(TupleTypeRepr *inputRepr,
37713771
// @_staticExclusiveOnly types cannot be passed as 'inout' in function
37723772
// types.
37733773
if (auto SD = ty->getStructOrBoundGenericStruct()) {
3774-
if (getASTContext().LangOpts.hasFeature(Feature::StaticExclusiveOnly) &&
3775-
SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
3774+
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
37763775
ownership == ParamSpecifier::InOut) {
37773776
diagnose(eltTypeRepr->getLoc(),
37783777
diag::attr_static_exclusive_only_let_only_param,

test/Sema/atomic-diagnose-var.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking
2+
// REQUIRES: synchronization
3+
4+
import Synchronization
5+
6+
var a = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}
7+
8+
let b = Atomic(0) // OK
9+
10+
class C {
11+
var d = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}
12+
let e = Atomic(0) // OK
13+
}
14+
15+
struct F: ~Copyable {
16+
var g = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}
17+
let h = Atomic(0) // OK
18+
}
19+
20+
func i(_: borrowing Atomic<Int>) {} // OK
21+
22+
func j(_: inout Atomic<Int>) {} // expected-error {{parameter of type 'Atomic<Int>' must be declared as either 'borrowing' or 'consuming'}}
23+
24+
func k(_: (inout Atomic<Int>) -> ()) {} // expected-error {{parameter of type 'Atomic<Int>' must be declared as either 'borrowing' or 'consuming'}}
25+
26+
func l(_: (borrowing Atomic<Int>) -> ()) {} // OK
27+
28+
func m() {
29+
let _: (Int, Int) -> Int = {
30+
var n = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}
31+
// expected-warning@-1 {{initialization of variable 'n' was never used; consider replacing with assignment to '_' or removing it}}
32+
33+
return $0 + $1
34+
}
35+
}
36+
37+
func o(_: consuming Atomic<Int>) {} // OK
38+
39+
func p(_: (consuming Atomic<Int>) -> ()) {} // OK

test/lit.site.cfg.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ if "@SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION@" == "TRUE":
155155
config.available_features.add('observation')
156156
if "@SWIFT_STDLIB_ENABLE_DEBUG_PRECONDITIONS_IN_RELEASE@" == "TRUE":
157157
config.available_features.add('swift_stdlib_debug_preconditions_in_release')
158+
if "@SWIFT_ENABLE_SYNCHRONIZATION@" == "TRUE":
159+
config.available_features.add('synchronization')
158160

159161
config.swift_freestanding_is_darwin = "@SWIFT_FREESTANDING_IS_DARWIN@" == "TRUE"
160162
config.swift_stdlib_use_relative_protocol_witness_tables = "@SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES@" == "TRUE"

test/stdlib/Atomics/LockFreeSingleConsumerStack.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class LockFreeSingleConsumerStack<Element> {
1717
}
1818
typealias NodePtr = UnsafeMutablePointer<Node>
1919

20-
private var _last = Atomic<NodePtr?>(nil)
21-
private var _consumerCount = Atomic<Int>(0)
20+
private let _last = Atomic<NodePtr?>(nil)
21+
private let _consumerCount = Atomic<Int>(0)
2222
private var foo = 0
2323

2424
deinit {

0 commit comments

Comments
 (0)