Skip to content

[ConstraintSystem] Fix a property wrapper constraint generation crash. #36611

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
Mar 29, 2021
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
33 changes: 14 additions & 19 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3560,9 +3560,7 @@ static bool generateWrappedPropertyTypeConstraints(
Type propertyType) {
auto dc = wrappedVar->getInnermostDeclContext();

Type wrapperType = LValueType::get(initializerType);
Type wrappedValueType;

auto wrapperAttributes = wrappedVar->getAttachedPropertyWrappers();
for (unsigned i : indices(wrapperAttributes)) {
// FIXME: We should somehow pass an OpenUnboundGenericTypeFn to
Expand All @@ -3573,16 +3571,20 @@ static bool generateWrappedPropertyTypeConstraints(
if (rawWrapperType->hasError() || !wrapperInfo)
return true;

// The former wrappedValue type must be equal to the current wrapper type
if (wrappedValueType) {
auto *typeRepr = wrapperAttributes[i]->getTypeRepr();
auto *locator =
cs.getConstraintLocator(typeRepr, LocatorPathElt::ContextualType());
wrapperType = cs.replaceInferableTypesWithTypeVars(rawWrapperType,
locator);
auto *typeExpr = wrapperAttributes[i]->getTypeExpr();
auto *locator = cs.getConstraintLocator(typeExpr);
auto wrapperType = cs.replaceInferableTypesWithTypeVars(rawWrapperType, locator);
cs.setType(typeExpr, wrapperType);

if (!wrappedValueType) {
// Equate the outermost wrapper type to the initializer type.
if (initializerType)
cs.addConstraint(ConstraintKind::Equal, wrapperType, initializerType, locator);
} else {
// The former wrappedValue type must be equal to the current wrapper type
cs.addConstraint(ConstraintKind::Equal, wrapperType, wrappedValueType,
locator);
cs.setContextualType(typeRepr, TypeLoc::withoutLoc(wrappedValueType),
cs.getConstraintLocator(locator, LocatorPathElt::ContextualType()));
cs.setContextualType(typeExpr, TypeLoc::withoutLoc(wrappedValueType),
CTP_ComposedPropertyWrapper);
}

Expand Down Expand Up @@ -3888,19 +3890,12 @@ bool ConstraintSystem::generateConstraints(

case SolutionApplicationTarget::Kind::uninitializedWrappedVar: {
auto *wrappedVar = target.getAsUninitializedWrappedVar();
auto *outermostWrapper = wrappedVar->getAttachedPropertyWrappers().front();
auto *typeExpr = outermostWrapper->getTypeExpr();
auto backingType = replaceInferableTypesWithTypeVars(
outermostWrapper->getType(),getConstraintLocator(typeExpr));

setType(typeExpr, backingType);

auto propertyType = getVarType(wrappedVar);
if (propertyType->hasError())
return true;

return generateWrappedPropertyTypeConstraints(
*this, backingType, wrappedVar, propertyType);
*this, /*initializerType=*/Type(), wrappedVar, propertyType);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2076,3 +2076,20 @@ struct OptionalWrapper<T> {
struct UseOptionalWrapper {
@OptionalWrapper var p: Int?? // Okay
}

@propertyWrapper
struct WrapperWithFailableInit<Value> {
var wrappedValue: Value

init(_ base: WrapperWithFailableInit<Value>) { fatalError() }

init?(_ base: WrapperWithFailableInit<Value?>) { fatalError() }
}

struct TestInitError {
// FIXME: bad diagnostics when a wrapper does not support init from wrapped value

// expected-error@+2 {{extraneous argument label 'wrappedValue:' in call}}
// expected-error@+1 {{cannot convert value of type 'Int' to specified type 'WrapperWithFailableInit<Int>'}}
@WrapperWithFailableInit var value: Int = 10
}