Skip to content

[Sema] Diagnose @_staticExclusiveOnly in clients without the feature flag #71420

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 2 commits into from
Feb 7, 2024
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
6 changes: 2 additions & 4 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2497,8 +2497,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

// @_staticExclusiveOnly types cannot be put into 'var's, only 'let'.
if (auto SD = VD->getInterfaceType()->getStructOrBoundGenericStruct()) {
if (Ctx.LangOpts.hasFeature(Feature::StaticExclusiveOnly) &&
SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
!VD->isLet()) {
Ctx.Diags.diagnoseWithNotes(
VD->diagnose(diag::attr_static_exclusive_only_let_only,
Expand Down Expand Up @@ -4214,8 +4213,7 @@ void TypeChecker::checkParameterList(ParameterList *params,
// @_staticExclusiveOnly types cannot be passed as 'inout', only as either
// a borrow or as consuming.
if (auto SD = param->getInterfaceType()->getStructOrBoundGenericStruct()) {
if (SD->getASTContext().LangOpts.hasFeature(Feature::StaticExclusiveOnly) &&
SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
param->isInOut()) {
SD->getASTContext().Diags.diagnoseWithNotes(
param->diagnose(diag::attr_static_exclusive_only_let_only_param,
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3771,8 +3771,7 @@ TypeResolver::resolveASTFunctionTypeParams(TupleTypeRepr *inputRepr,
// @_staticExclusiveOnly types cannot be passed as 'inout' in function
// types.
if (auto SD = ty->getStructOrBoundGenericStruct()) {
if (getASTContext().LangOpts.hasFeature(Feature::StaticExclusiveOnly) &&
SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
ownership == ParamSpecifier::InOut) {
diagnose(eltTypeRepr->getLoc(),
diag::attr_static_exclusive_only_let_only_param,
Expand Down
39 changes: 39 additions & 0 deletions test/Sema/atomic-diagnose-var.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %target-typecheck-verify-swift -disable-availability-checking
// REQUIRES: synchronization

import Synchronization

var a = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}

let b = Atomic(0) // OK

class C {
var d = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}
let e = Atomic(0) // OK
}

struct F: ~Copyable {
var g = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}
let h = Atomic(0) // OK
}

func i(_: borrowing Atomic<Int>) {} // OK

func j(_: inout Atomic<Int>) {} // expected-error {{parameter of type 'Atomic<Int>' must be declared as either 'borrowing' or 'consuming'}}

func k(_: (inout Atomic<Int>) -> ()) {} // expected-error {{parameter of type 'Atomic<Int>' must be declared as either 'borrowing' or 'consuming'}}

func l(_: (borrowing Atomic<Int>) -> ()) {} // OK

func m() {
let _: (Int, Int) -> Int = {
var n = Atomic(0) // expected-error {{variable of type 'Atomic<Int>' must be declared with a 'let'}}
// expected-warning@-1 {{initialization of variable 'n' was never used; consider replacing with assignment to '_' or removing it}}

return $0 + $1
}
}

func o(_: consuming Atomic<Int>) {} // OK

func p(_: (consuming Atomic<Int>) -> ()) {} // OK
2 changes: 2 additions & 0 deletions test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ if "@SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION@" == "TRUE":
config.available_features.add('observation')
if "@SWIFT_STDLIB_ENABLE_DEBUG_PRECONDITIONS_IN_RELEASE@" == "TRUE":
config.available_features.add('swift_stdlib_debug_preconditions_in_release')
if "@SWIFT_ENABLE_SYNCHRONIZATION@" == "TRUE":
config.available_features.add('synchronization')

config.swift_freestanding_is_darwin = "@SWIFT_FREESTANDING_IS_DARWIN@" == "TRUE"
config.swift_stdlib_use_relative_protocol_witness_tables = "@SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES@" == "TRUE"
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/Atomics/LockFreeSingleConsumerStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class LockFreeSingleConsumerStack<Element> {
}
typealias NodePtr = UnsafeMutablePointer<Node>

private var _last = Atomic<NodePtr?>(nil)
private var _consumerCount = Atomic<Int>(0)
private let _last = Atomic<NodePtr?>(nil)
private let _consumerCount = Atomic<Int>(0)
private var foo = 0

deinit {
Expand Down