Skip to content

Commit 3c924be

Browse files
committed
[CSFix] InitAccessors: Add a fix that tracks invalid member references within init accessors
1 parent 935142f commit 3c924be

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

include/swift/Sema/CSFix.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ enum class FixKind : uint8_t {
447447

448448
/// Ignore missing 'each' keyword before value pack reference.
449449
IgnoreMissingEachKeyword,
450+
451+
/// Ignore the fact that member couldn't be referenced within init accessor
452+
/// because its name doesn't appear in 'initializes' or 'accesses' attributes.
453+
AllowInvalidMemberReferenceInInitAccessor,
450454
};
451455

452456
class ConstraintFix {
@@ -3536,6 +3540,39 @@ class IgnoreMissingEachKeyword final : public ConstraintFix {
35363540
}
35373541
};
35383542

3543+
class AllowInvalidMemberReferenceInInitAccessor final : public ConstraintFix {
3544+
DeclNameRef MemberName;
3545+
3546+
AllowInvalidMemberReferenceInInitAccessor(ConstraintSystem &cs,
3547+
DeclNameRef memberName,
3548+
ConstraintLocator *locator)
3549+
: ConstraintFix(cs, FixKind::AllowInvalidMemberReferenceInInitAccessor,
3550+
locator),
3551+
MemberName(memberName) {}
3552+
3553+
public:
3554+
std::string getName() const override {
3555+
llvm::SmallVector<char, 16> scratch;
3556+
auto memberName = MemberName.getString(scratch);
3557+
return "allow reference to member '" + memberName.str() +
3558+
"' in init accessor";
3559+
}
3560+
3561+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3562+
3563+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
3564+
return diagnose(*commonFixes.front().first);
3565+
}
3566+
3567+
static AllowInvalidMemberReferenceInInitAccessor *
3568+
create(ConstraintSystem &cs, DeclNameRef memberName,
3569+
ConstraintLocator *locator);
3570+
3571+
static bool classof(const ConstraintFix *fix) {
3572+
return fix->getKind() == FixKind::AllowInvalidMemberReferenceInInitAccessor;
3573+
}
3574+
};
3575+
35393576
} // end namespace constraints
35403577
} // end namespace swift
35413578

lib/Sema/CSFix.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,3 +2796,16 @@ IgnoreMissingEachKeyword::create(ConstraintSystem &cs, Type valuePackTy,
27962796
return new (cs.getAllocator())
27972797
IgnoreMissingEachKeyword(cs, valuePackTy, locator);
27982798
}
2799+
2800+
bool AllowInvalidMemberReferenceInInitAccessor::diagnose(
2801+
const Solution &solution, bool asNote) const {
2802+
return false;
2803+
}
2804+
2805+
AllowInvalidMemberReferenceInInitAccessor *
2806+
AllowInvalidMemberReferenceInInitAccessor::create(ConstraintSystem &cs,
2807+
DeclNameRef memberName,
2808+
ConstraintLocator *locator) {
2809+
return new (cs.getAllocator())
2810+
AllowInvalidMemberReferenceInInitAccessor(cs, memberName, locator);
2811+
}

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10248,7 +10248,8 @@ fixMemberRef(ConstraintSystem &cs, Type baseTy,
1024810248
return AllowInvalidStaticMemberRefOnProtocolMetatype::create(cs, locator);
1024910249

1025010250
case MemberLookupResult::UR_UnavailableWithinInitAccessor:
10251-
return nullptr;
10251+
return AllowInvalidMemberReferenceInInitAccessor::create(cs, memberName,
10252+
locator);
1025210253
}
1025310254
}
1025410255

@@ -14674,6 +14675,10 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1467414675
return recordFix(fix, 100) ? SolutionKind::Error : SolutionKind::Solved;
1467514676
}
1467614677

14678+
case FixKind::AllowInvalidMemberReferenceInInitAccessor: {
14679+
return recordFix(fix, 5) ? SolutionKind::Error : SolutionKind::Solved;
14680+
}
14681+
1467714682
case FixKind::ExplicitlyConstructRawRepresentable: {
1467814683
// Let's increase impact of this fix for binary operators because
1467914684
// it's possible to get both `.rawValue` and construction fixes for

0 commit comments

Comments
 (0)