Skip to content

Commit c9b53ec

Browse files
committed
[ConstraintSystem] Created a locator in CSGen to store the location where the wrappedValue type of a property wrapper was determined and added necessary logic so that a constraint locator can recognize a composed property wrapper’s wrapped value type in CSFix.cpp, CSFix.h, and CSSimplify.cpp.
1 parent 11efb7d commit c9b53ec

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

include/swift/Sema/CSFix.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ enum class FixKind : uint8_t {
322322
/// even though result type of the reference doesn't conform
323323
/// to an expected protocol.
324324
AllowInvalidStaticMemberRefOnProtocolMetatype,
325+
326+
/// Allow the wrappedValue type of the outermost property
327+
/// wrapper of a composed property wrapper to mismatch
328+
/// the type of its innermost property wrapper.
329+
AllowWrappedValueMismatch,
325330
};
326331

327332
class ConstraintFix {
@@ -615,6 +620,20 @@ class TreatArrayLiteralAsDictionary final : public ContextualMismatch {
615620
ConstraintLocator *loc);
616621
};
617622

623+
class AllowWrappedValueMismatch : public ContextualMismatch {
624+
AllowWrappedValueMismatch(ConstraintSystem &cs, Type lhs, Type rhs,
625+
ConstraintLocator *locator)
626+
: ContextualMismatch(cs, FixKind::AllowWrappedValueMismatch, lhs, rhs, locator) {}
627+
628+
public:
629+
std::string getName() const override { return "fix wrapped value type mismatch"; }
630+
631+
bool diagnose(const Solution &solution, bool asNote = false) const override;
632+
633+
static AllowWrappedValueMismatch *create(ConstraintSystem &cs, Type lhs, Type rhs,
634+
ConstraintLocator *locator);
635+
};
636+
618637
/// Mark function type as explicitly '@escaping'.
619638
class MarkExplicitlyEscaping final : public ContextualMismatch {
620639
MarkExplicitlyEscaping(ConstraintSystem &cs, Type lhs, Type rhs,

lib/Sema/CSFix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ ContextualMismatch *ContextualMismatch::create(ConstraintSystem &cs, Type lhs,
372372
return new (cs.getAllocator()) ContextualMismatch(cs, lhs, rhs, locator);
373373
}
374374

375+
bool AllowWrappedValueMismatch::diagnose(const Solution &solution, bool asError) const {
376+
WrappedValueMismatch failure(solution, getFromType(), getToType(), getLocator());
377+
return(failure.diagnoseAsError());
378+
}
379+
380+
AllowWrappedValueMismatch *AllowWrappedValueMismatch::create(ConstraintSystem &cs,
381+
Type lhs,
382+
Type rhs,
383+
ConstraintLocator *locator) {
384+
return new (cs.getAllocator()) AllowWrappedValueMismatch(cs, lhs, rhs, locator);
385+
}
386+
375387
/// Computes the contextual type information for a type mismatch of a
376388
/// component in a structural type (tuple or function type).
377389
///

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,6 +3590,7 @@ static bool generateWrappedPropertyTypeConstraints(
35903590
auto dc = wrappedVar->getInnermostDeclContext();
35913591

35923592
Type wrappedValueType;
3593+
Type wrapperType = initializerType;
35933594
auto wrapperAttributes = wrappedVar->getAttachedPropertyWrappers();
35943595
for (unsigned i : indices(wrapperAttributes)) {
35953596
// FIXME: We should somehow pass an OpenUnboundGenericTypeFn to
@@ -3601,8 +3602,8 @@ static bool generateWrappedPropertyTypeConstraints(
36013602
return true;
36023603

36033604
auto *typeExpr = wrapperAttributes[i]->getTypeExpr();
3604-
auto *locator = cs.getConstraintLocator(typeExpr);
3605-
auto wrapperType = cs.replaceInferableTypesWithTypeVars(rawWrapperType, locator);
3605+
auto *locator = cs.getConstraintLocator(typeExpr, LocatorPathElt::WrappedValue(wrapperType.getPointer()));
3606+
wrapperType = cs.replaceInferableTypesWithTypeVars(rawWrapperType, locator);
36063607
cs.setType(typeExpr, wrapperType);
36073608

36083609
if (!wrappedValueType) {
@@ -3611,10 +3612,7 @@ static bool generateWrappedPropertyTypeConstraints(
36113612
cs.addConstraint(ConstraintKind::Equal, wrapperType, initializerType, locator);
36123613
} else {
36133614
// The former wrappedValue type must be equal to the current wrapper type
3614-
cs.addConstraint(ConstraintKind::Equal, wrapperType, wrappedValueType,
3615-
cs.getConstraintLocator(locator, LocatorPathElt::ContextualType()));
3616-
cs.setContextualType(typeExpr, TypeLoc::withoutLoc(wrappedValueType),
3617-
CTP_ComposedPropertyWrapper);
3615+
cs.addConstraint(ConstraintKind::Equal, wrapperType, wrappedValueType, locator);
36183616
}
36193617

36203618
wrappedValueType = wrapperType->getTypeOfMember(

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,6 +4312,12 @@ bool ConstraintSystem::repairFailures(
43124312
break;
43134313
}
43144314

4315+
case ConstraintLocator::WrappedValue: {
4316+
conversionsOrFixes.push_back(AllowWrappedValueMismatch::create(
4317+
*this, lhs, rhs, getConstraintLocator(locator)));
4318+
break;
4319+
}
4320+
43154321
case ConstraintLocator::FunctionArgument: {
43164322
auto *argLoc = getConstraintLocator(
43174323
locator.withPathElement(LocatorPathElt::SynthesizedArgument(0)));
@@ -11391,6 +11397,11 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1139111397

1139211398
return SolutionKind::Solved;
1139311399
}
11400+
11401+
case FixKind::AllowWrappedValueMismatch: {
11402+
if (recordFix(fix)) return SolutionKind::Error;
11403+
return SolutionKind::Solved;
11404+
}
1139411405

1139511406
case FixKind::UseSubscriptOperator:
1139611407
case FixKind::ExplicitlyEscaping:

0 commit comments

Comments
 (0)