Skip to content

[CSGen] Fix compiler crash when property wrapper applied to a param lacks wrappedValue initializer #77689

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 1 commit into from
Nov 20, 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
23 changes: 20 additions & 3 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5060,18 +5060,35 @@ ConstraintSystem::applyPropertyWrapperToParameter(
anchor = apply->getFn();
}

if (argLabel.hasDollarPrefix() && (!param || !param->hasExternalPropertyWrapper())) {
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> TypeMatchResult {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

recordAnyTypeVarAsPotentialHole(paramType);

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

return getTypeMatchSuccess();
};

// Incorrect use of projected value argument
if (argLabel.hasDollarPrefix() &&
(!param || !param->hasExternalPropertyWrapper())) {
auto *loc = getConstraintLocator(locator);
auto *fix =
RemoveProjectedValueArgument::create(*this, wrapperType, param, loc);
return recordPropertyWrapperFix(fix);
}

// Missing wrapped value initializer
auto wrapperInfo = param->getAttachedPropertyWrapperTypeInfo(0);
if (!argLabel.hasDollarPrefix() && !wrapperInfo.wrappedValueInit &&
param->hasExternalPropertyWrapper()) {
auto *loc = getConstraintLocator(locator);
auto *fix = UsePropertyWrapper::create(
*this, param, /*usingProjection=*/true, paramType, wrapperType, loc);
return recordPropertyWrapperFix(fix);
}

if (argLabel.hasDollarPrefix()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-typecheck-verify-swift

// https://github.com/swiftlang/swift/issues/65500

@propertyWrapper
struct Wrapper {
var wrappedValue: Bool { true }
init(wrappedValue: Bool, er: Void) {}

var projectedValue: Bool { true }
init(projectedValue: Bool) {}
}

func test(@Wrapper x: Bool) {}
test(x: false)
// expected-error@-1 {{cannot convert value 'x' of type 'Bool' to expected type 'Wrapper', use wrapper instead}}{{9-9=$}}