Skip to content

[Property Wrappers] Adjust composed property wrapper constraint generation #31876

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
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
36 changes: 24 additions & 12 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4209,26 +4209,35 @@ static Expr *generateConstraintsFor(ConstraintSystem &cs, Expr *expr,
static Type generateWrappedPropertyTypeConstraints(
ConstraintSystem &cs, Type initializerType,
VarDecl *wrappedVar, ConstraintLocator *locator) {
Type valueType = LValueType::get(initializerType);
auto dc = wrappedVar->getInnermostDeclContext();

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

for (unsigned i : indices(wrappedVar->getAttachedPropertyWrappers())) {
Type rawWrapperType = wrappedVar->getAttachedPropertyWrapperType(i);
if (!rawWrapperType || rawWrapperType->hasError())
return Type();

// The former wrappedValue type must be equal to the current wrapper type
if (wrappedValueType) {
wrapperType = cs.openUnboundGenericTypes(rawWrapperType, locator);
cs.addConstraint(ConstraintKind::Equal, wrappedValueType, wrapperType,
locator);
}

auto wrapperInfo = wrappedVar->getAttachedPropertyWrapperTypeInfo(i);
if (!wrapperInfo)
break;

locator = cs.getConstraintLocator(locator, ConstraintLocator::Member);
Type memberType = cs.createTypeVariable(locator, TVO_CanBindToLValue);
cs.addValueMemberConstraint(
valueType, wrapperInfo.valueVar->createNameRef(),
memberType, dc, FunctionRefKind::Unapplied, { }, locator);
valueType = memberType;
return Type();

wrappedValueType = wrapperType->getTypeOfMember(
dc->getParentModule(), wrapperInfo.valueVar);
}

// Set up an equality constraint to drop the lvalue-ness of the value
// type we produced.
Type propertyType = cs.createTypeVariable(locator, 0);
cs.addConstraint(ConstraintKind::Equal, propertyType, valueType, locator);
cs.addConstraint(ConstraintKind::Equal, propertyType, wrappedValueType, locator);
return propertyType;
}

Expand All @@ -4245,10 +4254,13 @@ static bool generateInitPatternConstraints(
assert(patternType && "All patterns have a type");

if (auto wrappedVar = target.getInitializationWrappedVar()) {
// Add an equal constraint between the pattern type and the
// property wrapper's "value" type.
Type propertyType = generateWrappedPropertyTypeConstraints(
cs, cs.getType(target.getAsExpr()), wrappedVar, locator);
if (!propertyType)
return true;

// Add an equal constraint between the pattern type and the
// property wrapper's "value" type.
cs.addConstraint(ConstraintKind::Equal, patternType,
propertyType, locator, /*isFavored*/ true);
} else if (!patternType->is<OpaqueTypeArchetypeType>()) {
Expand Down
47 changes: 46 additions & 1 deletion test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ struct MultipleWrappers {
// attempting to splice a 'wrappedValue:' argument into the call to Wrapper's
// init, but it doesn't have a matching init. We're then attempting to access
// the nested 'wrappedValue', but Wrapper's 'wrappedValue' is Int.
@Wrapper(stored: 17) // expected-error{{value of type 'Int' has no member 'wrappedValue'}}
@Wrapper(stored: 17) // expected-error{{cannot convert value of type 'Int' to expected argument type 'WrapperWithInitialValue<Int>'}}
@WrapperWithInitialValue // expected-error{{extra argument 'wrappedValue' in call}}
var x: Int = 17

Expand Down Expand Up @@ -1069,6 +1069,51 @@ struct TestComposition {
}
}

// ---------------------------------------------------------------------------
// Property wrapper composition type inference
// ---------------------------------------------------------------------------

protocol DefaultValue {
static var defaultValue: Self { get }
}

extension Int: DefaultValue {
static var defaultValue: Int { 0 }
}

struct TestCompositionTypeInference {
@propertyWrapper
struct A<Value: DefaultValue> {
var wrappedValue: Value

init(wrappedValue: Value = .defaultValue, key: String) {
self.wrappedValue = wrappedValue
}
}

@propertyWrapper
struct B<Value: DefaultValue>: DefaultValue {
var wrappedValue: Value

init(wrappedValue: Value = .defaultValue) {
self.wrappedValue = wrappedValue
}

static var defaultValue: B<Value> { B() }
}

// All of these are okay

@A(key: "b") @B
var a: Int = 0

@A(key: "b") @B
var b: Int

@A(key: "c") @B @B
var c: Int
}

// ---------------------------------------------------------------------------
// Missing Property Wrapper Unwrap Diagnostics
// ---------------------------------------------------------------------------
Expand Down