Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 2c3382f

Browse files
committed
Merge branch 'main' of github.com:apple/swift into tensorflow-stage
* 'main' of github.com:apple/swift: [Concurrency] Disallow global actor annotations on stored properties of structs. [Concurrency] Implicitly strip optionals for return type of translated "async throws".
2 parents ec6aff0 + be0ca0a commit 2c3382f

File tree

8 files changed

+32
-11
lines changed

8 files changed

+32
-11
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,8 @@ ERROR(global_actor_on_actor_class,none,
43264326
"actor class %0 cannot have a global actor", (Identifier))
43274327
ERROR(global_actor_on_local_variable,none,
43284328
"local variable %0 cannot have a global actor", (DeclName))
4329+
ERROR(global_actor_on_struct_property,none,
4330+
"stored property %0 of a struct cannot have a global actor", (DeclName))
43294331

43304332
ERROR(actor_isolation_multiple_attr,none,
43314333
"%0 %1 has multiple actor-isolation attributes ('%2' and '%3')",

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,13 @@ static Type decomposeCompletionHandlerType(
20652065
paramIdx == *info.completionHandlerErrorParamIndex())
20662066
continue;
20672067

2068-
resultTypeElts.push_back(param.getPlainType());
2068+
// If there is an error parameter, remove nullability.
2069+
Type paramType = param.getPlainType();
2070+
// TODO: Clang should gain a nullability form that overrides this.
2071+
if (info.completionHandlerErrorParamIndex())
2072+
paramType = paramType->lookThroughAllOptionalTypes();
2073+
2074+
resultTypeElts.push_back(paramType);
20692075
}
20702076

20712077
switch (resultTypeElts.size()) {

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ GlobalActorAttributeRequest::evaluate(
416416
.highlight(globalActorAttr->getRangeWithAt());
417417
return None;
418418
}
419+
420+
// Global actors don't make sense on a stored property of a struct.
421+
if (var->hasStorage() && var->getDeclContext()->getSelfStructDecl() &&
422+
var->isInstanceMember()) {
423+
var->diagnose(diag::global_actor_on_struct_property, var->getName())
424+
.highlight(globalActorAttr->getRangeWithAt());
425+
return None;
426+
}
427+
419428
}
420429
} else if (isa<ExtensionDecl>(decl)) {
421430
// Extensions are okay.

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,8 +1946,10 @@ bool swift::fixDeclarationName(InFlightDiagnostic &diag, const ValueDecl *decl,
19461946
}
19471947

19481948
// Fix the argument names that need fixing.
1949-
assert(name.getArgumentNames().size()
1950-
== targetName.getArgumentNames().size());
1949+
if (name.getArgumentNames().size()
1950+
!= targetName.getArgumentNames().size())
1951+
return false;
1952+
19511953
auto params = func->getParameters();
19521954
for (unsigned i = 0, n = name.getArgumentNames().size(); i != n; ++i) {
19531955
auto origArg = name.getArgumentNames()[i];

test/ClangImporter/objc_async.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import ObjCConcurrency
88
func testSlowServer(slowServer: SlowServer) async throws {
99
let _: Int = await slowServer.doSomethingSlow("mail")
1010
let _: Bool = await slowServer.checkAvailability()
11-
let _: String = try await slowServer.findAnswer() ?? "nope"
12-
let _: String = await try slowServer.findAnswerFailingly() ?? "nope"
11+
let _: String = try await slowServer.findAnswer()
12+
let _: String = await try slowServer.findAnswerFailingly()
1313
let _: Void = await slowServer.doSomethingFun("jump")
1414
let _: (Int) -> Void = slowServer.completionHandler
1515

test/IDE/print_clang_objc_async.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
// CHECK-DAG: func doSomethingSlow(_ operation: String, completionHandler handler: @escaping (Int) -> Void)
1111
// CHECK-DAG: func doSomethingSlow(_ operation: String) async -> Int
1212
// CHECK-DAG: func doSomethingDangerous(_ operation: String, completionHandler handler: ((String?, Error?) -> Void)? = nil)
13-
// CHECK-DAG: func doSomethingDangerous(_ operation: String) async throws -> String?
13+
// CHECK-DAG: func doSomethingDangerous(_ operation: String) async throws -> String
1414
// CHECK-DAG: func checkAvailability(completionHandler: @escaping (Bool) -> Void)
1515
// CHECK-DAG: func checkAvailability() async -> Bool
1616
// CHECK-DAG: func findAnswer(completionHandler handler: @escaping (String?, Error?) -> Void)
17-
// CHECK-DAG: func findAnswer() async throws -> String?
17+
// CHECK-DAG: func findAnswer() async throws -> String
1818
// CHECK-DAG: func findAnswerFailingly(completionHandler handler: @escaping (String?, Error?) -> Void) throws
19-
// CHECK-DAG: func findAnswerFailingly() async throws -> String?
19+
// CHECK-DAG: func findAnswerFailingly() async throws -> String
2020
// CHECK-DAG: func doSomethingFun(_ operation: String) async
2121
// CHECK: {{^[}]$}}
2222

test/attr/global_actor.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ struct OtherGlobalActor {
6464
_ = x
6565
}
6666

67-
@GA1 struct X { }
67+
@GA1 struct X {
68+
@GA1 var member: Int // expected-error{{stored property 'member' of a struct cannot have a global actor}}
69+
}
6870

6971
struct Y {
7072
@GA1 subscript(i: Int) -> Int { i }

test/decl/protocol/conforms/objc_async.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ObjCConcurrency
77

88
// Conform via async method
99
class C1: ConcurrentProtocol {
10-
func askUser(toSolvePuzzle puzzle: String) async throws -> String? { nil }
10+
func askUser(toSolvePuzzle puzzle: String) async throws -> String { "" }
1111

1212
func askUser(toJumpThroughHoop hoop: String) async -> String { "hello" }
1313
}
@@ -26,7 +26,7 @@ class C2: ConcurrentProtocol {
2626
// Conform via both; this is an error
2727
class C3: ConcurrentProtocol {
2828
// expected-note@+1{{method 'askUser(toSolvePuzzle:)' declared here}}
29-
func askUser(toSolvePuzzle puzzle: String) async throws -> String? { nil }
29+
func askUser(toSolvePuzzle puzzle: String) async throws -> String { "" }
3030

3131
// expected-error@+1{{'askUser(toSolvePuzzle:completionHandler:)' with Objective-C selector 'askUserToSolvePuzzle:completionHandler:' conflicts with method 'askUser(toSolvePuzzle:)' with the same Objective-C selector}}
3232
func askUser(toSolvePuzzle puzzle: String, completionHandler: ((String?, Error?) -> Void)?) {

0 commit comments

Comments
 (0)