Skip to content

[CS] Call checkParameterList for single-expr closures #76473

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
Sep 16, 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
52 changes: 6 additions & 46 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8883,7 +8883,8 @@ namespace {
rewriteFunction(closure);

if (AnyFunctionRef(closure).hasExternalPropertyWrapperParameters()) {
return Action::SkipNode(rewriteClosure(closure));
return Action::SkipNode(Rewriter.buildSingleCurryThunk(
closure, closure, Rewriter.cs.getConstraintLocator(closure)));
}

return Action::SkipNode(closure);
Expand Down Expand Up @@ -8935,49 +8936,6 @@ namespace {
std::optional<SyntacticElementTarget>
rewriteTarget(SyntacticElementTarget target);

AutoClosureExpr *rewriteClosure(ClosureExpr *closure) {
auto &solution = Rewriter.solution;

// Apply types to synthesized property wrapper vars.
for (auto *param : *closure->getParameters()) {
if (!param->hasAttachedPropertyWrapper())
continue;

// Set the interface type of each property wrapper synthesized var
auto *backingVar = param->getPropertyWrapperBackingProperty();
auto backingType =
solution.simplifyType(solution.getType(backingVar))->mapTypeOutOfContext();
backingVar->setInterfaceType(backingType);

if (auto *projectionVar = param->getPropertyWrapperProjectionVar()) {
projectionVar->setInterfaceType(
solution.simplifyType(solution.getType(projectionVar))->mapTypeOutOfContext());
}

auto *wrappedValueVar = param->getPropertyWrapperWrappedValueVar();
auto wrappedValueType =
solution.simplifyType(solution.getType(wrappedValueVar))->mapTypeOutOfContext();
wrappedValueVar->setInterfaceType(wrappedValueType->getWithoutSpecifierType());

if (param->hasImplicitPropertyWrapper()) {
if (wrappedValueType->is<LValueType>())
wrappedValueVar->setImplInfo(StorageImplInfo::getMutableComputed());

// Add an explicit property wrapper attribute, which is needed for
// synthesizing the accessors.
auto &context = wrappedValueVar->getASTContext();
auto *typeExpr = TypeExpr::createImplicit(backingType, context);
auto *attr = CustomAttr::create(context, SourceLoc(), typeExpr, /*implicit=*/true);
wrappedValueVar->getAttrs().add(attr);
}
}

TypeChecker::checkParameterList(closure->getParameters(), closure);

return Rewriter.buildSingleCurryThunk(
closure, closure, Rewriter.cs.getConstraintLocator(closure));
}

/// Rewrite the function for the given solution.
///
/// \returns true if an error occurred.
Expand All @@ -8996,9 +8954,11 @@ namespace {

switch (result) {
case SolutionApplicationToFunctionResult::Success: {
if (auto closure = dyn_cast_or_null<ClosureExpr>(
fn.getAbstractClosureExpr()))
if (auto closure =
dyn_cast_or_null<ClosureExpr>(fn.getAbstractClosureExpr())) {
TypeChecker::checkClosureAttributes(closure);
TypeChecker::checkParameterList(closure->getParameters(), closure);
}
return false;
}

Expand Down
42 changes: 42 additions & 0 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,46 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
};
} // namespace

static void applySolutionToClosurePropertyWrappers(ClosureExpr *closure,
const Solution &solution) {
for (auto *param : *closure->getParameters()) {
if (!param->hasAttachedPropertyWrapper())
continue;

// Set the interface type of each property wrapper synthesized var
auto *backingVar = param->getPropertyWrapperBackingProperty();
auto backingType = solution.simplifyType(solution.getType(backingVar))
->mapTypeOutOfContext();
backingVar->setInterfaceType(backingType);

if (auto *projectionVar = param->getPropertyWrapperProjectionVar()) {
projectionVar->setInterfaceType(
solution.simplifyType(solution.getType(projectionVar))
->mapTypeOutOfContext());
}

auto *wrappedValueVar = param->getPropertyWrapperWrappedValueVar();
auto wrappedValueType =
solution.simplifyType(solution.getType(wrappedValueVar))
->mapTypeOutOfContext();
wrappedValueVar->setInterfaceType(
wrappedValueType->getWithoutSpecifierType());

if (param->hasImplicitPropertyWrapper()) {
if (wrappedValueType->is<LValueType>())
wrappedValueVar->setImplInfo(StorageImplInfo::getMutableComputed());

// Add an explicit property wrapper attribute, which is needed for
// synthesizing the accessors.
auto &context = wrappedValueVar->getASTContext();
auto *typeExpr = TypeExpr::createImplicit(backingType, context);
auto *attr =
CustomAttr::create(context, SourceLoc(), typeExpr, /*implicit=*/true);
wrappedValueVar->getAttrs().add(attr);
}
}
}

SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
Solution &solution, AnyFunctionRef fn,
DeclContext *&currentDC,
Expand Down Expand Up @@ -2553,6 +2593,8 @@ SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
if (closure->hasExplicitResultType()) {
closure->setExplicitResultType(closureFnType->getResult());
}

applySolutionToClosurePropertyWrappers(closure, solution);
}

// Enter the context of the function before performing any additional
Expand Down
9 changes: 9 additions & 0 deletions test/attr/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ func testNonacceptedClosures() {

_ = fn
_ = fn2

// https://github.com/swiftlang/swift/issues/76291
_ = { (@objc x: Int) in 0 } // expected-error {{'@objc' attribute cannot be applied to this declaration}}
_ = { (@objc x: Int) in } // expected-error {{'@objc' attribute cannot be applied to this declaration}}

_ = { (@objc x: Int) in // expected-error {{'@objc' attribute cannot be applied to this declaration}}
print("hello")
return x
}
}