Skip to content

Commit 055888f

Browse files
authored
Merge pull request #73024 from slavapestov/concurrency-capture-invariants-6.0
[6.0] Concurrency: Minor fixes for isolation checking
2 parents 235b1c8 + 5712175 commit 055888f

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,9 +2610,23 @@ namespace {
26102610
return MacroWalking::Expansion;
26112611
}
26122612

2613+
PreWalkResult<Pattern *> walkToPatternPre(Pattern *pattern) override {
2614+
// Walking into patterns leads to nothing good because then we
2615+
// end up visiting the AccessorDecls of a top-level
2616+
// PatternBindingDecl twice.
2617+
return Action::SkipNode(pattern);
2618+
}
2619+
26132620
PreWalkAction walkToDeclPre(Decl *decl) override {
2621+
// Don't walk into local types because nothing in them can
2622+
// change the outcome of our analysis, and we don't want to
2623+
// assume things there have been type checked yet.
2624+
if (isa<TypeDecl>(decl)) {
2625+
return Action::SkipChildren();
2626+
}
2627+
26142628
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
2615-
if (func->isLocalContext()) {
2629+
if (func->getDeclContext()->isLocalContext()) {
26162630
checkLocalCaptures(func);
26172631
}
26182632

@@ -5355,11 +5369,9 @@ DefaultInitializerIsolation::evaluate(Evaluator &evaluator,
53555369
return ActorIsolation::forUnspecified();
53565370

53575371
auto i = pbd->getPatternEntryIndexForVarDecl(var);
5358-
if (!pbd->isInitializerChecked(i))
5359-
TypeChecker::typeCheckPatternBinding(pbd, i);
53605372

53615373
dc = cast<Initializer>(pbd->getInitContext(i));
5362-
initExpr = pbd->getInit(i);
5374+
initExpr = pbd->getCheckedAndContextualizedInit(i);
53635375
enclosingIsolation = getActorIsolation(var);
53645376
} else if (auto *param = dyn_cast<ParamDecl>(var)) {
53655377
// If this parameter corresponds to a stored property for a

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2722,7 +2722,16 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
27222722

27232723
// Trigger a request that will complete typechecking for the
27242724
// initializer.
2725-
(void)PBD->getCheckedAndContextualizedInit(i);
2725+
(void) PBD->getCheckedAndContextualizedInit(i);
2726+
2727+
if (auto *var = PBD->getSingleVar()) {
2728+
if (var->hasAttachedPropertyWrapper())
2729+
return;
2730+
}
2731+
2732+
if (!PBD->getDeclContext()->isLocalContext()) {
2733+
(void) PBD->getInitializerIsolation(i);
2734+
}
27262735
}
27272736
}
27282737

lib/Sema/TypeCheckStmt.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,12 +2994,10 @@ bool TypeChecker::typeCheckTapBody(TapExpr *expr, DeclContext *DC) {
29942994
}
29952995

29962996
void TypeChecker::typeCheckTopLevelCodeDecl(TopLevelCodeDecl *TLCD) {
2997-
// We intentionally use typeCheckStmt instead of typeCheckBody here
2998-
// because we want to contextualize all the TopLevelCode
2999-
// declarations simultaneously.
30002997
BraceStmt *Body = TLCD->getBody();
3001-
StmtChecker(TLCD).typeCheckStmt(Body);
2998+
StmtChecker(TLCD).typeCheckBody(Body);
30022999
TLCD->setBody(Body);
3000+
30033001
checkTopLevelActorIsolation(TLCD);
30043002
checkTopLevelEffects(TLCD);
30053003
performTopLevelDeclDiagnostics(TLCD);

lib/Sema/TypeCheckStorage.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,6 @@ static void checkAndContextualizePatternBindingInit(PatternBindingDecl *binding,
617617
if (auto *initContext = binding->getInitContext(i)) {
618618
auto *init = binding->getInit(i);
619619
TypeChecker::contextualizeInitializer(initContext, init);
620-
(void)binding->getInitializerIsolation(i);
621620
TypeChecker::checkInitializerEffects(initContext, init);
622621
}
623622
}

lib/Sema/TypeChecker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ TypeCheckSourceFileRequest::evaluate(Evaluator &eval, SourceFile *SF) const {
284284
for (auto D : SF->getTopLevelDecls()) {
285285
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
286286
TypeChecker::typeCheckTopLevelCodeDecl(TLCD);
287-
TypeChecker::contextualizeTopLevelCode(TLCD);
288287
} else {
289288
TypeChecker::typeCheckDecl(D);
290289
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 6
2+
3+
class NonSendable {} // expected-note 3{{class 'NonSendable' does not conform to the 'Sendable' protocol}}
4+
5+
func callee(_: @Sendable () -> NonSendable) {}
6+
7+
var testLocalCaptures: Int {
8+
let ns = NonSendable()
9+
10+
@Sendable func localFunc() -> NonSendable {
11+
return ns // expected-error {{capture of 'ns' with non-sendable type 'NonSendable' in a `@Sendable` local function}}
12+
}
13+
14+
callee { return ns } // expected-error {{capture of 'ns' with non-sendable type 'NonSendable' in a `@Sendable` closure}}
15+
16+
return 3
17+
}
18+
19+
struct Bad {
20+
var c: Int = {
21+
let ns = NonSendable()
22+
callee { return ns } // expected-error {{capture of 'ns' with non-sendable type 'NonSendable' in a `@Sendable` closure}}
23+
return 3
24+
}()
25+
}

0 commit comments

Comments
 (0)