Skip to content

[DNM] [Sema] Check whether wrappedValue initializer exists when generating … #68571

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 23 additions & 15 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4863,26 +4863,29 @@ ConstraintSystem::TypeMatchResult
ConstraintSystem::applyPropertyWrapperToParameter(
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
ConstraintKind matchKind, ConstraintLocatorBuilder locator) {

Expr *anchor = getAsExpr(locator.getAnchor());
if (auto *apply = dyn_cast<ApplyExpr>(anchor)) {
anchor = apply->getFn();
}

if (argLabel.hasDollarPrefix() && (!param || !param->hasExternalPropertyWrapper())) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

recordAnyTypeVarAsPotentialHole(paramType);
if (argLabel.hasDollarPrefix()) {
// Add constraints for projected property value
if (!param || !param->hasExternalPropertyWrapper()) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

auto *loc = getConstraintLocator(locator);
auto *fix = RemoveProjectedValueArgument::create(*this, wrapperType, param, loc);
if (recordFix(fix))
return getTypeMatchFailure(locator);
recordAnyTypeVarAsPotentialHole(paramType);

return getTypeMatchSuccess();
}
auto *loc = getConstraintLocator(locator);
auto *fix = RemoveProjectedValueArgument::create(*this, wrapperType, param, loc);
if (recordFix(fix)) {
return getTypeMatchFailure(locator);
} else {
return getTypeMatchSuccess();
}
}

if (argLabel.hasDollarPrefix()) {
Type projectionType = computeProjectedValueType(param, wrapperType);
addConstraint(matchKind, paramType, projectionType, locator);
if (param->hasImplicitPropertyWrapper()) {
Expand All @@ -4893,14 +4896,19 @@ ConstraintSystem::applyPropertyWrapperToParameter(
}

appliedPropertyWrappers[anchor].push_back({ wrapperType, PropertyWrapperInitKind::ProjectedValue });
} else if (param->hasExternalPropertyWrapper()) {
} else {
// Add constraints for wrapped property value

if (!param->allAttachedPropertyWrappersHaveWrappedValueInit()) {
return getTypeMatchFailure(locator);
// TODO: Record a CSFix instead?
}

Type wrappedValueType = computeWrappedValueType(param, wrapperType);
addConstraint(matchKind, paramType, wrappedValueType, locator);
setType(param->getPropertyWrapperWrappedValueVar(), wrappedValueType);

appliedPropertyWrappers[anchor].push_back({ wrapperType, PropertyWrapperInitKind::WrappedValue });
} else {
return getTypeMatchFailure(locator);
}

return getTypeMatchSuccess();
Expand Down
11 changes: 11 additions & 0 deletions test/Sema/property_wrapper_parameter_invalid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,14 @@ func testInvalidWrapperInference() {
// expected-error@+1 {{contextual closure type '() -> Void' expects 0 arguments, but 1 was used in closure body}}
testExtraParameter { $value in }
}

@propertyWrapper
struct ProjectionOnlyWrapper {
var wrappedValue: String

var projectedValue: String { self.wrappedValue }
init(projectedValue: String) { self.wrappedValue = projectedValue }
}

func testParameterWithoutWrappedValueInit(@ProjectionOnlyWrapper value: String) {}
_ = testParameterWithoutWrappedValueInit(value: "") // expected-error {{type of expression is ambiguous without a type annotation}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new line, please.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new line, please.