Skip to content

Commit c9c962b

Browse files
authored
Merge pull request #76473 from hamishknight/attrick
[CS] Call `checkParameterList` for single-expr closures
2 parents 84e93a2 + b1c90fe commit c9c962b

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

lib/Sema/CSApply.cpp

+6-46
Original file line numberDiff line numberDiff line change
@@ -8883,7 +8883,8 @@ namespace {
88838883
rewriteFunction(closure);
88848884

88858885
if (AnyFunctionRef(closure).hasExternalPropertyWrapperParameters()) {
8886-
return Action::SkipNode(rewriteClosure(closure));
8886+
return Action::SkipNode(Rewriter.buildSingleCurryThunk(
8887+
closure, closure, Rewriter.cs.getConstraintLocator(closure)));
88878888
}
88888889

88898890
return Action::SkipNode(closure);
@@ -8935,49 +8936,6 @@ namespace {
89358936
std::optional<SyntacticElementTarget>
89368937
rewriteTarget(SyntacticElementTarget target);
89378938

8938-
AutoClosureExpr *rewriteClosure(ClosureExpr *closure) {
8939-
auto &solution = Rewriter.solution;
8940-
8941-
// Apply types to synthesized property wrapper vars.
8942-
for (auto *param : *closure->getParameters()) {
8943-
if (!param->hasAttachedPropertyWrapper())
8944-
continue;
8945-
8946-
// Set the interface type of each property wrapper synthesized var
8947-
auto *backingVar = param->getPropertyWrapperBackingProperty();
8948-
auto backingType =
8949-
solution.simplifyType(solution.getType(backingVar))->mapTypeOutOfContext();
8950-
backingVar->setInterfaceType(backingType);
8951-
8952-
if (auto *projectionVar = param->getPropertyWrapperProjectionVar()) {
8953-
projectionVar->setInterfaceType(
8954-
solution.simplifyType(solution.getType(projectionVar))->mapTypeOutOfContext());
8955-
}
8956-
8957-
auto *wrappedValueVar = param->getPropertyWrapperWrappedValueVar();
8958-
auto wrappedValueType =
8959-
solution.simplifyType(solution.getType(wrappedValueVar))->mapTypeOutOfContext();
8960-
wrappedValueVar->setInterfaceType(wrappedValueType->getWithoutSpecifierType());
8961-
8962-
if (param->hasImplicitPropertyWrapper()) {
8963-
if (wrappedValueType->is<LValueType>())
8964-
wrappedValueVar->setImplInfo(StorageImplInfo::getMutableComputed());
8965-
8966-
// Add an explicit property wrapper attribute, which is needed for
8967-
// synthesizing the accessors.
8968-
auto &context = wrappedValueVar->getASTContext();
8969-
auto *typeExpr = TypeExpr::createImplicit(backingType, context);
8970-
auto *attr = CustomAttr::create(context, SourceLoc(), typeExpr, /*implicit=*/true);
8971-
wrappedValueVar->getAttrs().add(attr);
8972-
}
8973-
}
8974-
8975-
TypeChecker::checkParameterList(closure->getParameters(), closure);
8976-
8977-
return Rewriter.buildSingleCurryThunk(
8978-
closure, closure, Rewriter.cs.getConstraintLocator(closure));
8979-
}
8980-
89818939
/// Rewrite the function for the given solution.
89828940
///
89838941
/// \returns true if an error occurred.
@@ -8996,9 +8954,11 @@ namespace {
89968954

89978955
switch (result) {
89988956
case SolutionApplicationToFunctionResult::Success: {
8999-
if (auto closure = dyn_cast_or_null<ClosureExpr>(
9000-
fn.getAbstractClosureExpr()))
8957+
if (auto closure =
8958+
dyn_cast_or_null<ClosureExpr>(fn.getAbstractClosureExpr())) {
90018959
TypeChecker::checkClosureAttributes(closure);
8960+
TypeChecker::checkParameterList(closure->getParameters(), closure);
8961+
}
90028962
return false;
90038963
}
90048964

lib/Sema/CSSyntacticElement.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,46 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
25232523
};
25242524
} // namespace
25252525

2526+
static void applySolutionToClosurePropertyWrappers(ClosureExpr *closure,
2527+
const Solution &solution) {
2528+
for (auto *param : *closure->getParameters()) {
2529+
if (!param->hasAttachedPropertyWrapper())
2530+
continue;
2531+
2532+
// Set the interface type of each property wrapper synthesized var
2533+
auto *backingVar = param->getPropertyWrapperBackingProperty();
2534+
auto backingType = solution.simplifyType(solution.getType(backingVar))
2535+
->mapTypeOutOfContext();
2536+
backingVar->setInterfaceType(backingType);
2537+
2538+
if (auto *projectionVar = param->getPropertyWrapperProjectionVar()) {
2539+
projectionVar->setInterfaceType(
2540+
solution.simplifyType(solution.getType(projectionVar))
2541+
->mapTypeOutOfContext());
2542+
}
2543+
2544+
auto *wrappedValueVar = param->getPropertyWrapperWrappedValueVar();
2545+
auto wrappedValueType =
2546+
solution.simplifyType(solution.getType(wrappedValueVar))
2547+
->mapTypeOutOfContext();
2548+
wrappedValueVar->setInterfaceType(
2549+
wrappedValueType->getWithoutSpecifierType());
2550+
2551+
if (param->hasImplicitPropertyWrapper()) {
2552+
if (wrappedValueType->is<LValueType>())
2553+
wrappedValueVar->setImplInfo(StorageImplInfo::getMutableComputed());
2554+
2555+
// Add an explicit property wrapper attribute, which is needed for
2556+
// synthesizing the accessors.
2557+
auto &context = wrappedValueVar->getASTContext();
2558+
auto *typeExpr = TypeExpr::createImplicit(backingType, context);
2559+
auto *attr =
2560+
CustomAttr::create(context, SourceLoc(), typeExpr, /*implicit=*/true);
2561+
wrappedValueVar->getAttrs().add(attr);
2562+
}
2563+
}
2564+
}
2565+
25262566
SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
25272567
Solution &solution, AnyFunctionRef fn,
25282568
DeclContext *&currentDC,
@@ -2553,6 +2593,8 @@ SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
25532593
if (closure->hasExplicitResultType()) {
25542594
closure->setExplicitResultType(closureFnType->getResult());
25552595
}
2596+
2597+
applySolutionToClosurePropertyWrappers(closure, solution);
25562598
}
25572599

25582600
// Enter the context of the function before performing any additional

test/attr/closures.swift

+9
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,13 @@ func testNonacceptedClosures() {
1212

1313
_ = fn
1414
_ = fn2
15+
16+
// https://github.com/swiftlang/swift/issues/76291
17+
_ = { (@objc x: Int) in 0 } // expected-error {{'@objc' attribute cannot be applied to this declaration}}
18+
_ = { (@objc x: Int) in } // expected-error {{'@objc' attribute cannot be applied to this declaration}}
19+
20+
_ = { (@objc x: Int) in // expected-error {{'@objc' attribute cannot be applied to this declaration}}
21+
print("hello")
22+
return x
23+
}
1524
}

0 commit comments

Comments
 (0)